--- a/eit.py Sat Nov 04 20:12:11 2017 +0100 +++ b/eit.py Sat Nov 04 20:56:03 2017 +0100 @@ -1,6 +1,6 @@ #!/usr/bin/python -# -*- coding: iso-8859-2 -*- -# encoding: utf-8 +# -*- coding: utf-8 -*- +# iso-8859-2 # # EitSupport # Copyright (C) 2011 betonme @@ -27,18 +27,13 @@ import os import struct -import time +import sys +import getopt from datetime import datetime from ISO639 import LanguageCodes -#from Components.config import config -#from Components.Language import language -#from EMCTasker import emcDebugOut -#from IsoFileSupport import IsoSupport -#from MetaSupport import getInfoFile - #def crc32(data): # poly = 0x4c11db7 # crc = 0xffffffffL @@ -52,12 +47,25 @@ # crc = crc & 0xffffffffL # return crc -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'} +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' +} -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'ň', -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'ó', -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'ë', -u'È'+u'i': u'ï',u'È'+u'o': u'ö',u'È'+u'u': u'ü',u'È'+u'y': u'ÿ'} +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'ň', 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'ó', 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'ë', u'È'+u'i': u'ï', u'È'+u'o': u'ö', u'È'+u'u': u'ü', u'È'+u'y': u'ÿ' +} + +def emcDebugOut(msg): + print msg def convertCharSpecHR(text): for i, j in decoding_charSpecHR.iteritems(): @@ -69,20 +77,20 @@ text = text.replace(i, j) return text -def parseMJD(MJD): - # Parse 16 bit unsigned int containing Modified Julian Date, - # as per DVB-SI spec - # returning year,month,day - YY = int( (MJD - 15078.2) / 365.25 ) - MM = int( (MJD - 14956.1 - int(YY*365.25) ) / 30.6001 ) - D = MJD - 14956 - int(YY*365.25) - int(MM * 30.6001) - K=0 - if MM == 14 or MM == 15: K=1 +def parseMJD(mjd): + """Parse 16 bit unsigned int containing Modified Julian Date, + as per DVB-SI spec + returning year,month,day""" + year = int((mjd - 15078.2) / 365.25) + month = int((mjd - 14956.1 - int(year * 365.25)) / 30.6001) + day = mjd - 14956 - int(year * 365.25) - int(month * 30.6001) + correction = 0 + if month == 14 or month == 15: + correction = 1 + return (1900 + year + correction), (month - 1 - correction * 12), day - return (1900 + YY+K), (MM-1-K*12), D - -def unBCD(byte): - return (byte>>4)*10 + (byte & 0xf) +def bcd2dec(byte): + return (byte >> 4) * 10 + (byte & 0xf) def language_iso639_2to3(alpha2): ret = alpha2 @@ -94,14 +102,13 @@ return alpha return ret - class EitList(): """Eit File support class Description http://de.wikipedia.org/wiki/Event_Information_Table """ - EIT_SHORT_EVENT_DESCRIPTOR = 0x4d - EIT_EXTENDED_EVENT_DESCRIPOR = 0x4e + EIT_SHORT_EVENT_DESCRIPTOR = 0x4d + EIT_EXTENDED_EVENT_DESCRIPOR = 0x4e def __init__(self, path=None): self.eit_file = None @@ -124,7 +131,9 @@ def __toDate(self, d, t): if d and t: try: - return datetime(int(d[0]), int(d[1]), int(d[2]), int(t[0]), int(t[1])) + return datetime( + int(d[0]), int(d[1]), int(d[2]), + int(t[0]), int(t[1])) except ValueError: return None else: @@ -163,10 +172,10 @@ def getEitLengthInSeconds(self): length = self.eit.get('duration', "") - if len(length)>2: - return self.__mk_int((length[0]*60 + length[1])*60 + length[2]) - elif len(length)>1: - return self.__mk_int(length[0]*60 + length[1]) + if len(length) > 2: + return self.__mk_int((length[0] * 60 + length[1]) * 60 + length[2]) + elif len(length) > 1: + return self.__mk_int(length[0] * 60 + length[1]) else: return self.__mk_int(length) @@ -182,43 +191,39 @@ data = "" path = self.eit_file - #lang = language.getLanguage()[:2] lang = language_iso639_2to3( "de" ) - #print lang + str(path) if path and os.path.exists(path): - #print "Reading Event Information Table " + str(path) + print "Reading Event Information Table " + str(path) # Read data from file - # OE1.6 with Pyton 2.6 - #with open(self.eit_file, 'r') as file: lines = file.readlines() - f = None + fd = None try: - f = open(path, 'rb') + fd = open(path, 'rb') #lines = f.readlines() - data = f.read() - except Exception, e: - emcDebugOut("[META] Exception in readEitFile: " + str(e)) + data = fd.read() + except Exception, err: + print "[META] Exception in readEitFile: " + str(err) finally: - if f is not None: - f.close() + if fd is not None: + fd.close() # Parse the data if data and 12 <= len(data): # go through events pos = 0 - e = struct.unpack(">HHBBBBBBH", data[pos:pos+12]) + e = struct.unpack(">HHBBBBBBH", data[pos:pos + 12]) event_id = e[0] - date = parseMJD(e[1]) # Y, M, D - time = unBCD(e[2]), unBCD(e[3]), unBCD(e[4]) # HH, MM, SS - duration = unBCD(e[5]), unBCD(e[6]), unBCD(e[7]) # HH, MM, SS - running_status = (e[8] & 0xe000) >> 13 - free_CA_mode = e[8] & 0x1000 + date = parseMJD(e[1]) # Y, M, D + time = bcd2dec(e[2]), bcd2dec(e[3]), bcd2dec(e[4]) # HH, MM, SS + duration = bcd2dec(e[5]), bcd2dec(e[6]), bcd2dec(e[7]) # HH, MM, SS + running_status = (e[8] & 0xe000) >> 13 + free_CA_mode = e[8] & 0x1000 descriptors_len = e[8] & 0x0fff - if running_status in [1,2]: + if running_status in [1, 2]: self.eit['when'] = "NEXT" - elif running_status in [3,4]: + elif running_status in [3, 4]: self.eit['when'] = "NOW" self.eit['startdate'] = date @@ -237,18 +242,18 @@ endpos = len(data) - 1 while pos < endpos: rec = ord(data[pos]) - length = ord(data[pos+1]) + 2 + length = ord(data[pos + 1]) + 2 if rec == 0x4D: - descriptor_tag = ord(data[pos+1]) - descriptor_length = ord(data[pos+2]) - ISO_639_language_code = str(data[pos+3:pos+5]) - event_name_length = ord(data[pos+5]) - short_event_description = data[pos+6:pos+6+event_name_length] + descriptor_tag = ord(data[pos + 1]) + descriptor_length = ord(data[pos + 2]) + ISO_639_language_code = str(data[pos + 3:pos + 5]) + event_name_length = ord(data[pos + 5]) + short_event_description = data[pos + 6:pos + 6 + event_name_length] if ISO_639_language_code == lang: short_event_descriptor.append(short_event_description) short_event_descriptor_multi.append(short_event_description) elif rec == 0x4E: - ISO_639_language_code = str(data[pos+3:pos+5]) + ISO_639_language_code = str(data[pos + 3:pos + 5]) extended_event_description = "" extended_event_description_multi = "" for i in range (pos+8,pos+length): @@ -256,7 +261,7 @@ extended_event_description += '\n' extended_event_description_multi += '\n' else: - if data[i]== '\x10' or data[i]== '\x00' or data[i]== '\x02': + if data[i]=='\x10' or data[i]=='\x00' or data[i]=='\x02': pass else: extended_event_description += data[i] @@ -265,88 +270,83 @@ extended_event_descriptor.append(extended_event_description) extended_event_descriptor_multi.append(extended_event_description) elif rec == 0x50: - component_descriptor.append(data[pos+8:pos+length]) + component_descriptor.append(data[pos + 8:pos + length]) elif rec == 0x54: - content_descriptor.append(data[pos+8:pos+length]) + content_descriptor.append(data[pos + 8:pos + length]) elif rec == 0x4A: - linkage_descriptor.append(data[pos+8:pos+length]) + linkage_descriptor.append(data[pos + 8:pos + length]) elif rec == 0x55: - parental_rating_descriptor.append(data[pos+2:pos+length]) + parental_rating_descriptor.append(data[pos + 2:pos + length]) else: - print "unsopported descriptor: %x %x" %(rec, pos + 12) + print "unsupported descriptor: %x %x" % (rec, pos + 12) #print data[pos:pos+length] pass pos += length - ### TODO: right identication?! - - # Very bad but there can be both encodings - # User files can be in cp1252 - # Is there no other way? - if short_event_descriptor: - short_event_descriptor = "".join(short_event_descriptor) - else: - short_event_descriptor = "".join(short_event_descriptor_multi) - if short_event_descriptor: - #try: - # short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8") - #except UnicodeDecodeError: - # pass + # Very bad but there can be both encodings + # User files can be in cp1252 + # Is there no other way? + if short_event_descriptor: + short_event_descriptor = "".join(short_event_descriptor) + else: + short_event_descriptor = "".join(short_event_descriptor_multi) + if short_event_descriptor: + #try: + # short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8") + #except UnicodeDecodeError: + # pass + try: + short_event_descriptor.decode('utf-8') + except UnicodeDecodeError: try: - short_event_descriptor.decode('utf-8') + short_event_descriptor = short_event_descriptor.decode("cp1252").encode("utf-8") except UnicodeDecodeError: - try: - short_event_descriptor = short_event_descriptor.decode("cp1252").encode("utf-8") - except UnicodeDecodeError: - # do nothing, otherwise cyrillic wont properly displayed - #short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8") - pass - if (lang == "cs") or (lang == "sk"): - short_event_descriptor = str(convertCharSpecCZSK(short_event_descriptor)) - if (lang == "hr"): - short_event_descriptor = str(convertCharSpecHR(short_event_descriptor)) - self.eit['name'] = short_event_descriptor + # do nothing, otherwise cyrillic wont properly displayed + #short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8") + pass + if (lang == "cs") or (lang == "sk"): + short_event_descriptor = str(convertCharSpecCZSK(short_event_descriptor)) + if lang == "hr": + short_event_descriptor = str(convertCharSpecHR(short_event_descriptor)) + self.eit['name'] = short_event_descriptor - # Very bad but there can be both encodings - # User files can be in cp1252 - # Is there no other way? - if extended_event_descriptor: - extended_event_descriptor = "".join(extended_event_descriptor) - else: - extended_event_descriptor = "".join(extended_event_descriptor_multi) - if extended_event_descriptor: - #try: - # extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8") - #except UnicodeDecodeError: - # pass + # Very bad but there can be both encodings + # User files can be in cp1252 + # Is there no other way? + if extended_event_descriptor: + extended_event_descriptor = "".join(extended_event_descriptor) + else: + extended_event_descriptor = "".join(extended_event_descriptor_multi) + if extended_event_descriptor: + #try: + # extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8") + #except UnicodeDecodeError: + # pass + try: + extended_event_descriptor.decode('utf-8') + except UnicodeDecodeError: try: - extended_event_descriptor.decode('utf-8') + extended_event_descriptor = extended_event_descriptor.decode("cp1252").encode("utf-8") except UnicodeDecodeError: - try: - extended_event_descriptor = extended_event_descriptor.decode("cp1252").encode("utf-8") - except UnicodeDecodeError: - # do nothing, otherwise cyrillic wont properly displayed - #extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8") - pass - if (lang == "cs") or (lang == "sk"): - extended_event_descriptor = str(convertCharSpecCZSK(extended_event_descriptor)) - if (lang == "hr"): - extended_event_descriptor = str(convertCharSpecHR(extended_event_descriptor)) - self.eit['description'] = extended_event_descriptor + # do nothing, otherwise cyrillic wont properly displayed + #extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8") + pass + if (lang == "cs") or (lang == "sk"): + extended_event_descriptor = str(convertCharSpecCZSK(extended_event_descriptor)) + if lang == "hr": + extended_event_descriptor = str(convertCharSpecHR(extended_event_descriptor)) + self.eit['description'] = extended_event_descriptor - else: - # No date clear all - self.eit = {} + else: + # No data clear all + self.eit = {} -"""Module docstring. - -Read Eit File and show the information. -""" -import sys -import getopt def readeit(eitfile): - eitlist=EitList(eitfile) + """Module docstring. + Read Eit File and show the information. + """ + eitlist = EitList(eitfile) print "Name: ", eitlist.getEitName() print "StartDate: ", eitlist.getEitStartDate() print "Description: ", eitlist.getEitDescription()