From 75d20095d095ac12360c8e90f3cc0b78647a9ab6 Mon Sep 17 00:00:00 2001
From: wonipapa <sungpyohong@gmail.com>
Date: Tue, 11 Apr 2017 14:32:11 +0900
Subject: [PATCH] Update epg2xml.py

---
 epg2xml.py | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/epg2xml.py b/epg2xml.py
index 7476965..184aa97 100644
--- a/epg2xml.py
+++ b/epg2xml.py
@@ -46,7 +46,7 @@ def getEpg():
 
     print('<?xml version="1.0" encoding="UTF-8"?>')
     print('<!DOCTYPE tv SYSTEM "xmltv.dtd">\n')
-    print('<tv generator-info-name="epg2xml.py ' + __version__ + '">')
+    print('<tv generator-info-name="epg2xml ' + __version__ + '">')
 
     for Channeldata in Channeldatas: #Get Channel & Print Channel info
         if Channeldata['Enabled'] == 1:
@@ -133,8 +133,10 @@ def GetEPGFromEPG(ChannelInfo):
                             grade = re.match('.*schedule_([\d,]+)?.*',image)
                             if not (grade is None): rating = int(grade.group(1))
                             else : rating = 0
+                            programName = matches.group(2).strip() if matches.group(2) else ''
+                            subprogramName = matches.group(4).strip() if matches.group(4) else ''
                             #programName, startTime, rating, subprogramName, rebroadcast, episode
-                            epginfo.append([matches.group(2), startTime, rating, matches.group(4), matches.group(5), matches.group(7)])
+                            epginfo.append([programName, startTime, rating, subprogramName, matches.group(5), matches.group(7)])
 
             for epg1, epg2 in zip(epginfo, epginfo[1:]):
                 programName = epg1[0] if epg1[0] else ''
@@ -226,11 +228,14 @@ def GetEPGFromLG(ChannelInfo):
             data = data.replace('<재>', '&lt;재&gt;')
             strainer = SoupStrainer('table')
             soup = BeautifulSoup(data, 'lxml', parse_only=strainer, from_encoding='utf-8')
-            html = soup.find('table', {'class':'datatable06'}).tbody.find_all('tr') if soup.find('table', {'class':'datatable06'}) else ''
+            html = soup.find('table').tbody.find_all('tr') if soup.find('table') else ''
             if(html):
                 for row in html:
                     for cell in [row.find_all('td')]:
-                        epginfo.append([cell[1].text.strip(), str(day) + ' ' + cell[0].text, cell[2].text.strip(), cell[1].find('img', alt=True)['alt'].strip()])
+                        rating = 0 if cell[1].find('span', {'class': 'tag cte_all'}).text.strip()=="All" else int(cell[1].find('span', {'class': 'tag cte_all'}).text.strip())
+                        cell[1].find('span', {'class': 'tagGroup'}).decompose()
+                        epginfo.append([cell[1].text.strip(), str(day) + ' ' + cell[0].text, cell[2].text.strip(), rating])
+                        #cell[1].find('img', alt=True)['alt'].strip()])
                 for epg1, epg2 in zip(epginfo, epginfo[1:]):
                     programName = ''
                     subprogramName = ''
@@ -250,9 +255,6 @@ def GetEPGFromLG(ChannelInfo):
                     desc = ''
                     actors = ''
                     producers = ''
-                    rating = 0
-                    matches = re.match('(\d+)세이상 관람가', epg1[3].encode('utf-8'))
-                    if not(matches is None): rating = int(matches.group(1))
                     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)
             else:
@@ -291,6 +293,8 @@ def GetEPGFromSK(ChannelInfo):
                         programName = matches.group(1).strip() if matches.group(1) else ''
                         subprogramName = matches.group(3).strip() if matches.group(3) else ''
                         episode = matches.group(2).replace('회', '') if matches.group(2) else ''
+                        episode = '' if episode== '0' else episode
+#                            printError(episode)
                         rebroadcast = True if matches.group(5) else False
                     startTime = datetime.datetime.fromtimestamp(int(program['startTime'])/1000)
                     startTime = startTime.strftime('%Y%m%d%H%M%S')
@@ -338,7 +342,10 @@ def GetEPGFromSKY(ChannelInfo):
                     else: pass
                 else :
                     programs = data['scheduleListIn']
-                    for program  in {v['starttime']:v for v in programs}.values():
+#                    for v in programs :
+#                        printError(str(ChannelId) + ' ' + str(v['starttime']) + ' ' + str(v['endtime'])+ ' ' + v['program_name'])
+#                    for program  in {v['starttime']:v for v in programs}.values():
+                    for program in programs :
                         programName = unescape(program['program_name']).replace('lt;','<').replace('gt;','>').replace('amp;','&') if program['program_name'] else ''
                         subprogramName = unescape(program['program_subname']).replace('lt;','<').replace('gt;','>').replace('amp;','&') if program['program_subname'] else ''
                         startTime = program['starttime']
@@ -447,7 +454,7 @@ def writeProgram(programdata):
     else:
         desc =''
     if programdata['desc'] : desc = desc + '\n' + escape(programdata['desc'])
-    contentTypeDict={'교양':'Arts / Culture (without music)', '만화':'Cartoons / Puppets', '교육':'Education / Science / Factual topics', '취미':'Leisure hobbies', '드라마':'Movie / Drama', '영화':'Movie / Drama', '음악':'Music / Ballet / Dance', '뉴스':'News / Current affairs', '다큐':'Documentary', '시사/다큐':'Documentary', '연예':'Show / Game show', '스포츠':'Sports', '홈쇼핑':'Advertisement / Shopping'}
+    contentTypeDict={'교양':'Arts / Culture (without music)', '만화':'Cartoons / Puppets', '교육':'Education / Science / Factual topics', '취미':'Leisure hobbies', '드라마':'Movie / Drama', '영화':'Movie / Drama', '음악':'Music / Ballet / Dance', '뉴스':'News / Current affairs', '다큐':'Documentary', '라이프':'Documentary', '시사/다큐':'Documentary', '연예':'Show / Game show', '스포츠':'Sports', '홈쇼핑':'Advertisement / Shopping'}
     contentType = ''
     for key, value in contentTypeDict.iteritems():
         if category.startswith(key):
@@ -507,6 +514,7 @@ except ValueError:
     printError("epg2xml." + JSON_SYNTAX_ERROR)
     sys.exit()
 
+
 parser = argparse.ArgumentParser(description = 'EPG 정보를 출력하는 방법을 선택한다')
 argu1 = parser.add_argument_group(description = 'IPTV 선택')
 argu1.add_argument('-i', dest = 'MyISP', choices = ['KT', 'LG', 'SK'], help = '사용하는 IPTV : KT, LG, SK', default = MyISP)