Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from __future__ import print_function
- import os
- import sys
- import re
- reload(sys)
- sys.setdefaultencoding('utf-8')
- __version__ = '1.0.0'
- DEFAULT_JSON_FILE = 'epg2xml.json'
- DEFAULT_M3U_FILE = 'channels.m3u'
- RE_MY_CHANNELS = re.compile(r'"MyChannels"\s*:\s*"([^"]*)"')
- def update_channel_enabled():
- json_file = DEFAULT_JSON_FILE
- m3u_file = DEFAULT_M3U_FILE
- for argv in sys.argv:
- filename = os.path.basename(argv)
- if filename == 'epg2xml.json':
- json_file = argv
- elif os.path.splitext(filename)[1] == '.m3u':
- m3u_file = argv
- used_channels = get_used_channels(m3u_file)
- new_my_channels = ','.join(used_channels)
- print_log('epg2xml.json 파일의 "MyChannels"를 수정합니다.')
- try:
- with open(json_file, 'r+') as f:
- lines = []
- for line in f:
- rs = RE_MY_CHANNELS.search(line)
- if rs is not None:
- line = line.replace(rs.group(1), new_my_channels)
- lines.append(line)
- f.seek(0)
- f.writelines(lines)
- f.truncate()
- except IOError:
- print_error(json_file + ' 파일을 기록할 수 없습니다.')
- sys.exit()
- print_log('epg2xml.json 파일의 "MyChannels" 수정을 완료했습니다.')
- def get_used_channels(channel_file):
- print_log('사용중인 채널 ID를 수집중입니다.')
- tvg_id_list = []
- try:
- with open(channel_file) as f:
- for line in f:
- m = re.search(r'tvg-id="(\d+)"', line)
- if m is not None:
- tvg_id = str(m.groups()[0])
- tvg_id_list.append(tvg_id)
- except IOError:
- print_error(channel_file + ' 파일을 읽을 수 없습니다.')
- sys.exit()
- return sorted(tvg_id_list, key=lambda x: int(x))
- def print_log(*args):
- print(*args, file=sys.stderr)
- def print_error(*args):
- print("Error:", *args, file=sys.stderr)
- update_channel_enabled()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement