eit.py

Sat, 25 Nov 2017 15:37:54 +0100

author
mdd
date
Sat, 25 Nov 2017 15:37:54 +0100
changeset 13
cf5c5cec1b2b
parent 9
1bf778001041
child 17
842120f00078
permissions
-rw-r--r--

bugfix: cleanup status messages when processing multiple files

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
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
4 """
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
5 EitSupport
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
6 Copyright (C) 2011 betonme
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
7 Copyright (C) 2016 Wolfgang Fahl
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
8 Cleanup 2017 by mdd
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
9 """
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
10 # This EITParser is based on:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
11 # https://github.com/betonme/e2openplugin-EnhancedMovieCenter/blob/master/src/EitSupport.py
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
12 #
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
13 # In case of reuse of this source code please do not remove this copyright.
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
14 #
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
15 # This program is free software: you can redistribute it and/or modify
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
16 # it under the terms of the GNU General Public License as published by
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
17 # the Free Software Foundation, either version 3 of the License, or
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
18 # (at your option) any later version.
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
19 #
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
20 # This program is distributed in the hope that it will be useful,
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
23 # GNU General Public License for more details.
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
24 #
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
25 # For more information on the GNU General Public License see:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
26 # <http://www.gnu.org/licenses/>.
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
27 #
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
28
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
29 # seite 36, inhalt der for schleife!
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
30 # https://www.dvb.org/resources/public/standards/a38_dvb-si_specification.pdf
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
31
6
1420abafd049 finish eit
mdd
parents: 2
diff changeset
32 #pylint: disable=missing-docstring
1420abafd049 finish eit
mdd
parents: 2
diff changeset
33 #pylint: disable=line-too-long
1420abafd049 finish eit
mdd
parents: 2
diff changeset
34
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
35 import os
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
36 import struct
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
37 import sys
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
38 import getopt
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
39
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
40 from datetime import datetime
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
41
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
42 from ISO639 import LanguageCodes
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
43
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
44 #def crc32(data):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
45 # poly = 0x4c11db7
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
46 # crc = 0xffffffffL
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
47 # for byte in data:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
48 # byte = ord(byte)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
49 # for bit in range(7,-1,-1): # MSB to LSB
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
50 # z32 = crc>>31 # top bit
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
51 # crc = crc << 1
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
52 # if ((byte>>bit)&1) ^ z32:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
53 # crc = crc ^ poly
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
54 # crc = crc & 0xffffffffL
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
55 # return crc
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
56
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
57 EIT_SHORT_EVENT_DESCRIPTOR = 0x4d
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
58 EIT_EXTENDED_EVENT_DESCRIPOR = 0x4e
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
59
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
60 CHARSPEC_HR = {
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
61 u'Ć': u'\u0106', u'æ': u'\u0107', u'®': u'\u017D', u'¾': u'\u017E',
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
62 u'©': u'\u0160', u'¹': u'\u0161', u'Č': u'\u010C', u'è': u'\u010D', u'ð': u'\u0111'
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
63 }
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
64
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
65 CHARSPEC_CZSK = {
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
66 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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 }
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
76
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
77 def convert_charspec_hr(text):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
78 for i, j in CHARSPEC_HR.iteritems():
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
79 text = text.replace(i, j)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
80 return text
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
81
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
82 def convert_charspec_czsk(text):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
83 for i, j in CHARSPEC_CZSK.iteritems():
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
84 text = text.replace(i, j)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
85 return text
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
86
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
87 def parse_mjd(mjd):
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
88 """Parse 16 bit unsigned int containing Modified Julian Date,
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
89 as per DVB-SI spec
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
90 returning year,month,day"""
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
91 year = int((mjd - 15078.2) / 365.25)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
92 month = int((mjd - 14956.1 - int(year * 365.25)) / 30.6001)
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
93 day = mjd - 14956 - int(year * 365.25) - int(month * 30.6001)
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
94 correction = 0
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
95 if month == 14 or month == 15:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
96 correction = 1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
97 return (1900 + year + correction), (month - 1 - correction * 12), day
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
98
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
99 def bcd2dec(byte):
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
100 return (byte >> 4) * 10 + (byte & 0xf)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
101
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
102
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
103 def mkint(data):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
104 """
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
105 Convert string to Integer
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
106 """
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
107 return int(data) if data else 0
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
108
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
109 def todate(sdate, stime):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
110 """
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
111 Convert date and time to datetime tuple
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
112 """
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
113 if sdate and stime:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
114 try:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
115 return datetime(
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
116 int(sdate[0]), int(sdate[1]), int(sdate[2]),
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
117 int(stime[0]), int(stime[1]))
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
118 except ValueError:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
119 return None
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
120 else:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
121 return None
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
122
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
123 def cleanstring(data):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
124 """remove nonprintable chars from short desc
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
125 """
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
126 for char in ['\x10', '\x00', '\x02', '\x15']:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
127 data = data.replace(char, '')
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
128 return data
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
129
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
130 def language_iso639_2to3(alpha2):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
131 ret = alpha2
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
132 if alpha2 in LanguageCodes:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
133 language = LanguageCodes[alpha2]
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
134 for alpha, name in LanguageCodes.items():
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
135 if name == language:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
136 if len(alpha) == 3:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
137 return alpha
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
138 return ret
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
139
6
1420abafd049 finish eit
mdd
parents: 2
diff changeset
140 class eitinfo(object):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
141 """Eit File support class
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
142 Description
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
143 http://de.wikipedia.org/wiki/Event_Information_Table
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
144 """
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
145 def __init__(self, path=None):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
146 self.eit_file = None
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
147
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
148 self.eit = {}
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
149 self.iso = None
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
150
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
151 self.load(path)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
152
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
153 def load(self, path):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
154 if path:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
155 self.eit_file = path
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
156 self._read_file()
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
157
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
158 def get_genre(self):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
159 return self.eit.get('genre', "")
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
160
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
161 def get_components(self):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
162 return self.eit.get('components', "")
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
163
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
164 def get_startdate(self):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
165 return self.eit.get('startdate', "")
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
166
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
167 def get_starttime(self):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
168 return self.eit.get('starttime', "")
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
169
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
170 def get_duration(self):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
171 return self.eit.get('duration', "")
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
172
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
173 def get_name(self):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
174 return self.eit.get('name', "").strip()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
175
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
176 def get_description(self):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
177 return self.eit.get('description', "").strip()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
178
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
179 def get_duration_seconds(self):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
180 length = self.eit.get('duration', "")
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
181 if len(length) > 2:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
182 return mkint((length[0] * 60 + length[1]) * 60 + length[2])
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
183 elif len(length) > 1:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
184 return mkint(length[0] * 60 + length[1])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
185 else:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
186 return mkint(length)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
187
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
188 def get_date(self):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
189 return todate(self.get_startdate(), self.get_starttime())
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
190
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
191 def dumpEit(self):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
192 print self.eit
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
193
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
194 ##############################################################################
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
195 ## File IO Functions
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
196 def _read_file(self):
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
197 data = ""
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
198 path = self.eit_file
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
199
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
200 lang = language_iso639_2to3("de")
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
201
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
202 if path and os.path.exists(path):
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
203 print "Reading Event Information Table " + str(path)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
204
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
205 # Read data from file
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
206 fd = None
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
207 try:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
208 fd = open(path, 'rb')
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
209 #lines = f.readlines()
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
210 data = fd.read()
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
211 except Exception, err:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
212 print "[META] Exception in readEitFile: " + str(err)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
213 finally:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
214 if fd is not None:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
215 fd.close()
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
216
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
217 # Parse the data
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
218 if data and 12 <= len(data):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
219 # go through events
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
220 pos = 0
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
221 e = struct.unpack(">HHBBBBBBH", data[pos:pos + 12])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
222 event_id = e[0]
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
223 date = parse_mjd(e[1]) # Y, M, D
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
224 time = bcd2dec(e[2]), bcd2dec(e[3]), bcd2dec(e[4]) # HH, MM, SS
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
225 duration = bcd2dec(e[5]), bcd2dec(e[6]), bcd2dec(e[7]) # HH, MM, SS
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
226 #running_status = (e[8] & 0xe000) >> 13
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
227 #free_CA_mode = e[8] & 0x1000
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
228 descriptors_len = e[8] & 0x0fff
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
229
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
230 #if running_status in [1, 2]:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
231 # self.eit['when'] = "NEXT"
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
232 #elif running_status in [3, 4]:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
233 # self.eit['when'] = "NOW"
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
234
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
235 self.eit['startdate'] = date
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
236 self.eit['starttime'] = time
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
237 self.eit['duration'] = duration
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
238
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
239 pos = pos + 12
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
240 short_event_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
241 short_event_descriptor_multi = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
242 extended_event_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
243 extended_event_descriptor_multi = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
244 component_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
245 content_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
246 linkage_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
247 parental_rating_descriptor = []
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
248 endpos = len(data) - 1
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
249 while pos < endpos:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
250 rec = ord(data[pos])
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
251 length = ord(data[pos + 1]) + 2
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
252 if rec == 0x4D:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
253 descriptor_tag = ord(data[pos + 1])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
254 descriptor_length = ord(data[pos + 2])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
255 ISO_639_language_code = str(data[pos + 3:pos + 5])
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
256 event_name_length = ord(data[pos + 5])
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
257 short_event_description = cleanstring(data[pos + 6:pos + 6 + event_name_length])
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
258
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
259 tmp_length = ord(data[pos + 6 + event_name_length])
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
260 self.eit['genre'] = cleanstring(data[pos + 7 + event_name_length:pos + 7 + tmp_length + event_name_length])
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
261
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
262 if ISO_639_language_code == lang:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
263 short_event_descriptor.append(short_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
264 short_event_descriptor_multi.append(short_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
265 elif rec == 0x4E:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
266 ISO_639_language_code = str(data[pos + 3:pos + 5])
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
267 extended_event_description = ""
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
268 extended_event_description_multi = ""
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
269 for i in range(pos + 8, pos + length):
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
270 if str(ord(data[i])) == "138":
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
271 extended_event_description += '\n'
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
272 extended_event_description_multi += '\n'
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
273 elif data[i] not in ['\x10', '\x00', '\x02', '\x15']:
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
274 extended_event_description += data[i]
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
275 extended_event_description_multi += data[i]
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
276 if ISO_639_language_code == lang:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
277 extended_event_descriptor.append(extended_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
278 extended_event_descriptor_multi.append(extended_event_description)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
279 elif rec == 0x50:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
280 #tmp_type = ord(data[pos + 3:pos + 4])
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
281 #print "type: %x" % tmp_type
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
282 component_descriptor.append(cleanstring(data[pos + 8:pos + length]))
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
283 elif rec == 0x54:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
284 content_descriptor.append(cleanstring(data[pos + 8:pos + length]))
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
285 elif rec == 0x4A:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
286 linkage_descriptor.append(cleanstring(data[pos + 8:pos + length]))
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
287 elif rec == 0x55:
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
288 parental_rating_descriptor.append(cleanstring(data[pos + 2:pos + length]))
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
289 else:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
290 print "unsupported descriptor: %x %x" % (rec, pos + 12)
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
291 print data[pos:pos + length]
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
292
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
293 pos += length
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
294
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
295 self.eit['components'] = ", ".join(component_descriptor)
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
296
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
297
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
298
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
299 # Very bad but there can be both encodings
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
300 # User files can be in cp1252
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
301 # Is there no other way?
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
302 if short_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
303 short_event_descriptor = "".join(short_event_descriptor)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
304 else:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
305 short_event_descriptor = "".join(short_event_descriptor_multi)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
306 if short_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
307 #try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
308 # short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
309 #except UnicodeDecodeError:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
310 # pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
311 try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
312 short_event_descriptor.decode('utf-8')
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
313 except UnicodeDecodeError:
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
314 try:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
315 short_event_descriptor = short_event_descriptor.decode("cp1252").encode("utf-8")
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
316 except UnicodeDecodeError:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
317 # do nothing, otherwise cyrillic wont properly displayed
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
318 #short_event_descriptor = short_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
319 pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
320 if (lang == "cs") or (lang == "sk"):
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
321 short_event_descriptor = str(convert_charspec_czsk(short_event_descriptor))
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
322 if lang == "hr":
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
323 short_event_descriptor = str(convert_charspec_hr(short_event_descriptor))
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
324 self.eit['name'] = short_event_descriptor
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
325
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
326 # Very bad but there can be both encodings
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
327 # User files can be in cp1252
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
328 # Is there no other way?
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
329 if extended_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
330 extended_event_descriptor = "".join(extended_event_descriptor)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
331 else:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
332 extended_event_descriptor = "".join(extended_event_descriptor_multi)
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
333 if extended_event_descriptor:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
334 #try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
335 # extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
336 #except UnicodeDecodeError:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
337 # pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
338 try:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
339 extended_event_descriptor.decode('utf-8')
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
340 except UnicodeDecodeError:
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
341 try:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
342 extended_event_descriptor = extended_event_descriptor.decode("cp1252").encode("utf-8")
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
343 except UnicodeDecodeError:
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
344 # do nothing, otherwise cyrillic wont properly displayed
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
345 #extended_event_descriptor = extended_event_descriptor.decode("iso-8859-1").encode("utf-8")
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
346 pass
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
347 if (lang == "cs") or (lang == "sk"):
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
348 extended_event_descriptor = str(convert_charspec_czsk(extended_event_descriptor))
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
349 if lang == "hr":
2
319f8c3fd394 finished EIT parser
mdd
parents: 1
diff changeset
350 extended_event_descriptor = str(convert_charspec_hr(extended_event_descriptor))
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
351 self.eit['description'] = extended_event_descriptor
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
352
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
353 else:
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
354 # No data clear all
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
355 self.eit = {}
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
356
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
357
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
358 def readeit(eitfile):
1
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
359 """Module docstring.
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
360 Read Eit File and show the information.
88b1fefb7c92 pylint the crap
mdd
parents: 0
diff changeset
361 """
6
1420abafd049 finish eit
mdd
parents: 2
diff changeset
362 eitlist = eitinfo(eitfile)
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 6
diff changeset
363 if len(eitlist.eit) == 0:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 6
diff changeset
364 return None
6
1420abafd049 finish eit
mdd
parents: 2
diff changeset
365 out = "Movie name: %s" % eitlist.get_name()
1420abafd049 finish eit
mdd
parents: 2
diff changeset
366 out += "\nGenre: %s" % eitlist.get_genre()
1420abafd049 finish eit
mdd
parents: 2
diff changeset
367 out += "\nComponents: %s" % eitlist.get_components()
1420abafd049 finish eit
mdd
parents: 2
diff changeset
368 out += "\nStartDate: %s" % eitlist.get_date()
1420abafd049 finish eit
mdd
parents: 2
diff changeset
369 out += "\nDescription: %s" % eitlist.get_description()
1420abafd049 finish eit
mdd
parents: 2
diff changeset
370 out += "\nDuration: %02i:%02i:%02i" % eitlist.get_duration()
1420abafd049 finish eit
mdd
parents: 2
diff changeset
371 out += " (%s minutes)" % (eitlist.get_duration_seconds() / 60)
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
372
6
1420abafd049 finish eit
mdd
parents: 2
diff changeset
373 print out
1420abafd049 finish eit
mdd
parents: 2
diff changeset
374 return out
0
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
375
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
376 def main():
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
377 # parse command line options
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
378 try:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
379 opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
380 except getopt.error, msg:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
381 print msg
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
382 print "for help use --help"
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
383 sys.exit(2)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
384 # process options
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
385 for o, a in opts:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
386 if o in ("-h", "--help"):
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
387 print __doc__
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
388 sys.exit(0)
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
389 # process arguments
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
390 for arg in args:
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
391 readeit(arg) # process() is defined elsewhere
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
392
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
393 if __name__ == "__main__":
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
394 main()
8b8cdafcc745 basic EIT parser crap added
mdd
parents:
diff changeset
395

mercurial