eit.py

Sat, 04 Nov 2017 20:12:11 +0100

author
mdd
date
Sat, 04 Nov 2017 20:12:11 +0100
changeset 0
8b8cdafcc745
child 1
88b1fefb7c92
permissions
-rw-r--r--

basic EIT parser crap added

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

mercurial