forked from ortegaangelo/epgData2XMLTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
504 lines (427 loc) · 19.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import datetime
import os
from optparse import OptionParser
import glob
import http.client
import urllib
import zipfile
from io import BytesIO
from xml.dom.minidom import getDOMImplementation
from xml.etree import cElementTree
import re
import time
import logging
import traceback
import tvdb_api
def log():
return logging.getLogger("epgData2XMLTV")
class App:
INPUT_PATH = 'epgdata_files'
OUTPUT_PATH = 'output'
APIKEY = '################'
PIN = '######################'
DAY = -1
URL = 'www.epgdata.com'
DATEFORMAT = '%Y%m%d'
timeoffset ="+0000"
channel_ids = []
genre_map = {}
category_map = {}
def __init__(self):
parser = OptionParser()
parser.add_option("-i", "--input", help="The path for the input files.")
parser.add_option("-o", "--output", help="The path for the output files.")
parser.add_option("-k", "--key", help="api key for thetvdb.com")
parser.add_option("-p", "--pin", help="The pin code for epgdata.com")
parser.add_option("-d", "--day", help="The day to retrieve from epgdata.com")
parser.add_option("-v", "--debug", action="store_true", dest="debug", help="Enable debug output")
(options, args) = parser.parse_args()
if options.input is not None:
self.INPUT_PATH = options.input
if options.output is not None:
self.OUTPUT_PATH = options.output
if options.key is not None:
self.APIKEY = options.key
if options.pin is not None:
self.PIN = options.pin
if options.day is not None:
self.DAY = int(options.day)
if options.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
self.t = tvdb_api.Tvdb(apikey=self.APIKEY)
self.t.config['language'] = 'de'
# evaluate timezone
localtime = time.localtime()
if localtime.tm_isdst:
self.timeoffset = time.strftime("+%H%M", time.gmtime(-time.altzone))
else:
self.timeoffset = time.strftime("+%H%M", time.gmtime(-time.timezone))
# Clean old files
self.cleanup()
# Fetch include.zip
self.fetch_include()
# Fetch new files
if self.DAY != -1:
self.fetch_data(self.DAY)
else:
self.DAY = 0
while self.DAY <= 6:
self.fetch_data(self.DAY)
self.DAY += 1
# Generated Merged File
self.generate_merged()
def cleanup(self):
log().debug('cleanup')
files = os.listdir(self.INPUT_PATH)
for filename in files:
try:
splitted = filename.split('_')
file_date = datetime.datetime.strptime(splitted[0], self.DATEFORMAT)
date = (datetime.datetime.now() + datetime.timedelta(days=-1))
if file_date < date:
os.remove('{}/{}'.format(self.INPUT_PATH, filename))
log().info('{} deleted.'.format(filename))
except Exception:
log().error('{} failed to delete.'.format(filename))
def fetch_include(self):
# Fetch include
params = urllib.parse.urlencode(
{'action': 'sendInclude', 'iOEM': 'vdr', 'pin': self.PIN, 'dataType': 'xml'})
headers = {'Content-type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache'}
conn = http.client.HTTPConnection("www.epgdata.com")
conn.request('GET', '/index.php?{}'.format(params), None, headers)
response = conn.getresponse()
content_type = response.getheader('content-type')
log().info(params)
log().info(content_type)
log().info(response.status)
if response.status == 200 and content_type == 'application/x-zip-compressed':
# Content
content = response.read()
# Uncompress ZIP and save XML
xml_zip = zipfile.ZipFile(BytesIO(content))
for name in xml_zip.namelist():
uncompressed = xml_zip.read(name)
output_filename = '{}/{}'.format(self.INPUT_PATH, name)
output = open(output_filename, 'wb')
output.write(uncompressed)
output.close()
def fetch_data(self, day):
# Exists
date = (datetime.datetime.now() + datetime.timedelta(days=day))
files = os.listdir(self.INPUT_PATH)
for filename in files:
if filename.startswith(date.strftime(self.DATEFORMAT)):
log().info('Already fetched.')
return
# Fetch data
params = urllib.parse.urlencode(
{'action': 'sendPackage', 'iOEM': 'vdr', 'dayOffset': self.DAY, 'pin': self.PIN, 'dataType': 'xml'})
headers = {'Content-type': 'application/x-www-form-urlencoded', 'Cache-Control': 'no-cache'}
conn = http.client.HTTPConnection("www.epgdata.com")
conn.request('GET', '/index.php?{}'.format(params), None, headers)
response = conn.getresponse()
content_type = response.getheader('content-type')
log().info(params)
log().info(content_type)
log().info(response.status)
if response.status == 200 and content_type == 'application/x-zip-compressed':
# Content
content = response.read()
# Uncompress ZIP and save XML
xml_zip = zipfile.ZipFile(BytesIO(content))
for name in xml_zip.namelist():
uncompressed = xml_zip.read(name)
output_filename = '{}/{}'.format(self.INPUT_PATH, name)
output = open(output_filename, 'wb')
output.write(uncompressed)
output.close()
def generate_merged(self):
log().debug('generate_merged')
# New xml
impl = getDOMImplementation()
new_doc = impl.createDocument(None, "tv", None)
top_element = new_doc.documentElement
top_element.setAttribute("generator-info-name", 'EPGData2XMLTV')
top_element.setAttribute("generator-info-url", 'http://github.com/ortegaangelo/EPGData2XMLTV')
# Categories '{}/{}'.format(self.INPUT_PATH, 'category.xml')
self.parse_categories('{}/{}'.format(self.INPUT_PATH, 'category.xml'))
# Genres
self.parse_genres('{}/{}'.format(self.INPUT_PATH, 'genre.xml'))
# Channels
self.generate_channel_data(top_element, '{}/{}'.format(self.INPUT_PATH, 'channel_y.xml'))
# Program data
files = glob.glob(self.INPUT_PATH + "/*.xml")
for xml_file in files:
top_element = self.generate_program_data(top_element, xml_file)
# Write to file
xml = ['<?xml version="1.0" encoding="utf-8" standalone="yes"?>', top_element.toprettyxml()]
file_handle = open(self.OUTPUT_PATH + "/epg.xml", "w")
file_handle.write(''.join(xml))
file_handle.close()
def parse_categories(self, epg_path):
log().debug('parse_categories: ' +epg_path)
context = cElementTree.iterparse(epg_path, events=("start", "end",))
context = iter(context)
event, root = next(context)
for event, elem in context:
if event == "end" and elem.tag == 'data':
key = elem.findtext('ca0')
value = elem.findtext('ca1')
self.category_map[key] = value
root.clear()
def parse_genres(self, epg_path):
log().debug('parse_genres: ' +epg_path)
context = cElementTree.iterparse(epg_path, events=("start", "end",))
context = iter(context)
event, root = next(context)
for event, elem in context:
if event == "end" and elem.tag == 'data':
key = elem.findtext('g0')
value = elem.findtext('g1')
self.genre_map[key] = value
root.clear()
def generate_channel_data(self, parent, channel_path):
log().debug('generate_channel_data: ' +channel_path)
context = cElementTree.iterparse(channel_path, events=("start", "end",))
context = iter(context)
event, root = next(context)
for event, elem in context:
if event == "end" and elem.tag == 'data':
child = self.generate_channel_element(elem)
if child is not None:
parent.appendChild(child)
root.clear()
def generate_channel_element(self, elem):
# Read channel filter
with open("channelfilter") as f:
channel_filter = f.read().splitlines()
tvchannel_id = elem.findtext('ch4')
tvchannel_name = elem.findtext('ch0')
if tvchannel_name not in channel_filter:
return None
log().debug('generate_channel_element: ' +tvchannel_name)
impl = getDOMImplementation()
new_doc = impl.createDocument(None, "channel", None)
top_element = new_doc.documentElement
top_element.setAttribute("id", tvchannel_id)
if len(tvchannel_name)>0:
display_name_node = new_doc.createElement("display-name")
display_name_node.setAttribute("lang", 'de')
display_name = new_doc.createTextNode(tvchannel_name)
display_name_node.appendChild(display_name)
top_element.appendChild(display_name_node)
self.channel_ids.append(tvchannel_id)
return top_element
def generate_program_data(self, parent, epg_path):
log().debug('generate_program_data: ' +epg_path)
context = cElementTree.iterparse(epg_path, events=("start", "end",))
context = iter(context)
event, root = next(context)
for event, elem in context:
if event == "end" and elem.tag == 'data':
child = self.generate_program_element(elem)
if child is not None:
parent.appendChild(child)
root.clear()
return parent
def generate_program_element(self, elem):
tvchannel_id = elem.findtext('d2')
if tvchannel_id not in self.channel_ids:
return None
#broadcast_id = elem.findtext('d0')
starttime = elem.findtext('d4')
endtime = elem.findtext('d5')
tvshow_length = elem.findtext('d7')
primetime = elem.findtext('d9')
category_id = elem.findtext('d10')
age_marker = elem.findtext('d16')
title = elem.findtext('d19')
subtitle = elem.findtext('d20')
comment_long = elem.findtext('d21')
genreid = elem.findtext('d25')
sequence = elem.findtext('d26')
tvd_total_value = elem.findtext('d30')
country = elem.findtext('d32')
year = elem.findtext('d33')
moderator = elem.findtext('d34')
studio_guest = elem.findtext('d35')
regisseur = elem.findtext('d36')
actor = elem.findtext('d37')
image_big = elem.findtext('d40')
log().debug('generate_program_element: ' +title)
impl = getDOMImplementation()
new_doc = impl.createDocument(None, "programme", None)
top_element = new_doc.documentElement
top_element.setAttribute("channel", tvchannel_id)
top_element.setAttribute("start", datetime.datetime.strptime(starttime, '%Y-%m-%d %H:%M:%S')
.strftime('%Y%m%d%H%M%S '+self.timeoffset))
top_element.setAttribute("stop", datetime.datetime.strptime(endtime, '%Y-%m-%d %H:%M:%S')
.strftime('%Y%m%d%H%M%S '+self.timeoffset))
if len(title)>0:
title_node = new_doc.createElement("title")
title_node.setAttribute("lang", 'de')
title_text = new_doc.createTextNode(title)
title_node.appendChild(title_text)
top_element.appendChild(title_node)
if len(subtitle)>0:
subtitle_node = new_doc.createElement("sub-title")
subtitle_node.setAttribute("lang", 'de')
subtitle_text = new_doc.createTextNode(subtitle)
subtitle_node.appendChild(subtitle_text)
top_element.appendChild(subtitle_node)
if len(comment_long)>0:
desc_node = new_doc.createElement("desc")
desc_node.setAttribute("lang", 'de')
desc_text = new_doc.createTextNode(comment_long)
desc_node.appendChild(desc_text)
top_element.appendChild(desc_node)
director_list = []
if len(regisseur)>0:
parts = regisseur.split('|')
for name in parts:
director_list.append(name)
actor_list = []
if len(actor)>0:
parts = actor.split(') - ')
for name in parts:
actor_list.append(name+")")
presenter_list = []
if len(moderator)>0:
parts = moderator.split('|')
for name in parts:
presenter_list.append(name)
guest_list = []
if len(studio_guest)>0:
parts = studio_guest.split('|')
for name in parts:
guest_list.append(name)
if len(director_list)>0 or len(actor_list)>0 or len(presenter_list)>0 or len(guest_list)>0:
credits_node = new_doc.createElement("credits")
for name in director_list:
director_node = new_doc.createElement("director")
director_text = new_doc.createTextNode(name)
director_node.appendChild(director_text)
credits_node.appendChild(director_node)
for name in actor_list:
actor_node = new_doc.createElement("actor")
actor_name = re.findall(r"(.*?)\(.*?\)", name)
#actor_role = re.findall(r"(?<=\().*?(?=\))", name)
if len(actor_name)>0:
if len(actor_name[0].strip())>0:
actor_text = new_doc.createTextNode(actor_name[0].strip())
actor_node.appendChild(actor_text)
else:
actor_text = new_doc.createTextNode(name)
actor_node.appendChild(actor_text)
#if actor_role and len(actor_role[0])>0:
# actor_node.setAttribute("role", actor_role[0])
if actor_node.hasChildNodes():
credits_node.appendChild(actor_node)
for name in presenter_list:
presenter_node = new_doc.createElement("presenter")
presenter_text = new_doc.createTextNode(name)
presenter_node.appendChild(presenter_text)
credits_node.appendChild(presenter_node)
for name in guest_list:
guest_node = new_doc.createElement("guest")
guest_text = new_doc.createTextNode(name)
guest_node.appendChild(guest_text)
credits_node.appendChild(guest_node)
top_element.appendChild(credits_node)
if self.category_map.__contains__(category_id):
category_node = new_doc.createElement("category")
category_node.setAttribute("lang", 'de')
category_text = new_doc.createTextNode(self.category_map[category_id] )
category_node.appendChild(category_text)
top_element.appendChild(category_node)
if self.genre_map.__contains__(genreid):
category_node = new_doc.createElement("category")
category_node.setAttribute("lang", 'de')
category_text = new_doc.createTextNode(self.genre_map[genreid] )
category_node.appendChild(category_text)
top_element.appendChild(category_node)
# if len(broadcast_id)>0:
# category_node = new_doc.createElement("category")
# category_node.setAttribute("lang", 'de')
# category_text = new_doc.createTextNode(broadcast_id)
# category_node.appendChild(category_text)
# top_element.appendChild(category_node)
if len(tvshow_length)>0:
length_node = new_doc.createElement("length")
length_node.setAttribute("units", 'minutes')
length_text = new_doc.createTextNode(tvshow_length)
length_node.appendChild(length_text)
top_element.appendChild(length_node)
#if int(sequence) is not 0:
# episode_node = new_doc.createElement("episode-num")
# episode_node.setAttribute("system","onscreen")
# episode_text = new_doc.createTextNode(sequence)
# episode_node.appendChild(episode_text)
# top_element.appendChild(episode_node)
if int(sequence) != 0:
episode_node = new_doc.createElement("episode-num")
episode_node.setAttribute("system","xmltv_ns")
season = ""
episodeNumber = str(int(sequence)-1)
if len(subtitle)>0:
try:
seriesName = title.split(' - ')
episodeName = subtitle.split(' / ')
episode_tvdb = self.t[seriesName[0]].search(episodeName[0], key='episodeName')
if len(episode_tvdb)>0:
season = str(episode_tvdb[0]['airedSeason']-1)
episodeNumber = str(episode_tvdb[0]['airedEpisodeNumber']-1)
log().debug('episode: ' +title + '.' + subtitle + '.' + season + '.' + episodeNumber)
except tvdb_api.tvdb_shownotfound:
log().info("Could not find episode for: " + title + '.' + subtitle)
except tvdb_api.tvdb_error:
log().error("tvdb error: " + title + '.' + subtitle)
except KeyError:
log().error("Mapping key not found: " + title + '.' + subtitle)
except:
log().error("Unexpected error:")
log().error(traceback.format_exc())
episode_text = new_doc.createTextNode(season+"."+episodeNumber+".")
episode_node.appendChild(episode_text)
top_element.appendChild(episode_node)
if len(country)>0:
parts = country.split('|')
for c in parts:
country_node = new_doc.createElement("country")
country_text = new_doc.createTextNode(c)
country_node.appendChild(country_text)
top_element.appendChild(country_node)
if len(year)>0:
year_node = new_doc.createElement("date")
year_text = new_doc.createTextNode(year)
year_node.appendChild(year_text)
top_element.appendChild(year_node)
if len(image_big)>0:
src_node = new_doc.createElement("icon")
src_node.setAttribute("src", image_big)
top_element.appendChild(src_node)
if len(age_marker)>0:
rating_node = new_doc.createElement("rating")
value_node = new_doc.createElement("value")
rating_text = new_doc.createTextNode(age_marker)
value_node.appendChild(rating_text)
rating_node.appendChild(value_node)
top_element.appendChild(rating_node)
if int(tvd_total_value)>0:
star_rating_node = new_doc.createElement("star-rating")
value_node = new_doc.createElement("value")
rating = int(tvd_total_value)-1
star_rating_text = new_doc.createTextNode(str(rating) + "/4")
value_node.appendChild(star_rating_text)
star_rating_node.appendChild(value_node)
top_element.appendChild(star_rating_node)
if int(primetime)==1:
premiere_node = new_doc.createElement("premiere")
premiere_text = new_doc.createTextNode("Premiere")
premiere_node.appendChild(premiere_text)
top_element.appendChild(premiere_node)
return top_element
App()