-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicons.py
807 lines (647 loc) · 25.2 KB
/
picons.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#!/bin/python2
# -*- coding: utf-8 -*-
__version__ = "0.3.9"
import argparse
import sys
import logging
import os
import io
import re
import unicodedata
import json
import urllib2
import threading
import time
import uuid
import urllib
from functools import wraps
import socket
__progress__ = 0
reload(sys)
sys.setdefaultencoding('utf-8')
DEBUG_MODE = False
CONFIG = {
'updateurl': "https://raw.githubusercontent.com/josemoraes99/enigma2_picons/master/picons.py",
'urlPicons': "https://hk319yfwbl.execute-api.sa-east-1.amazonaws.com/prod",
'e2dir': '/etc/enigma2/',
'lambedbFile': '/etc/enigma2/lamedb',
'localPiconDirectory': '/usr/share/enigma2/picon/',
'bouquetGroup': ["bouquets.radio", "bouquets.tv"],
'tvheadendAddress': 'localhost',
'tvheadendPort': '9981',
'tvheadendPiconDirectory': '/home/root/tvheadend_picons/',
'tvheadendChannelConfigDirectory': '/home/root/.hts/tvheadend/channel/config/',
'tvheadendAuth': '',
'dl_simultaneos': 14,
}
DEV_CONFIG = {
'updateurl': CONFIG['updateurl'],
'urlPicons': CONFIG['urlPicons'],
'e2dir': 'etc/',
'lambedbFile': 'etc/lamedb',
'localPiconDirectory': 'picon/',
'bouquetGroup': CONFIG['bouquetGroup'],
'tvheadendAddress': 'e2.lan',
'tvheadendPort': '9981',
'tvheadendPiconDirectory': 'tvheadend_picons/',
'tvheadendChannelConfigDirectory': 'etc/tvheadend/channel/config/',
'tvheadendAuth': '',
'dl_simultaneos': 14,
}
def update(dl_url, force_update=False):
"""
Attempts to download the update url in order to find if an update is needed.
If an update is needed, the current script is backed up and the update is
saved in its place.
"""
def compare_versions(vA, vB):
"""
Compares two version number strings
@param vA: first version string to compare
@param vB: second version string to compare
@author <a href="http_stream://sebthom.de/136-comparing-version-numbers-in-jython-pytho/">Sebastian Thomschke</a>
@return negative if vA < vB, zero if vA == vB, positive if vA > vB.
"""
if vA == vB:
return 0
def num(s):
if s.isdigit():
return int(s)
return s
seqA = map(num, re.findall('\d+|\w+', vA.replace('-SNAPSHOT', '')))
seqB = map(num, re.findall('\d+|\w+', vB.replace('-SNAPSHOT', '')))
# this is to ensure that 1.0 == 1.0.0 in cmp(..)
lenA, lenB = len(seqA), len(seqB)
for i in range(lenA, lenB):
seqA += (0,)
for i in range(lenB, lenA):
seqB += (0,)
rc = cmp(seqA, seqB)
if rc == 0:
if vA.endswith('-SNAPSHOT'):
return -1
if vB.endswith('-SNAPSHOT'):
return 1
return rc
# dl the first 256 bytes and parse it for version number
try:
http_stream = urllib.urlopen(dl_url)
# update_file = http_stream.read(256)
update_file = http_stream.read(300)
http_stream.close()
except IOError, (errno, strerror):
logging.info("Unable to retrieve version data")
logging.info("Error %s: %s" % (errno, strerror))
return
match_regex = re.search(r'__version__ *= *"(\S+)"', update_file)
if not match_regex:
logging.info("No version info could be found")
return
update_version = match_regex.group(1)
if not update_version:
logging.info("Unable to parse version data")
return
if force_update:
logging.info("Forcing update, downloading version %s..." %
update_version)
else:
cmp_result = compare_versions(__version__, update_version)
if cmp_result < 0:
logging.info("Newer version %s available, downloading..." %
update_version)
elif cmp_result > 0:
logging.info("Local version %s newer then available %s, not updating." % (
__version__, update_version))
return
else:
logging.info("You already have the latest version.")
return
# dl, backup, and save the updated script
app_path = os.path.realpath(sys.argv[0])
# if __asModule__ == True:
# app_path = __file__
if not os.access(app_path, os.W_OK):
logging.info("Cannot update -- unable to write to %s" % app_path)
dl_path = app_path + ".new"
backup_path = app_path + ".old"
try:
dl_file = open(dl_path, 'w')
http_stream = urllib.urlopen(dl_url)
total_size = None
bytes_so_far = 0
chunk_size = 8192
try:
total_size = int(http_stream.info().getheader(
'Content-Length').strip())
except:
# The header is improper or missing Content-Length, just download
dl_file.write(http_stream.read())
while total_size:
chunk = http_stream.read(chunk_size)
dl_file.write(chunk)
bytes_so_far += len(chunk)
if not chunk:
break
percent = float(bytes_so_far) / total_size
percent = round(percent*100, 2)
sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" %
(bytes_so_far, total_size, percent))
if bytes_so_far >= total_size:
sys.stdout.write('\n')
http_stream.close()
dl_file.close()
except IOError, (errno, strerror):
logging.info("Download failed")
logging.info("Error %s: %s" % (errno, strerror))
return
try:
os.rename(app_path, backup_path)
except OSError, (errno, strerror):
logging.info("Unable to rename %s to %s: (%d) %s" %
(app_path, backup_path, errno, strerror))
return
try:
os.rename(dl_path, app_path)
except OSError, (errno, strerror):
logging.info("Unable to rename %s to %s: (%d) %s" %
(dl_path, app_path, errno, strerror))
return
try:
import shutil
shutil.copymode(backup_path, app_path)
except:
os.chmod(app_path, 0755)
logging.info("New version installed as %s" % app_path)
logging.info("(previous version backed up to %s)" % (backup_path))
return True
# def convert_size(size_bytes):
# if size_bytes == 0:
# return "0B"
# size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
# i = int(math.floor(math.log(size_bytes, 1024)))
# p = math.pow(1024, i)
# s = round(size_bytes / p, 2)
# return "%s %s" % (s, size_name[i])
# update_progress() : Displays or updates a console progress bar
# Accepts a float between 0 and 1. Any int will be converted to a float.
# A value under 0 represents a 'halt'.
# A value at 1 or bigger represents 100%
def update_progress(progress):
if DEBUG_MODE:
return
barLength = 50 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "\r\n"
# status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rProgresso: [{0}] {1}% {2}".format(
"="*block + " "*(barLength-block), int(progress*100), status)
# text = "\rPercent: [{0}] {1}% {2}".format( "="*block + " "*(barLength-block), int(progress*100), status)
sys.stdout.write(text)
sys.stdout.flush()
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
"""Retry calling the decorated function using an exponential backoff.
http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
:param ExceptionToCheck: the exception to check. may be a tuple of
exceptions to check
:type ExceptionToCheck: Exception or tuple
:param tries: number of times to try (not retry) before giving up
:type tries: int
:param delay: initial delay between retries in seconds
:type delay: int
:param backoff: backoff multiplier e.g. value of 2 will double the delay
each retry
:type backoff: int
:param logger: logger to use. If None, print
:type logger: logging.Logger instance
"""
def deco_retry(f):
@wraps(f)
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay
while mtries > 1:
try:
return f(*args, **kwargs)
except ExceptionToCheck, e:
msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
if logger:
logger.warning(msg)
else:
print msg
time.sleep(mdelay)
mtries -= 1
mdelay *= backoff
return f(*args, **kwargs)
return f_retry # true decorator
return deco_retry
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
@retry(urllib2.URLError, tries=4, delay=3, backoff=2)
def urlopen_with_retry(url):
return urllib2.urlopen(url)
def downloadFile(url, fileArr, conf):
global __progress__
if DEBUG_MODE:
print("Downloading " + url)
imgdata = urlopen_with_retry(url).read()
for dlArr in fileArr:
if dlArr[0] == "e2":
filename = conf['localPiconDirectory'] + dlArr[1]
with open(filename, 'wb') as f:
if DEBUG_MODE:
print("Saving " + filename)
f.write(imgdata)
if dlArr[0] == "tvh":
filename = conf['tvheadendPiconDirectory'] + dlArr[1]
if not os.path.isdir(conf['tvheadendPiconDirectory']):
os.mkdir(conf['tvheadendPiconDirectory'])
with open(filename, 'wb') as f:
if DEBUG_MODE:
print("Saving " + filename)
f.write(imgdata)
if DEBUG_MODE:
print("concluido " + url)
__progress__ += 1
def downloadPicons(listDw, conf):
logging.info("Enviando lista dos picons")
global __progress__
piconsList = []
receivedList = []
downloadList = []
numDownloads = 0
for file in listDw:
piconsList.append(file[0])
uuidOne = uuid.getnode()
piconsList = list(dict.fromkeys(piconsList))
data = {'src': 'e2', 'node': uuidOne, 'listChannel': piconsList}
data = json.dumps(data)
# print(data)
req = urllib2.Request(conf['urlPicons'], data, {
'Content-Type': 'application/json'})
fil = urllib2.urlopen(req)
# response = json.load(fil)
# print(response)
# data = json.load(response)
# print(response)
# listURL = ast.literal_eval(response) # procurar alternativa
listURL = json.load(fil)
fil.close()
if DEBUG_MODE:
print( json.dumps(listURL, sort_keys=True, indent=4) )
print( "Downloads simultaneos: ",CONFIG['dl_simultaneos'])
# time.sleep(5)
for l in listURL:
found = False
for rl in receivedList:
if l[1] == rl[0]:
found = True
rl[1].append(l[0])
if found == False:
receivedList.append([l[1], [l[0]]])
for l in receivedList:
for chan in l[1]:
# print chan
for file in listDw:
if file[0] == chan:
found = False
for dl in downloadList:
if dl[0] == l[0]:
found = True
dl[1] = dl[1] + file[1] # combine array
if found == False:
downloadList.append([l[0], file[1]])
logging.info("Download dos arquivos")
threads = []
update_progress(float(0))
numDownloads = len(downloadList)
for dl in downloadList:
if numDownloads > 0:
prog = float(__progress__) / float(numDownloads)
update_progress(prog)
t = threading.Thread(target=downloadFile, args=(dl[0], dl[1], conf))
t.start()
threads.append(t)
while threading.active_count() > CONFIG['dl_simultaneos']:
# print(threading.active_count())
time.sleep(0.1)
while __progress__ < numDownloads:
if threading.active_count() == 1:
break
prog = float(__progress__) / float(numDownloads)
update_progress(prog)
time.sleep(0.3)
update_progress(float(1))
map(lambda t: t.join(), threads)
def lerLameDb(f):
logging.info("Lendo arquivo " + f)
if os.path.isfile(f):
lDb = []
finalList = []
with io.open(f, encoding='utf-8', errors='ignore') as f:
for line in f:
lDb.append(line)
for x in range(0, len(lDb)):
# print x
if lDb[x].startswith("p:"):
nomeCanal = lDb[x - 1].strip()
tmpId = lDb[x - 2].strip().split(":")
oPosit = format(int(tmpId[4]), 'x')
if oPosit == "80":
oPosit = "1"
idChannel = "1:0:" + oPosit + ":" + tmpId[0].lstrip("0") + ":" + tmpId[2].lstrip(
"0") + ":" + tmpId[3].lstrip("0") + ":" + tmpId[1] + ":0:0:0"
canalclean = re.sub(re.compile('\W'), '', ''.join(c.lower() for c in unicodedata.normalize(
'NFKD', nomeCanal.replace("+", "mais")).encode('ascii', 'ignore') if not c.isspace()))
filenameE2 = idChannel.replace(":", "_").upper() + '.png'
if canalclean != "":
finalList.append([filenameE2, canalclean])
return finalList
else:
logging.info("Arquivo nao encontrado")
return False
def lerBouquetGroup(conf):
g = conf['bouquetGroup']
bResult = []
for b in g:
bResult = bResult + lerArquivoBouquet(b, conf)
listChan = []
for f in bResult:
listChan = listChan + lerArquivoUserBouquet(f, conf)
listChClean = []
for l in listChan:
if l not in listChClean:
listChClean.append(l)
return listChClean
def lerArquivoBouquet(f, conf):
fileR = conf['e2dir'] + f
logging.info("Lendo arquivo " + fileR)
if os.path.isfile(fileR):
with io.open(fileR, encoding='utf-8', errors='ignore') as f:
resp = []
for line in f:
if line.startswith("#SERVICE"):
resp.append(line.split('BOUQUET "')[1].split('" ')[0])
return resp
else:
logging.info("Arquivo nao encontrado")
return False
def lerArquivoUserBouquet(f, conf):
excludeBouquets = ["1:0:CA", "1:320:0"] # tres primeiros
fileR = conf['e2dir'] + f
channels = []
logging.info("Lendo arquivo " + fileR)
if os.path.isfile(fileR):
with io.open(fileR, encoding='utf-8', errors='ignore') as f:
resp = []
for line in f:
if line.startswith("#SERVICE"):
lineSpl = line.split('#SERVICE ')[1].strip()
if "::" in lineSpl:
tmpChannel = lineSpl.split("::")
canalclean = re.sub(re.compile('\W'), '', ''.join(c.lower() for c in unicodedata.normalize(
'NFKD', tmpChannel[1].replace("+", "mais")).encode('ascii', 'ignore') if not c.isspace()))
filenameE2 = tmpChannel[0].replace(
":", "_").upper() + '.png'
channels.append([filenameE2, canalclean])
return channels
def remove_duplicates(a):
tmpList = []
finalList = []
for item in a:
found = False
if item[1].strip() != "":
for curItem in tmpList:
if item[0] == curItem[0] and item[1] == curItem[1]:
found = True
if found == False:
# tmpList.append([item[1],[["e2", item[0]]]])
tmpList.append([item[0], item[1]])
for i in tmpList:
found = False
for f in finalList:
if i[1] == f[0]:
found = True
f[1].append(["e2", i[0]])
if found == False:
# i[1].append(["e2", i[1]])
# else:
finalList.append([i[1], [["e2", i[0]]]])
return finalList
def mergeLists(listE2, listTvh):
finalList = []
for item in listE2:
for itemTvh in listTvh:
if item[0] == itemTvh[1]:
# print "found " + itemTvh[0]
item[1].append(["tvh", itemTvh[0]])
finalList.append(item)
for itemTvh in listTvh:
itemFound = False
for itemFinal in finalList:
if itemTvh[1] == itemFinal[0]:
itemFound = True
if itemFound == False:
# print "new item " + itemTvh[1]
finalList.append([itemTvh[1], [["tvh", itemTvh[0]]]])
return finalList
def check_for_tvh(conf):
"""
Verifica se tvh ok
"""
logging.info("Verificando TVHeadend")
resp = False
logging.info("TVHeadend running")
try:
req = urllib2.Request(
"http://" + conf['tvheadendAddress'] + ":" + conf['tvheadendPort'] + '/api/serverinfo')
urllib2.urlopen(req)
except urllib2.HTTPError as e_error:
logging.info("TVHeadend com autenticação, utilize --help")
logging.info('Error code: %s', e_error.code)
except urllib2.URLError as e_error:
logging.info("TVHeadend nao encontrado")
logging.info('Reason: %s', e_error.reason)
else:
resp = True
return resp
def getTvhChannelList(conf):
logging.info("Obtendo lista de canais do TVHeadend")
finalList = []
req = urllib2.Request("http://" + conf['tvheadendAuth'] +
conf['tvheadendAddress'] + ":" + conf['tvheadendPort'] + '/api/channel/list')
fil = urllib2.urlopen(req)
listURL = json.load(fil)
fil.close()
for l in listURL['entries']:
canalclean = re.sub(re.compile('\W'), '', ''.join(c.lower() for c in unicodedata.normalize(
'NFKD', l['val'].replace("+", "mais")).encode('ascii', 'ignore') if not c.isspace()))
canalTvh = re.sub(re.compile('\W'), '', ''.join(c.lower() for c in unicodedata.normalize(
'NFKD', l['val'].replace("+", "plus").replace("&", "and")).encode('ascii', 'ignore') if not c.isspace()))
if canalclean == 'namenotset':
continue
item = []
item.append(canalTvh + ".png")
item.append(canalclean)
found = False
for it in finalList:
if it[1] == canalclean:
found = True
if found == False and canalclean != "":
finalList.append(item)
return finalList
def changeTvhConfig(conf):
logging.info("Alterando configuracao do TVHeadend")
tvhreq = urllib.urlopen("http://" + conf['tvheadendAuth'] + conf['tvheadendAddress'] + ":" + conf['tvheadendPort'] +
'/api/config/save?node={"chiconpath":"file:///home/root/tvheadend_picons/%25c.png", "prefer_picon":"False", "chiconscheme":"2"}')
# if os.path.isdir(conf['tvheadendChannelConfigDirectory']):
# logging.info( "Reset Icon dos canais do TVHeadend" )
# path = conf['tvheadendChannelConfigDirectory']
# files = []
# # r=root, d=directories, f = files
# for r, d, f in os.walk(path):
# for file in f:
# files.append(os.path.join(r, file))
# for f in files:
# with open(f, "r+") as jsonFile:
# data = json.load(jsonFile)
# if data['icon'] != "":
# data['icon'] = ""
# jsonFile.seek(0) # rewind
# json.dump(data, jsonFile, indent=4)
# jsonFile.truncate()
req = urllib2.Request("http://" + conf['tvheadendAuth'] +
conf['tvheadendAddress'] + ":" + conf['tvheadendPort'] + '/api/channel/list')
fil = urllib2.urlopen(req)
listURL = json.load(fil)
fil.close()
logging.info("'Reset Icon' dos canais do TVHeadend")
for l in listURL['entries']:
req1 = urllib2.Request("http://" + conf['tvheadendAuth'] + conf['tvheadendAddress'] + ":" +
conf['tvheadendPort'] + '/api/idnode/save?node={"uuid":"' + l['key'] + '","icon":""}')
fil1 = urllib2.urlopen(req1)
def reconfigure_image_cache(conf):
logging.info("Reconfigurando imagecache do TVHeadend")
req = urllib2.Request("http://" + conf['tvheadendAddress'] +
":" + conf['tvheadendPort'] + '/api/imagecache/config/load')
fil = urllib2.urlopen(req)
listURL = json.load(fil)
fil.close()
imagecache_enabled = False
for l in listURL['entries'][0]['params']:
if l['id'].lower() == "enabled":
if l['value'] == True:
imagecache_enabled = True
if not imagecache_enabled:
req = urllib2.Request("http://" + conf['tvheadendAddress'] + ":" +
conf['tvheadendPort'] + '/api/imagecache/config/save?node={"enabled":"true"}')
fil = urllib2.urlopen(req)
fil.close()
time.sleep(1)
req = urllib2.Request("http://" + conf['tvheadendAddress'] + ":" +
conf['tvheadendPort'] + '/api/imagecache/config/clean?clean=1')
fil = urllib2.urlopen(req)
fil.close()
# executa automaticamente com o clean
# req = urllib2.Request( "http://" + conf['tvheadendAddress'] + ":" + conf['tvheadendPort'] + '/api/imagecache/config/trigger?trigger=1' )
# fil = urllib2.urlopen(req)
# fil.close()
def iniciaDownloadPicons(conf):
listMerged = []
listFiles = lerLameDb(conf['lambedbFile'])
if listFiles:
channelList = lerBouquetGroup(conf)
if channelList:
listMerged = listFiles + channelList
listMerged = remove_duplicates(listMerged)
hasTvh = check_for_tvh(conf)
if (hasTvh):
tvhChannelList = getTvhChannelList(conf)
listMerged = mergeLists(listMerged, tvhChannelList)
downloadPicons(listMerged, conf)
if (hasTvh):
changeTvhConfig(conf)
reconfigure_image_cache(conf)
logging.info("Atencao, recomendado reiniciar o serviço do TVHeadend")
logging.info("Pronto.")
def main():
global CONFIG, DEV_CONFIG
global DEBUG_MODE
parser = argparse.ArgumentParser(
description='Download de picons para o e2.')
group = parser.add_mutually_exclusive_group()
group.add_argument('--no-update', action='store_true',
help='não verifica por atualização')
group.add_argument('--force-update', action='store_true',
help='força atualização')
groupDebug = parser.add_mutually_exclusive_group()
groupDebug.add_argument(
'--debug', action='store_true', help='modo de testes')
groupDev = parser.add_mutually_exclusive_group()
groupDev.add_argument(
'--dev', action='store_true', help='modo de testes')
# groupTvhAuth = parser.add_mutually_exclusive_group()
# groupTvhAuth.add_argument('--tvh-user', action='store_true', help = 'usuario admin do Tvheadend')
# groupTvhAuth.add_argument('--tvh-password', action='store_true', help = 'senha do Tvheadend')
parser.add_argument('--tvh-user', type=str,
help='usuario admin do Tvheadend')
parser.add_argument('--tvh-password', type=str, help='senha do Tvheadend')
parser.add_argument('--downloads-simultaneos', type=int, help='quantidade de downloads simultaneos, entre 1 e 20')
args = parser.parse_args()
# workaround tvheadend localhost
CONFIG['tvheadendAddress'] = get_ip()
# print CONFIG['tvheadendAddress']
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
logging.info("version " + __version__)
ckUpdates = True
if args.no_update or args.dev:
ckUpdates = False
if args.dev:
print(args)
CONFIG = DEV_CONFIG
if args.debug:
print("Debug mode ativo.")
DEBUG_MODE = True
if (args.tvh_user and not args.tvh_password) or (not args.tvh_user and args.tvh_password):
print("Necessário --tvh-user e --tvh-password juntos")
sys.exit()
if args.tvh_user and args.tvh_password:
# CONFIG['tvheadendAuth'] = args.tvh_user + ":" + args.tvh_password + "@"
# print CONFIG['tvheadendAuth']
print("Autenticação não implementado ainda, sorry.")
sys.exit()
if args.downloads_simultaneos:
if args.downloads_simultaneos > 0 and args.downloads_simultaneos < 20:
CONFIG['dl_simultaneos'] = args.downloads_simultaneos
if args.force_update:
update(CONFIG['updateurl'], True)
logging.info("Pronto.")
sys.exit()
if ckUpdates:
updateReturn = update(CONFIG['updateurl'])
if updateReturn:
logging.info("Reiniciando script")
python = sys.executable
os.execl(python, python, *sys.argv)
iniciaDownloadPicons(CONFIG)
if __name__ == "__main__":
main()