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