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