Sat, 04 Nov 2017 22:34:12 +0100
added filename duplicate checker
0 | 1 | #!/usr/bin/python |
1 | 2 | # -*- coding: utf-8 -*- |
3 | # iso-8859-2 | |
2 | 4 | """ |
5 | EitSupport | |
6 | Copyright (C) 2011 betonme | |
7 | Copyright (C) 2016 Wolfgang Fahl | |
8 | Cleanup 2017 by mdd | |
9 | """ | |
0 | 10 | # This EITParser is based on: |
11 | # https://github.com/betonme/e2openplugin-EnhancedMovieCenter/blob/master/src/EitSupport.py | |
12 | # | |
13 | # In case of reuse of this source code please do not remove this copyright. | |
14 | # | |
15 | # This program is free software: you can redistribute it and/or modify | |
16 | # it under the terms of the GNU General Public License as published by | |
17 | # the Free Software Foundation, either version 3 of the License, or | |
18 | # (at your option) any later version. | |
19 | # | |
20 | # This program is distributed in the hope that it will be useful, | |
21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | # GNU General Public License for more details. | |
24 | # | |
25 | # For more information on the GNU General Public License see: | |
26 | # <http://www.gnu.org/licenses/>. | |
27 | # | |
28 | ||
2 | 29 | # seite 36, inhalt der for schleife! |
30 | # https://www.dvb.org/resources/public/standards/a38_dvb-si_specification.pdf | |
31 | ||
0 | 32 | import os |
33 | import struct | |
1 | 34 | import sys |
35 | import getopt | |
0 | 36 | |
37 | from datetime import datetime | |
38 | ||
39 | from ISO639 import LanguageCodes | |
40 | ||
41 | #def crc32(data): | |
42 | # poly = 0x4c11db7 | |
43 | # crc = 0xffffffffL | |
44 | # for byte in data: | |
45 | # byte = ord(byte) | |
46 | # for bit in range(7,-1,-1): # MSB to LSB | |
47 | # z32 = crc>>31 # top bit | |
48 | # crc = crc << 1 | |
49 | # if ((byte>>bit)&1) ^ z32: | |
50 | # crc = crc ^ poly | |
51 | # crc = crc & 0xffffffffL | |
52 | # return crc | |
53 | ||
2 | 54 | EIT_SHORT_EVENT_DESCRIPTOR = 0x4d |
55 | EIT_EXTENDED_EVENT_DESCRIPOR = 0x4e | |
56 | ||
57 | CHARSPEC_HR = { | |
1 | 58 | u'Ć': u'\u0106', u'æ': u'\u0107', u'®': u'\u017D', u'¾': u'\u017E', |
59 | u'©': u'\u0160', u'¹': u'\u0161', u'Č': u'\u010C', u'è': u'\u010D', u'ð': u'\u0111' | |
60 | } | |
0 | 61 | |
2 | 62 | CHARSPEC_CZSK = { |
1 | 63 | u'Ï'+u'C': u'Č', u'Ï'+u'E': u'Ě', u'Ï'+u'L': u'Ľ', u'Ï'+u'N': u'Ň', u'Ï'+u'R': u'Ř', |
64 | u'Ï'+u'S': u'Š', u'Ï'+u'T': u'Ť', u'Ï'+u'Z': u'Ž', u'Ï'+u'c': u'č', u'Ï'+u'd': u'ď', | |
65 | u'Ï'+u'e': u'ě', u'Ï'+u'l': u'ľ', u'Ï'+u'n': u'ň', u'Ï'+u'r': u'ř', u'Ï'+u's': u'š', | |
66 | u'Ï'+u't': u'ť', u'Ï'+u'z': u'ž', u'Ï'+u'D': u'Ď', u'Â'+u'A': u'Á', u'Â'+u'E': u'É', | |
67 | u'Â'+u'I': u'Í', u'Â'+u'O': u'Ó', u'Â'+u'U': u'Ú', u'Â'+u'a': u'á', u'Â'+u'e': u'é', | |
68 | u'Â'+u'i': u'í', u'Â'+u'o': u'ó', u'Â'+u'u': u'ú', u'Â'+u'y': u'ý', u'Ã'+u'o': u'ô', | |
69 | u'Ã'+u'O': u'Ô', u'Ê'+u'u': u'ů', u'Ê'+u'U': u'Ů', u'È'+u'A': u'Ä', u'È'+u'E': u'Ë', | |
70 | u'È'+u'I': u'Ï', u'È'+u'O': u'Ö', u'È'+u'U': u'Ü', u'È'+u'Y': u'Ÿ', u'È'+u'a': u'ä', | |
71 | u'È'+u'e': u'ë', u'È'+u'i': u'ï', u'È'+u'o': u'ö', u'È'+u'u': u'ü', u'È'+u'y': u'ÿ' | |
72 | } | |
73 | ||
2 | 74 | def convert_charspec_hr(text): |
75 | for i, j in CHARSPEC_HR.iteritems(): | |
0 | 76 | text = text.replace(i, j) |
77 | return text | |
78 | ||
2 | 79 | def convert_charspec_czsk(text): |
80 | for i, j in CHARSPEC_CZSK.iteritems(): | |
0 | 81 | text = text.replace(i, j) |
82 | return text | |
83 | ||
2 | 84 | def parse_mjd(mjd): |
1 | 85 | """Parse 16 bit unsigned int containing Modified Julian Date, |
86 | as per DVB-SI spec | |
87 | returning year,month,day""" | |
88 | year = int((mjd - 15078.2) / 365.25) | |
89 | month = int((mjd - 14956.1 - int(year * 365.25)) / 30.6001) | |
2 | 90 | day = mjd - 14956 - int(year * 365.25) - int(month * 30.6001) |
1 | 91 | correction = 0 |
92 | if month == 14 or month == 15: | |
93 | correction = 1 | |
94 | return (1900 + year + correction), (month - 1 - correction * 12), day | |
0 | 95 | |
1 | 96 | def bcd2dec(byte): |
97 | return (byte >> 4) * 10 + (byte & 0xf) | |
0 | 98 | |
2 | 99 | |
100 | def mkint(data): | |
101 | """ | |
102 | Convert string to Integer | |
103 | """ | |
104 | return int(data) if data else 0 | |
105 | ||
106 | def todate(sdate, stime): | |
107 | """ | |
108 | Convert date and time to datetime tuple | |
109 | """ | |
110 | if sdate and stime: | |
111 | try: | |
112 | return datetime( | |
113 | int(sdate[0]), int(sdate[1]), int(sdate[2]), | |
114 | int(stime[0]), int(stime[1])) | |
115 | except ValueError: | |
116 | return None | |
117 | else: | |
118 | return None | |
119 | ||
120 | def cleanstring(data): | |
121 | """remove nonprintable chars from short desc | |
122 | """ | |
123 | for char in ['\x10', '\x00', '\x02', '\x15']: | |
124 | data = data.replace(char, '') | |
125 | return data | |
126 | ||
0 | 127 | def language_iso639_2to3(alpha2): |
128 | ret = alpha2 | |
129 | if alpha2 in LanguageCodes: | |
130 | language = LanguageCodes[alpha2] | |
131 | for alpha, name in LanguageCodes.items(): | |
132 | if name == language: | |
133 | if len(alpha) == 3: | |
134 | return alpha | |
135 | return ret | |
136 | ||
2 | 137 | class EitList(object): |
0 | 138 | """Eit File support class |
139 | Description | |
140 | http://de.wikipedia.org/wiki/Event_Information_Table | |
141 | """ | |
142 | def __init__(self, path=None): | |
143 | self.eit_file = None | |
144 | ||
145 | self.eit = {} | |
146 | self.iso = None | |
147 | ||
2 | 148 | self.load(path) |
0 | 149 | |
2 | 150 | def load(self, path): |
0 | 151 | if path: |
2 | 152 | self.eit_file = path |
153 | self._read_file() | |
0 | 154 | |
2 | 155 | def get_genre(self): |
156 | return self.eit.get('genre', "") | |
0 | 157 | |
2 | 158 | def get_components(self): |
159 | return self.eit.get('components', "") | |
0 | 160 | |
2 | 161 | def get_startdate(self): |
0 | 162 | return self.eit.get('startdate', "") |
163 | ||
2 | 164 | def get_starttime(self): |
0 | 165 | return self.eit.get('starttime', "") |
166 | ||
2 | 167 | def get_duration(self): |
0 | 168 | return self.eit.get('duration', "") |
169 | ||
2 | 170 | def get_name(self): |
0 | 171 | return self.eit.get('name', "").strip() |
172 | ||
2 | 173 | def get_description(self): |
0 | 174 | return self.eit.get('description', "").strip() |
175 | ||
2 | 176 | def get_duration_seconds(self): |
0 | 177 | length = self.eit.get('duration', "") |
1 | 178 | if len(length) > 2: |
2 | 179 | return mkint((length[0] * 60 + length[1]) * 60 + length[2]) |
1 | 180 | elif len(length) > 1: |
2 | 181 | return mkint(length[0] * 60 + length[1]) |
0 | 182 | else: |
2 | 183 | return mkint(length) |
0 | 184 | |
2 | 185 | def get_date(self): |
186 | return todate(self.get_startdate(), self.get_starttime()) | |
0 | 187 | |
188 | def dumpEit(self): | |
189 | print self.eit | |
190 | ||
191 | ############################################################################## | |
192 | ## File IO Functions | |
2 | 193 | def _read_file(self): |
0 | 194 | data = "" |
195 | path = self.eit_file | |
196 | ||
2 | 197 | lang = language_iso639_2to3("de") |
0 | 198 | |
199 | if path and os.path.exists(path): | |
1 | 200 | print "Reading Event Information Table " + str(path) |
0 | 201 | |
202 | # Read data from file | |
1 | 203 | fd = None |
0 | 204 | try: |
1 | 205 | fd = open(path, 'rb') |
0 | 206 | #lines = f.readlines() |
1 | 207 | data = fd.read() |
208 | except Exception, err: | |
209 | print "[META] Exception in readEitFile: " + str(err) | |
0 | 210 | finally: |
1 | 211 | if fd is not None: |
212 | fd.close() | |
0 | 213 | |
214 | # Parse the data | |
215 | if data and 12 <= len(data): | |
216 | # go through events | |
217 | pos = 0 | |
1 | 218 | e = struct.unpack(">HHBBBBBBH", data[pos:pos + 12]) |
0 | 219 | event_id = e[0] |
2 | 220 | date = parse_mjd(e[1]) # Y, M, D |
1 | 221 | time = bcd2dec(e[2]), bcd2dec(e[3]), bcd2dec(e[4]) # HH, MM, SS |
222 | duration = bcd2dec(e[5]), bcd2dec(e[6]), bcd2dec(e[7]) # HH, MM, SS | |
2 | 223 | #running_status = (e[8] & 0xe000) >> 13 |
224 | #free_CA_mode = e[8] & 0x1000 | |
0 | 225 | descriptors_len = e[8] & 0x0fff |
226 | ||
2 | 227 | #if running_status in [1, 2]: |
228 | # self.eit['when'] = "NEXT" | |
229 | #elif running_status in [3, 4]: | |
230 | # self.eit['when'] = "NOW" | |
0 | 231 | |
232 | self.eit['startdate'] = date | |
233 | self.eit['starttime'] = time | |
234 | self.eit['duration'] = duration | |
235 | ||
236 | pos = pos + 12 | |
237 | short_event_descriptor = [] | |
238 | short_event_descriptor_multi = [] | |
239 | extended_event_descriptor = [] | |
240 | extended_event_descriptor_multi = [] | |
241 | component_descriptor = [] | |
242 | content_descriptor = [] | |
243 | linkage_descriptor = [] | |
244 | parental_rating_descriptor = [] | |
245 | endpos = len(data) - 1 | |
246 | while pos < endpos: | |
247 | rec = ord(data[pos]) | |
1 | 248 | length = ord(data[pos + 1]) + 2 |
0 | 249 | if rec == 0x4D: |
1 | 250 | descriptor_tag = ord(data[pos + 1]) |
251 | descriptor_length = ord(data[pos + 2]) | |
252 | ISO_639_language_code = str(data[pos + 3:pos + 5]) | |
253 | event_name_length = ord(data[pos + 5]) | |
2 | 254 | short_event_description = cleanstring(data[pos + 6:pos + 6 + event_name_length]) |
255 | ||
256 | tmp_length = ord(data[pos + 6 + event_name_length]) | |
257 | self.eit['genre'] = cleanstring(data[pos + 7 + event_name_length:pos + 7 + tmp_length + event_name_length]) | |
258 | ||
0 | 259 | if ISO_639_language_code == lang: |
260 | short_event_descriptor.append(short_event_description) | |
261 | short_event_descriptor_multi.append(short_event_description) | |
262 | elif rec == 0x4E: | |
1 | 263 | ISO_639_language_code = str(data[pos + 3:pos + 5]) |
0 | 264 | extended_event_description = "" |
265 | extended_event_description_multi = "" | |
2 | 266 | for i in range(pos + 8, pos + length): |
267 | if str(ord(data[i])) == "138": | |
0 | 268 | extended_event_description += '\n' |
269 | extended_event_description_multi += '\n' | |
2 | 270 | elif data[i] not in ['\x10', '\x00', '\x02', '\x15']: |
271 | extended_event_description += data[i] | |
272 | extended_event_description_multi += data[i] | |
0 | 273 | if ISO_639_language_code == lang: |
274 | extended_event_descriptor.append(extended_event_description) | |
275 | extended_event_descriptor_multi.append(extended_event_description) | |
276 | elif rec == 0x50: | |
2 | 277 | #tmp_type = ord(data[pos + 3:pos + 4]) |
278 | #print "type: %x" % tmp_type | |
279 | component_descriptor.append(cleanstring(data[pos + 8:pos + length])) | |
0 | 280 | elif rec == 0x54: |
2 | 281 | content_descriptor.append(cleanstring(data[pos + 8:pos + length])) |
0 | 282 | elif rec == 0x4A: |
2 | 283 | linkage_descriptor.append(cleanstring(data[pos + 8:pos + length])) |
0 | 284 | elif rec == 0x55: |
2 | 285 | parental_rating_descriptor.append(cleanstring(data[pos + 2:pos + length])) |
0 | 286 | else: |
1 | 287 | print "unsupported descriptor: %x %x" % (rec, pos + 12) |
2 | 288 | print data[pos:pos + length] |
289 | ||
0 | 290 | pos += length |
291 | ||
2 | 292 | self.eit['components'] = ", ".join(component_descriptor) |
293 | ||
294 | ||
295 | ||
1 | 296 | # Very bad but there can be both encodings |
297 | # User files can be in cp1252 | |
298 | # Is there no other way? | |
299 | if short_event_descriptor: | |
300 | short_event_descriptor = "".join(short_event_descriptor) | |
301 | else: | |
302 | short_event_descriptor = "".join(short_event_descriptor_multi) | |
303 | if short_event_descriptor: | |
304 | #try: | |
305 | # short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8") | |
306 | #except UnicodeDecodeError: | |
307 | # pass | |
308 | try: | |
309 | short_event_descriptor.decode('utf-8') | |
310 | except UnicodeDecodeError: | |
0 | 311 | try: |
1 | 312 | short_event_descriptor = short_event_descriptor.decode("cp1252").encode("utf-8") |
0 | 313 | except UnicodeDecodeError: |
1 | 314 | # do nothing, otherwise cyrillic wont properly displayed |
315 | #short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8") | |
316 | pass | |
317 | if (lang == "cs") or (lang == "sk"): | |
2 | 318 | short_event_descriptor = str(convert_charspec_czsk(short_event_descriptor)) |
1 | 319 | if lang == "hr": |
2 | 320 | short_event_descriptor = str(convert_charspec_hr(short_event_descriptor)) |
1 | 321 | self.eit['name'] = short_event_descriptor |
0 | 322 | |
1 | 323 | # Very bad but there can be both encodings |
324 | # User files can be in cp1252 | |
325 | # Is there no other way? | |
326 | if extended_event_descriptor: | |
327 | extended_event_descriptor = "".join(extended_event_descriptor) | |
328 | else: | |
329 | extended_event_descriptor = "".join(extended_event_descriptor_multi) | |
330 | if extended_event_descriptor: | |
331 | #try: | |
332 | # extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8") | |
333 | #except UnicodeDecodeError: | |
334 | # pass | |
335 | try: | |
336 | extended_event_descriptor.decode('utf-8') | |
337 | except UnicodeDecodeError: | |
0 | 338 | try: |
1 | 339 | extended_event_descriptor = extended_event_descriptor.decode("cp1252").encode("utf-8") |
0 | 340 | except UnicodeDecodeError: |
1 | 341 | # do nothing, otherwise cyrillic wont properly displayed |
342 | #extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8") | |
343 | pass | |
344 | if (lang == "cs") or (lang == "sk"): | |
2 | 345 | extended_event_descriptor = str(convert_charspec_czsk(extended_event_descriptor)) |
1 | 346 | if lang == "hr": |
2 | 347 | extended_event_descriptor = str(convert_charspec_hr(extended_event_descriptor)) |
1 | 348 | self.eit['description'] = extended_event_descriptor |
0 | 349 | |
1 | 350 | else: |
351 | # No data clear all | |
352 | self.eit = {} | |
0 | 353 | |
354 | ||
355 | def readeit(eitfile): | |
1 | 356 | """Module docstring. |
357 | Read Eit File and show the information. | |
358 | """ | |
359 | eitlist = EitList(eitfile) | |
2 | 360 | print "Name: ", eitlist.get_name() |
361 | print "Genre: ", eitlist.get_genre() | |
362 | print "Components: ", eitlist.get_components() | |
363 | print "StartDate: ", eitlist.get_date() | |
364 | print "Description: ", eitlist.get_description() | |
365 | print "Duration: ", eitlist.get_duration() | |
366 | print "Minutes: ", eitlist.get_duration_seconds() / 60 | |
0 | 367 | |
368 | #eitlist.dumpEit() | |
369 | ||
370 | def main(): | |
371 | # parse command line options | |
372 | try: | |
373 | opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) | |
374 | except getopt.error, msg: | |
375 | print msg | |
376 | print "for help use --help" | |
377 | sys.exit(2) | |
378 | # process options | |
379 | for o, a in opts: | |
380 | if o in ("-h", "--help"): | |
381 | print __doc__ | |
382 | sys.exit(0) | |
383 | # process arguments | |
384 | for arg in args: | |
385 | readeit(arg) # process() is defined elsewhere | |
386 | ||
387 | if __name__ == "__main__": | |
388 | main() | |
389 |