-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathe2ToTVH.py
executable file
·464 lines (401 loc) · 14.9 KB
/
e2ToTVH.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/python
import sys, os, re, argparse, hashlib
# Class handling the content of the lamedb.
class lamedb:
def __init__(self, filename):
self.filename = filename
self.services = False
self.transponders = False
self.load()
# Exit if there are no services or transponders.
if len(self.services) == 0 or len(self.transponders) == 0:
print "ERROR: No services or transponders available."
sys.exit(-1)
# Read lamedb file and load parsed content into memory.
def load(self):
# Access file and load content into memory.
content = ""
try:
print "Reading file: " + self.filename
f = open(self.filename)
content = f.readlines()
f.close()
except:
print "ERROR: Could not open or read from file: " + self.filename
sys.exit(-1)
# Parse lamedb content.
read_transponders = False
read_services = False
tmp = []
self.transponders = {}
self.services = {}
print "Parsing content from file: " + self.filename
for line in content:
line = line.rstrip('\n')
line = line.decode("ascii", "ignore")
line = line.encode('utf-8')
# Got END tag.
if line == "end":
read_transponders = False
read_services = False
continue
# Got START tag for: transponders.
if line == "transponders":
read_transponders = True
continue
# Got START tag for: services.
if line == "services":
read_services = True
continue
# Read data for: transponders.
# 1 - 00820000:1ce8:0071
# 2 - s 12188000:27500000:1:4:130:2:0
if read_transponders == True:
if line == "/":
self.transponders[tmp[0]] = tmp[1].replace("\t", "").replace(" ", ":")
tmp = []
else:
tmp.append(line)
# Read data for: services.
# 1 - 1c85:00820000:1ce8:0071:1:0
# 2 - Polsat Romans
# 3 - p:Cyfrowy Polsat S.A.,C:0000
if read_services == True:
if line[0:2] == "p:":
hsid, namespace, htsid, honid, stype, snumber = tmp[0].split(':')
transponder = namespace + ":" + htsid + ":" + honid
# Strip leading 0 from hex strings.
hsid = re.sub("^0+","", hsid)
namespace = re.sub("^0+","", namespace)
htsid = re.sub("^0+","", htsid)
honid = re.sub("^0+","", honid)
stype = str(hex(int(stype))).lower()
stype = stype[2:len(stype)]
stype = re.sub("^0+","", stype)
sref = "1:0:" + stype.upper() + ":" + hsid.upper() + ":" + htsid.upper() + \
":" + honid.upper() + ":" + namespace.upper() + ":0:0:0:"
self.services[sref] = {'hsid' : hsid, # Service id (SSID value from stream) in Hex.
'namespace' : namespace, # DVB namespace in Hex.
'htsid' : htsid, # Transport stream id in Hex.
'honid' : honid, # Original network id in Hex.
'stype' : stype, # Service type.
'snumber' : snumber, # Service number in Decimal.
'transponder' : transponder, # Transponder.
'sname' : tmp[1]} # Service name.
# Split and parse provider data.
provdata = []
tmpprovdata = line.split(',')
for tmpdata in tmpprovdata:
psdata = tmpdata.split(':')
if psdata[0] == "p":
self.services[sref]['provider']= psdata[1]
else:
data = {}
# Strip leading 0 of hex fields.
psdata[1] = re.sub("^0+","", psdata[1])
data[psdata[0]] = psdata[1]
provdata.append(data)
self.services[sref]['provider_data'] = provdata
tmp = []
appLog('debug', 'Got lamedb entry: ' + str(self.services[sref]))
else:
tmp.append(line)
# Get channelname and servicedata by using a service reference key.
def getServiceBySRef(self, sref):
try:
return self.services[sref]
except:
return None
# Class handling the enigma2 bouquets and services.
class e2bouquets:
def __init__(self, directory):
self.directory = directory
self.tv_bouquets = []
self.tv_services = []
self.radio_bouquets = []
self.radio_services = []
self.load()
# Load the bouquet informations into memory.
def load(self):
if os.path.isfile(self.directory + "/" + "bouquets.tv"):
print "Reading TV bouquets..."
self.read_bqfile("bouquets.tv")
if os.path.isfile(self.directory + "/" + "bouquets.radio"):
print "Reading RADIO bouquets..."
self.read_bqfile("bouquets.radio")
# Read a bouquet file.
def read_bqfile(self, filename):
data = {}
bqname = ""
services = []
subbouquets = []
bqtype = ""
try:
appLog('debug', 'Reading e2 bouquet file: ' + self.directory + "/" + filename)
f = open(self.directory + "/" + filename)
bqs = f.readlines()
f.close()
except:
print "ERROR: Could not open or read from file: " + self.directory + "/" + filename
sys.exit(-1)
# Regex to match certain lines.
bqname_re = re.compile('#NAME (.*) .*$')
subbouquet_re = re.compile('.*BOUQUET "(.*)".*')
service_re = re.compile('#SERVICE (.*:)')
bqtype_re = re.compile('^#NAME .*\((.*)\)')
for line in bqs:
line = line.rstrip('\n')
# Name.
sresult = bqname_re.search(line)
if sresult != None:
bqname = sresult.group(1)
# Bouquet-Type.
sresult = bqtype_re.search(line)
if sresult != None:
bqtype = sresult.group(1)
# Subbouquet.
sresult = subbouquet_re.search(line)
if sresult != None:
subbouquets.append(sresult.group(1))
continue
# Service.
sresult = service_re.search(line)
if sresult != None:
services.append(sresult.group(1))
# Add bouquet to list if service exists.
if len(services) > 0:
if bqtype == "TV":
appLog('debug', 'Found e2 TV bouquet: ' + bqname)
self.tv_bouquets.append(bqname)
for service in services:
appLog('debug', 'Found e2 TV service: ' + str(service))
self.tv_services.append([service, bqname])
if bqtype == "Radio":
appLog('debug', 'Found e2 RADIO bouquet: ' + bqname)
self.radio_bouquets.append(bqname)
for service in services:
appLog('debug', 'Found e2 RADIO service: ' + str(service))
self.radio_services.append([service, bqname])
# Walk through other existing subbouquets.
if len(subbouquets) > 0:
appLog('debug', 'Found ' + str(len(subbouquets)) + ' subbouquets')
for sub in subbouquets:
self.read_bqfile(sub)
# Class handling the TV-Headend file structure.
class tvhstruct:
def __init__(self, directory, lamedb, e2bq):
self.directory = directory
self.lamedb = lamedb
self.e2bq = e2bq
self.services = {}
# Load existing TV-Headend configuration.
self.load()
# Load TV-Headend service configuration into memory.
def load(self):
tvh_svcname_re = re.compile('"svcname": "(.*)"')
tvh_provider_re = re.compile('"provider": "(.*)"')
tvh_sid_re = re.compile('"sid": (.*),')
print "Reading TV-Headend service files..."
for (path, dirs, files) in os.walk(self.directory + '/input/dvb/networks'):
spath = path.split('/')
if spath[len(spath)-1] == "services":
for file in files:
f = open(path + "/" + file)
content = f.readlines()
f.close()
svcname = ""
provider = ""
sid = ""
for line in content:
line = line.rstrip('\n')
sresult = tvh_svcname_re.search(line)
if sresult != None:
svcname = sresult.group(1)
sresult = tvh_provider_re.search(line)
if sresult != None:
provider = sresult.group(1)
sresult = tvh_sid_re.search(line)
if sresult != None:
sid = sresult.group(1)
if svcname != "" and provider != "":
self.services[file] = [svcname, provider]
appLog('debug', 'Found TVH service: ' + file + ' (' + svcname + ' / ' + provider + ')')
# Get a service from dictionary by using the name as key.
def getServiceByName(self, svcname, provider):
for key in self.services:
if self.services[key][0] == svcname:
return key
return None
# Write given bouquets into TV-Headend configuration.
def writeBouquets(self):
directory = self.directory + "/channeltags"
print "Writing TV-Headend bouquet configuration..."
# Loop through all bouquets and write configuration files.
i = 1
for bq in self.e2bq.tv_bouquets:
self.writeBouquetFile(i, bq)
i = i + 1
for bq in self.e2bq.radio_bouquets:
self.writeBouquetFile(i, bq)
i = i + 1
# Write a TV-Headend bouquet file.
def writeBouquetFile(self, number, name):
print "Wrting bouquet file: " + name
appLog('debug', 'Bouquet name: ' + name)
directory = self.directory + "/channeltags"
try:
f = open(directory + "/" + str(number), 'w')
f.write('{\n')
f.write('\t"enabled": 1,\n')
f.write('\t"internal": 0,\n')
f.write('\t"titledIcon": 0,\n')
f.write('\t"name": "' + name + '",\n')
f.write('\t"comment": "",\n')
f.write('\t"icon": "",\n')
f.write('\t"id": ' + str(number) + '\n')
f.write('}\n')
f.close()
except:
print "ERROR: Could not write file: " + directory + "/" + str(number)
sys.exit(-1)
# Write into TV-Headend service configuration.
def writeServices(self):
directory = self.directory + "/channel"
print "Writing TV-Headend service configuration..."
i = 0
# Write TV services.
for service in self.e2bq.tv_services:
i = i + 1
servicedata = self.lamedb.getServiceBySRef(service[0])
if servicedata == None:
print " ServiceReference from bouquet not found in lamedb: " + service[0]
continue
tvhkey = self.getServiceByName(servicedata['sname'], servicedata['provider'])
if tvhkey == None:
print " Could not find service in TV-Headend service list: " + servicedata['sname'] + " (" + \
servicedata['provider'] + ")"
continue
bqn = 1
for bq in self.e2bq.tv_bouquets:
if bq == service[1]:
break;
bqn = bqn + 1
self.writeServiceFile(servicedata['hsid'], i, tvhkey, bqn)
# Write RADIO services.
for service in self.e2bq.radio_services:
i = i + 1
servicedata = self.lamedb.getServiceBySRef(service[0])
if servicedata == None:
print "ERROR: ServiceReference from bouquet not found in lamedb: " + service[0]
continue
tvhkey = self.getServiceByName(servicedata['sname'], servicedata['provider'])
if tvhkey == None:
print " Could not find service in TV-Headend service list: " + servicedata['sname'] + " (" + \
servicedata['provider'] + ")"
continue
bqn = len(self.e2bq.tv_bouquets)+1
for bq in self.e2bq.radio_bouquets:
if bq == service[1]:
break;
bqn = bqn + 1
self.writeServiceFile(servicedata['hsid'], i, tvhkey, bqn)
# Write a TV-Headend service file.
def writeServiceFile(self, sid, channelnumber, tvhkey, tag):
directory = self.directory + "/channel"
try:
m = hashlib.md5()
m.update(sid)
md5sid = m.hexdigest()
appLog('debug', 'Wring file for service: ' + md5sid)
f = open(directory + "/" + str(md5sid), 'w')
f.write('{\n')
f.write('\t"number": ' + str(channelnumber) + ',\n')
f.write('\t"dvr_pre_time": 5,\n')
f.write('\t"dvr_pst_time": 5,\n')
f.write('\t"services": [\n')
f.write('\t\t"' + tvhkey + '"\n')
f.write('\t],\n')
f.write('\t"tags": [\n')
f.write('\t\t' + str(tag) + '\n')
f.write('\t]\n')
f.write('}\n')
f.close()
except:
print "ERROR: Could not write file: " + directory + "/" + str(md5sid)
sys.exit(-1)
# Log messages to stdout.
def appLog(mode, text, reason = ""):
if mode.lower() == 'debug':
if debug == False:
return
msg = "[" + mode.upper() + "] " + text
if reason != "":
msg = msg + " (" + reason + ")"
print msg
# Main entry for this script.
debug = False
def main(argv):
global debug
inputdir = ""
outputdir = ""
parser = argparse.ArgumentParser(description = 'Enigma2 to TV-Headend channel and bouquet converter')
parser.add_argument('-d','--debug', help = 'Show debug output', action="store_true")
parser.add_argument('-i','--input', help = 'Enigma2 input directory', required = True)
parser.add_argument('-o','--output', help = 'TV-Headend configuration directory', required = True)
args = parser.parse_args()
if args.debug == True:
debug = True
appLog('debug', 'Commandline options: ' + str(args))
inputdir = args.input.rstrip('/')
outputdir = args.output.rstrip('/')
# Check whether directories exist.
if not os.path.isdir(args.input):
print "ERROR: Enigma2 source directory does not exist"
sys.exit(-1)
if not os.path.isdir(args.output):
print "ERROR: TV-Headend destination directory does not exist"
sys.exit(-1)
# Check directory for tvh source services.
directory = outputdir + "/input/dvb/networks"
if not os.path.isdir(directory):
print "ERROR: Mandatory TV-Headend service directory does not exist"
sys.exit(-1)
# Check directory for channeltags.
directory = outputdir + "/channeltags"
print "Writing TV-Headend bouquet configuration..."
if not os.path.isdir(directory):
try:
os.makedirs(directory)
except:
print "ERROR: Could not create directory: " + directory
sys.exit(-1)
else:
print "ERROR: Directory already exists: " + directory
sys.exit(-1)
# Check directory for channel services.
directory = outputdir + "/channel"
print "Writing TV-Headend service configuration..."
if not os.path.isdir(directory):
try:
os.makedirs(directory)
except:
print "ERROR: Could not create directory: " + directory
sys.exit(-1)
else:
print "ERROR: Directory already exists: " + directory
sys.exit(-1)
# Trigger main functions.
appLog('debug', 'Creating lamedb object which holds all service informations')
ldb = lamedb(inputdir + '/lamedb')
appLog('debug', 'Creating e2 objects which holds all bouquet informations')
e2bq = e2bouquets(inputdir)
appLog('debug', 'Creating TV-Headend objects which holds all already existing services')
tvh = tvhstruct(outputdir, ldb, e2bq)
appLog('debug', 'Write found bouquets')
tvh.writeBouquets()
appLog('debug', 'Write found services')
tvh.writeServices()
appLog('debug', 'END OF CODE')
# Main entry for script.
if __name__ == "__main__":
main(sys.argv[1:])