eit.py

Sat, 04 Nov 2017 20:56:03 +0100

author
mdd
date
Sat, 04 Nov 2017 20:56:03 +0100
changeset 1
88b1fefb7c92
parent 0
8b8cdafcc745
child 2
319f8c3fd394
permissions
-rw-r--r--

pylint the crap

0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
1 #!/usr/bin/python
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
2 # -*- coding: utf-8 -*-
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
3 # iso-8859-2
0
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
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
30 import sys
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
31 import getopt
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
32
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
33 from datetime import datetime
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
34
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
35 from ISO639 import LanguageCodes
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
36
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
37 #def crc32(data):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
38 # poly = 0x4c11db7
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
39 # crc = 0xffffffffL
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
40 # for byte in data:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
41 # byte = ord(byte)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
42 # for bit in range(7,-1,-1): # MSB to LSB
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
43 # z32 = crc>>31 # top bit
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
44 # crc = crc << 1
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
45 # if ((byte>>bit)&1) ^ z32:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
46 # crc = crc ^ poly
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
47 # crc = crc & 0xffffffffL
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
48 # return crc
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
49
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
50 decoding_charSpecHR = {
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
51 u'Ć': u'\u0106', u'æ': u'\u0107', u'®': u'\u017D', u'¾': u'\u017E',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
52 u'©': u'\u0160', u'¹': u'\u0161', u'Č': u'\u010C', u'è': u'\u010D', u'ð': u'\u0111'
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
53 }
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
54
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
55 decoding_charSpecCZSK = {
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
56 u'Ï'+u'C': u'Č', u'Ï'+u'E': u'Ě', u'Ï'+u'L': u'Ľ', u'Ï'+u'N': u'Ň', u'Ï'+u'R': u'Ř',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
57 u'Ï'+u'S': u'Š', u'Ï'+u'T': u'Ť', u'Ï'+u'Z': u'Ž', u'Ï'+u'c': u'č', u'Ï'+u'd': u'ď',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
58 u'Ï'+u'e': u'ě', u'Ï'+u'l': u'ľ', u'Ï'+u'n': u'ň', u'Ï'+u'r': u'ř', u'Ï'+u's': u'š',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
59 u'Ï'+u't': u'ť', u'Ï'+u'z': u'ž', u'Ï'+u'D': u'Ď', u'Â'+u'A': u'Á', u'Â'+u'E': u'É',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
60 u'Â'+u'I': u'Í', u'Â'+u'O': u'Ó', u'Â'+u'U': u'Ú', u'Â'+u'a': u'á', u'Â'+u'e': u'é',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
61 u'Â'+u'i': u'í', u'Â'+u'o': u'ó', u'Â'+u'u': u'ú', u'Â'+u'y': u'ý', u'Ã'+u'o': u'ô',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
62 u'Ã'+u'O': u'Ô', u'Ê'+u'u': u'ů', u'Ê'+u'U': u'Ů', u'È'+u'A': u'Ä', u'È'+u'E': u'Ë',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
63 u'È'+u'I': u'Ï', u'È'+u'O': u'Ö', u'È'+u'U': u'Ü', u'È'+u'Y': u'Ÿ', u'È'+u'a': u'ä',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
64 u'È'+u'e': u'ë', u'È'+u'i': u'ï', u'È'+u'o': u'ö', u'È'+u'u': u'ü', u'È'+u'y': u'ÿ'
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
65 }
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
66
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
67 def emcDebugOut(msg):
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
68 print msg
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
69
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
70 def convertCharSpecHR(text):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
71 for i, j in decoding_charSpecHR.iteritems():
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
72 text = text.replace(i, j)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
73 return text
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
74
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
75 def convertCharSpecCZSK(text):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
76 for i, j in decoding_charSpecCZSK.iteritems():
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
77 text = text.replace(i, j)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
78 return text
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
79
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
80 def parseMJD(mjd):
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
81 """Parse 16 bit unsigned int containing Modified Julian Date,
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
82 as per DVB-SI spec
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
83 returning year,month,day"""
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
84 year = int((mjd - 15078.2) / 365.25)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
85 month = int((mjd - 14956.1 - int(year * 365.25)) / 30.6001)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
86 day = mjd - 14956 - int(year * 365.25) - int(month * 30.6001)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
87 correction = 0
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
88 if month == 14 or month == 15:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
89 correction = 1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
90 return (1900 + year + correction), (month - 1 - correction * 12), day
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
91
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
92 def bcd2dec(byte):
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
93 return (byte >> 4) * 10 + (byte & 0xf)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
94
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
95 def language_iso639_2to3(alpha2):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
96 ret = alpha2
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
97 if alpha2 in LanguageCodes:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
98 language = LanguageCodes[alpha2]
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
99 for alpha, name in LanguageCodes.items():
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
100 if name == language:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
101 if len(alpha) == 3:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
102 return alpha
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
103 return ret
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
104
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
105 class EitList():
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
106 """Eit File support class
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
107 Description
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
108 http://de.wikipedia.org/wiki/Event_Information_Table
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
109 """
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
110 EIT_SHORT_EVENT_DESCRIPTOR = 0x4d
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
111 EIT_EXTENDED_EVENT_DESCRIPOR = 0x4e
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
112
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
113 def __init__(self, path=None):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
114 self.eit_file = None
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
115
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
116 self.eit = {}
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
117 self.iso = None
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
118
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
119 self.__newPath(path)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
120 self.__readEitFile()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
121
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
122 def __newPath(self, path):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
123 name = None
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
124 if path:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
125 if self.eit_file != path:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
126 self.eit_file = path
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
127
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
128 def __mk_int(self, s):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
129 return int(s) if s else 0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
130
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
131 def __toDate(self, d, t):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
132 if d and t:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
133 try:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
134 return datetime(
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
135 int(d[0]), int(d[1]), int(d[2]),
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
136 int(t[0]), int(t[1]))
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
137 except ValueError:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
138 return None
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
139 else:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
140 return None
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
141
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
142 def getEitsid(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
143 return self.eit.get('service', "") #TODO
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
144
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
145 def getEitTsId(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
146 return self.eit.get('transportstream', "") #TODO
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
147
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
148 def getEitWhen(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
149 return self.eit.get('when', "")
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
150
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
151 def getEitStartDate(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
152 return self.eit.get('startdate', "")
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
153
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
154 def getEitStartTime(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
155 return self.eit.get('starttime', "")
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
156
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
157 def getEitDuration(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
158 return self.eit.get('duration', "")
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
159
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
160 def getEitName(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
161 return self.eit.get('name', "").strip()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
162
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
163 def getEitDescription(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
164 return self.eit.get('description', "").strip()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
165
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
166 # Wrapper
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
167 def getEitShortDescription(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
168 return self.getEitName()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
169
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
170 def getEitExtendedDescription(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
171 return self.getEitDescription()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
172
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
173 def getEitLengthInSeconds(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
174 length = self.eit.get('duration', "")
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
175 if len(length) > 2:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
176 return self.__mk_int((length[0] * 60 + length[1]) * 60 + length[2])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
177 elif len(length) > 1:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
178 return self.__mk_int(length[0] * 60 + length[1])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
179 else:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
180 return self.__mk_int(length)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
181
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
182 def getEitDate(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
183 return self.__toDate(self.getEitStartDate(), self.getEitStartTime())
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
184
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
185 def dumpEit(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
186 print self.eit
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
187
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
188 ##############################################################################
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
189 ## File IO Functions
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
190 def __readEitFile(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
191 data = ""
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
192 path = self.eit_file
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
193
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
194 lang = language_iso639_2to3( "de" )
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
195
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
196 if path and os.path.exists(path):
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
197 print "Reading Event Information Table " + str(path)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
198
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
199 # Read data from file
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
200 fd = None
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
201 try:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
202 fd = open(path, 'rb')
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
203 #lines = f.readlines()
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
204 data = fd.read()
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
205 except Exception, err:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
206 print "[META] Exception in readEitFile: " + str(err)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
207 finally:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
208 if fd is not None:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
209 fd.close()
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
210
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
211 # Parse the data
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
212 if data and 12 <= len(data):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
213 # go through events
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
214 pos = 0
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
215 e = struct.unpack(">HHBBBBBBH", data[pos:pos + 12])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
216 event_id = e[0]
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
217 date = parseMJD(e[1]) # Y, M, D
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
218 time = bcd2dec(e[2]), bcd2dec(e[3]), bcd2dec(e[4]) # HH, MM, SS
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
219 duration = bcd2dec(e[5]), bcd2dec(e[6]), bcd2dec(e[7]) # HH, MM, SS
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
220 running_status = (e[8] & 0xe000) >> 13
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
221 free_CA_mode = e[8] & 0x1000
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
222 descriptors_len = e[8] & 0x0fff
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
223
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
224 if running_status in [1, 2]:
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
225 self.eit['when'] = "NEXT"
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
226 elif running_status in [3, 4]:
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
227 self.eit['when'] = "NOW"
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
228
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
229 self.eit['startdate'] = date
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
230 self.eit['starttime'] = time
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
231 self.eit['duration'] = duration
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
232
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
233 pos = pos + 12
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
234 short_event_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
235 short_event_descriptor_multi = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
236 extended_event_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
237 extended_event_descriptor_multi = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
238 component_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
239 content_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
240 linkage_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
241 parental_rating_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
242 endpos = len(data) - 1
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
243 while pos < endpos:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
244 rec = ord(data[pos])
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
245 length = ord(data[pos + 1]) + 2
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
246 if rec == 0x4D:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
247 descriptor_tag = ord(data[pos + 1])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
248 descriptor_length = ord(data[pos + 2])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
249 ISO_639_language_code = str(data[pos + 3:pos + 5])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
250 event_name_length = ord(data[pos + 5])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
251 short_event_description = data[pos + 6:pos + 6 + event_name_length]
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
252 if ISO_639_language_code == lang:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
253 short_event_descriptor.append(short_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
254 short_event_descriptor_multi.append(short_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
255 elif rec == 0x4E:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
256 ISO_639_language_code = str(data[pos + 3:pos + 5])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
257 extended_event_description = ""
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
258 extended_event_description_multi = ""
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
259 for i in range (pos+8,pos+length):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
260 if str(ord(data[i]))=="138":
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
261 extended_event_description += '\n'
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
262 extended_event_description_multi += '\n'
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
263 else:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
264 if data[i]=='\x10' or data[i]=='\x00' or data[i]=='\x02':
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
265 pass
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
266 else:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
267 extended_event_description += data[i]
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
268 extended_event_description_multi += data[i]
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
269 if ISO_639_language_code == lang:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
270 extended_event_descriptor.append(extended_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
271 extended_event_descriptor_multi.append(extended_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
272 elif rec == 0x50:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
273 component_descriptor.append(data[pos + 8:pos + length])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
274 elif rec == 0x54:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
275 content_descriptor.append(data[pos + 8:pos + length])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
276 elif rec == 0x4A:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
277 linkage_descriptor.append(data[pos + 8:pos + length])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
278 elif rec == 0x55:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
279 parental_rating_descriptor.append(data[pos + 2:pos + length])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
280 else:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
281 print "unsupported descriptor: %x %x" % (rec, pos + 12)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
282 #print data[pos:pos+length]
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
283 pass
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
284 pos += length
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
285
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
286 # Very bad but there can be both encodings
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
287 # User files can be in cp1252
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
288 # Is there no other way?
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
289 if short_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
290 short_event_descriptor = "".join(short_event_descriptor)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
291 else:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
292 short_event_descriptor = "".join(short_event_descriptor_multi)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
293 if short_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
294 #try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
295 # short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
296 #except UnicodeDecodeError:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
297 # pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
298 try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
299 short_event_descriptor.decode('utf-8')
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
300 except UnicodeDecodeError:
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
301 try:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
302 short_event_descriptor = short_event_descriptor.decode("cp1252").encode("utf-8")
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
303 except UnicodeDecodeError:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
304 # do nothing, otherwise cyrillic wont properly displayed
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
305 #short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
306 pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
307 if (lang == "cs") or (lang == "sk"):
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
308 short_event_descriptor = str(convertCharSpecCZSK(short_event_descriptor))
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
309 if lang == "hr":
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
310 short_event_descriptor = str(convertCharSpecHR(short_event_descriptor))
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
311 self.eit['name'] = short_event_descriptor
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
312
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
313 # Very bad but there can be both encodings
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
314 # User files can be in cp1252
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
315 # Is there no other way?
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
316 if extended_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
317 extended_event_descriptor = "".join(extended_event_descriptor)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
318 else:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
319 extended_event_descriptor = "".join(extended_event_descriptor_multi)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
320 if extended_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
321 #try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
322 # extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
323 #except UnicodeDecodeError:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
324 # pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
325 try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
326 extended_event_descriptor.decode('utf-8')
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
327 except UnicodeDecodeError:
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
328 try:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
329 extended_event_descriptor = extended_event_descriptor.decode("cp1252").encode("utf-8")
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
330 except UnicodeDecodeError:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
331 # do nothing, otherwise cyrillic wont properly displayed
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
332 #extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
333 pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
334 if (lang == "cs") or (lang == "sk"):
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
335 extended_event_descriptor = str(convertCharSpecCZSK(extended_event_descriptor))
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
336 if lang == "hr":
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
337 extended_event_descriptor = str(convertCharSpecHR(extended_event_descriptor))
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
338 self.eit['description'] = extended_event_descriptor
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
339
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
340 else:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
341 # No data clear all
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
342 self.eit = {}
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
343
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
344
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
345 def readeit(eitfile):
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
346 """Module docstring.
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
347 Read Eit File and show the information.
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
348 """
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
349 eitlist = EitList(eitfile)
0
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