diff --git a/epg2xml.py b/epg2xml.py index bc61da5..3c50cc0 100644 --- a/epg2xml.py +++ b/epg2xml.py @@ -45,6 +45,7 @@ if not sys.version_info[:2] == (2, 7): debug = False today = datetime.date.today() ua = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36', 'accept': '*/*'} +timeout = 3 CHANNEL_ERROR = ' 존재하지 않는 채널입니다.' CONTENT_ERROR = ' EPG 정보가 없습니다.' HTTP_ERROR = ' EPG 정보를 가져오는데 문제가 있습니다.' @@ -114,6 +115,8 @@ def getEpg(): GetEPGFromLG(ChannelInfo) elif ChannelSource == 'SK': GetEPGFromSK(ChannelInfo) + elif ChannelSource == 'SKB': + GetEPGFromSKB(ChannelInfo) elif ChannelSource == 'SKY': GetEPGFromSKY(ChannelInfo) elif ChannelSource == 'NAVER': @@ -143,7 +146,7 @@ def GetEPGFromEPG(ChannelInfo): day = today + datetime.timedelta(days=k) params = {'beforegroup':'100', 'checkchannel':ServiceId, 'select_group':'100', 'start_date':day.strftime('%Y%m%d')} try: - response = requests.post(url, data=params, headers=ua, timeout=3) + response = requests.post(url, data=params, headers=ua, timeout=timeout) response.raise_for_status() html_data = response.content data = unicode(html_data, 'euc-kr', 'ignore').encode('utf-8', 'ignore') @@ -207,7 +210,7 @@ def GetEPGFromKT(ChannelInfo): day = today + datetime.timedelta(days=k) params = {'ch_name':'', 'ch_no':ServiceId, 'nowdate':day.strftime('%Y%m%d'), 'seldatie':day.strftime('%Y%m%d'), 'tab_no':'1'} try: - response = requests.get(url, params=params, headers=ua, timeout=3) + response = requests.get(url, params=params, headers=ua, timeout=timeout) response.raise_for_status() html_data = response.content data = unicode(html_data, 'euc-kr', 'ignore').encode('utf-8', 'ignore') @@ -258,7 +261,7 @@ def GetEPGFromLG(ChannelInfo): day = today + datetime.timedelta(days=k) params = {'chnlCd': ServiceId, 'evntCmpYmd': day.strftime('%Y%m%d')} try: - response = requests.post(url, data=params, headers=ua, timeout=3) + response = requests.post(url, data=params, headers=ua, timeout=timeout) response.raise_for_status() html_data = response.content data = unicode(html_data, 'euc-kr', 'ignore').encode('utf-8', 'ignore') @@ -310,7 +313,7 @@ def GetEPGFromSK(ChannelInfo): url = 'http://m.btvplus.co.kr/Common/Inc/IFGetData.asp' params = {'variable': 'IF_LIVECHART_DETAIL', 'pcode':'|^|start_time=' + today.strftime('%Y%m%d') + '00|^|end_time='+ lastday.strftime('%Y%m%d') + '24|^|svc_id=' + str(ServiceId)} try: - response = requests.post(url, data=params, headers=ua, timeout=3) + response = requests.post(url, data=params, headers=ua, timeout=timeout) response.raise_for_status() json_data = response.text try: @@ -353,6 +356,65 @@ def GetEPGFromSK(ChannelInfo): if(debug): printError(ChannelName + str(e)) else: pass +#Get EPG data from SKB +def GetEPGFromSKB(ChannelInfo): + ChannelId = ChannelInfo[0] + ChannelName = ChannelInfo[1] + ServiceId = ChannelInfo[3] + url = 'http://www.skbroadband.com/content/realtime/Channel_List.do' + epginfo = [] + for k in range(period): + day = today + datetime.timedelta(days=k) + params = {'key_depth2': ServiceId, 'key_depth3': day.strftime('%Y%m%d'), 'tab_gubun':'lst'} + try: + response = requests.post(url, data=params, headers=ua, timeout=timeout) + response.raise_for_status() + html_data = response.content + data = unicode(html_data, 'euc-kr', 'ignore').encode('utf-8', 'ignore') + strainer = SoupStrainer('tr', {'class':day.strftime('%Y%m%d')}) + soup = BeautifulSoup(data, 'lxml', parse_only=strainer, from_encoding='utf-8') + html = soup.find_all('tr') if soup.find_all('tr') else '' + if(html): + for row in html: + startTime = str(day) + ' ' + row.find('th').text + for cell in [row.find_all('td')]: + pattern = "(.*?)\s*(\(([\d,]+)회\))?\s*(<(.*)>)?\s*(\(재\)?)" + pattern = "^(.*?)(\(([\d,]+)회\))?(<(.*)>)?(\((재)\))?$" + matches = re.match(pattern, cell[0].text.decode('string_escape')) + if not(matches is None) : + programName = matches.group(1) if matches.group(1) else '' + subprogramName = matches.group(5) if matches.group(5) else '' + rebroadcast = True if matches.group(7) else False + episode = matches.group(3) if matches.group(3) else '' + rating = re.match('.*\s*([\d,]+)\s*.*', cell[1].text.decode('string_escape')) + if not(rating is None) : + rating = int(rating.group(1)) + else : + rating = 0 + #programName, startTime, rating, subprogramName, rebroadcast, episode + epginfo.append([programName, startTime, rating, subprogramName, rebroadcast, episode]) + else: + if(debug): printError(ChannelName + CONTENT_ERROR) + else: pass + except (requests.exceptions.RequestException) as e: + if(debug): printError(ChannelName + str(e)) + else: pass + for epg1, epg2 in zip(epginfo, epginfo[1:]): + programName = epg1[0] if epg1[0] else '' + subprogramName = epg1[3] if epg1[3] else '' + episode = epg1[5] if epg1[5] else '' + rebroadcast = epg1[4] if epg1[4] else False + startTime = datetime.datetime.strptime(epg1[1], '%Y-%m-%d %H:%M') + startTime = startTime.strftime('%Y%m%d%H%M%S') + endTime = datetime.datetime.strptime(epg2[1], '%Y-%m-%d %H:%M') + endTime = endTime.strftime('%Y%m%d%H%M%S') + category = '' + desc = '' + actors = '' + producers = '' + rating = epg1[2] if epg1[2] else 0 + programdata = {'channelId':ChannelId, 'startTime':startTime, 'endTime':endTime, 'programName':programName, 'subprogramName':subprogramName, 'desc':desc, 'actors':actors, 'producers':producers, 'category':category, 'episode':episode, 'rebroadcast':rebroadcast, 'rating':rating} + writeProgram(programdata) # Get EPG data from SKY def GetEPGFromSKY(ChannelInfo): @@ -364,7 +426,7 @@ def GetEPGFromSKY(ChannelInfo): day = today + datetime.timedelta(days=k) params = {'area': 'in', 'inFd_channel_id': ServiceId, 'inairdate': day.strftime('%Y-%m-%d'), 'indate_type': 'now'} try: - response = requests.post(url, data=params, headers=ua, timeout=3) + response = requests.post(url, data=params, headers=ua, timeout=timeout) response.raise_for_status() json_data = response.text try: @@ -412,7 +474,7 @@ def GetEPGFromNaver(ChannelInfo): totaldate.append(day.strftime('%Y%m%d')) params = {'_callback': 'epg', 'fileKey': 'single_schedule_channel_day', 'pkid': '66', 'u1': 'single_schedule_channel_day', 'u2': ','.join(totaldate), 'u3': today.strftime('%Y%m%d'), 'u4': period, 'u5': ServiceId, 'u6': '1', 'u7': ChannelName + '편성표', 'u8': ChannelName + '편성표', 'where': 'nexearch'} try: - response = requests.get(url, params=params, headers=ua, timeout=3) + response = requests.get(url, params=params, headers=ua, timeout=timeout) response.raise_for_status() json_data = re.sub(re.compile("/\*.*?\*/",re.DOTALL ) ,"" ,response.text.split("epg(")[1].strip(");").strip()) try: @@ -444,6 +506,7 @@ def GetEPGFromNaver(ChannelInfo): except (requests.RequestException) as e: if(debug): printError(ChannelName + str(e)) else: pass + # Get EPG data from Tbroad def GetEPGFromTbroad(ChannelInfo): url='https://www.tbroad.com/chplan/selectRealTimeListForNormal.tb' @@ -510,7 +573,7 @@ def GetEPGFromMil(ChannelInfo): day = today + datetime.timedelta(days=k) params = {'program_date': day.strftime('%Y%m%d')} try: - response = requests.get(url, params=params, headers=ua, timeout=3) + response = requests.get(url, params=params, headers=ua, timeout=timeout) response.raise_for_status() json_data = response.text try: @@ -561,7 +624,7 @@ def GetEPGFromIfm(ChannelInfo): day = today + datetime.timedelta(days=k) params = {'outDay':dayofweek[(day.weekday()+1)%7], 'viewDt':day} try: - response = requests.get(url, params=params, headers=ua, timeout=3) + response = requests.get(url, params=params, headers=ua, timeout=timeout) response.raise_for_status() json_data = response.text try: @@ -608,7 +671,7 @@ def GetEPGFromKbs(ChannelInfo): for k in range(period): day = today + datetime.timedelta(days=k) try: - response = requests.get(url, params=params, headers=ua, timeout=3) + response = requests.get(url, params=params, headers=ua, timeout=timeout) response.raise_for_status() json_data = response.text try: @@ -756,7 +819,7 @@ argu2.add_argument('-o', '--outfile', metavar = default_xml_file, nargs = '?', c argu2.add_argument('-s', '--socket', metavar = default_xml_socket, nargs = '?', const = default_xml_socket, help = 'xmltv.sock(External: XMLTV)로 EPG정보 전송') argu3 = parser.add_argument_group('추가옵션') argu3.add_argument('--icon', dest = 'icon', metavar = "http://www.example.com/icon", help = '채널 아이콘 URL, 기본값: '+ default_icon_url, default = default_icon_url) -argu3.add_argument('-l', '--limit', dest = 'limit', type=int, metavar = "1-7", choices = range(1,8), help = 'EPG 정보를 가져올 기간, 기본값: '+ str(default_fetch_limit), default = default_fetch_limit) +argu3.add_argument('-l', '--limit', dest = 'limit', type=int, metavar = "1-2", choices = range(1,3), help = 'EPG 정보를 가져올 기간, 기본값: '+ str(default_fetch_limit), default = default_fetch_limit) argu3.add_argument('--rebroadcast', dest = 'rebroadcast', metavar = 'y, n', choices = 'yn', help = '제목에 재방송 정보 출력', default = default_rebroadcast) argu3.add_argument('--episode', dest = 'episode', metavar = 'y, n', choices = 'yn', help = '제목에 회차 정보 출력', default = default_episode) argu3.add_argument('--verbose', dest = 'verbose', metavar = 'y, n', choices = 'yn', help = 'EPG 정보 추가 출력', default = default_verbose) @@ -833,11 +896,12 @@ else : sys.exit() if default_fetch_limit : - if not any(str(default_fetch_limit) in s for s in ['1', '2', '3', '4', '5', '6', '7']): - printError("default_fetch_limit 는 1, 2, 3, 4, 5, 6, 7만 가능합니다.") + if not any(str(default_fetch_limit) in s for s in ['1', '2']): + printError("default_fetch_limit 는 1, 2만 가능합니다.") sys.exit() else : period = int(default_fetch_limit) + if period > 2 : period = 2 else : printError("epg2xml.json 파일의 default_fetch_limit항목이 없습니다."); sys.exit() @@ -862,3 +926,4 @@ elif output == "socket" : printError("epg2xml.json 파일의 default_xml_socket항목이 없습니다."); sys.exit() getEpg() +