SHARE
TWEET

update_mychannels

a guest Sep 6th, 2017 2,038 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from __future__ import print_function
  5. import os
  6. import sys
  7. import re
  8.  
  9. reload(sys)
  10. sys.setdefaultencoding('utf-8')
  11.  
  12. __version__ = '1.0.0'
  13.  
  14.  
  15. DEFAULT_JSON_FILE = 'epg2xml.json'
  16. DEFAULT_M3U_FILE = 'channels.m3u'
  17. RE_MY_CHANNELS = re.compile(r'"MyChannels"\s*:\s*"([^"]*)"')
  18.  
  19.  
  20. def update_channel_enabled():
  21.     json_file = DEFAULT_JSON_FILE
  22.     m3u_file = DEFAULT_M3U_FILE
  23.  
  24.     for argv in sys.argv:
  25.         filename = os.path.basename(argv)
  26.         if filename == 'epg2xml.json':
  27.             json_file = argv
  28.         elif os.path.splitext(filename)[1] == '.m3u':
  29.             m3u_file = argv
  30.  
  31.     used_channels = get_used_channels(m3u_file)
  32.     new_my_channels = ','.join(used_channels)
  33.  
  34.     print_log('epg2xml.json 파일의 "MyChannels"를 수정합니다.')
  35.     try:
  36.         with open(json_file, 'r+') as f:
  37.             lines = []
  38.             for line in f:
  39.                 rs = RE_MY_CHANNELS.search(line)
  40.                 if rs is not None:
  41.                     line = line.replace(rs.group(1), new_my_channels)
  42.  
  43.                 lines.append(line)
  44.  
  45.             f.seek(0)
  46.             f.writelines(lines)
  47.             f.truncate()
  48.  
  49.     except IOError:
  50.         print_error(json_file + ' 파일을 기록할 수 없습니다.')
  51.         sys.exit()
  52.  
  53.     print_log('epg2xml.json 파일의 "MyChannels" 수정을 완료했습니다.')
  54.  
  55.  
  56. def get_used_channels(channel_file):
  57.     print_log('사용중인 채널 ID를 수집중입니다.')
  58.  
  59.     tvg_id_list = []
  60.  
  61.     try:
  62.         with open(channel_file) as f:
  63.             for line in f:
  64.                 m = re.search(r'tvg-id="(\d+)"', line)
  65.                 if m is not None:
  66.                     tvg_id = str(m.groups()[0])
  67.                     tvg_id_list.append(tvg_id)
  68.     except IOError:
  69.         print_error(channel_file + ' 파일을 읽을 수 없습니다.')
  70.         sys.exit()
  71.  
  72.     return sorted(tvg_id_list, key=lambda x: int(x))
  73.  
  74.  
  75. def print_log(*args):
  76.     print(*args, file=sys.stderr)
  77.  
  78.  
  79. def print_error(*args):
  80.     print("Error:", *args, file=sys.stderr)
  81.  
  82.  
  83. update_channel_enabled()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top