-
Notifications
You must be signed in to change notification settings - Fork 5
/
service.py
596 lines (548 loc) · 28.1 KB
/
service.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
# -*- coding: utf-8 -*-
import xbmc
import xbmcaddon
import xbmcvfs
import xbmcgui
import time
from datetime import datetime
import os
import json
import re
import socket
from collections import Counter
from resources.lib import xml_structure
from resources.providers import magenta_DE
from resources.providers import tvspielfilm_DE
from resources.providers import swisscom_CH
from resources.providers import horizon
from resources.providers import zattoo
import sys
import platform
import importlib
ADDON = xbmcaddon.Addon(id="service.takealug.epg-grabber")
addon_name = ADDON.getAddonInfo('name')
addon_version = ADDON.getAddonInfo('version')
loc = ADDON.getLocalizedString
datapath = xbmcvfs.translatePath(ADDON.getAddonInfo('profile'))
temppath = os.path.join(datapath, "temp")
thread_temppath = os.path.join(temppath, "multithread")
machine = platform.machine()
def getAddonSetting(setting):
value = True if xbmcaddon.Addon(id="service.takealug.epg-grabber").getSetting(setting).upper() == 'TRUE' else False
return value
def getAddonCronSetting(setting):
value = xbmcaddon.Addon(id="service.takealug.epg-grabber").getSetting(setting)
return value
## Read Global Settings
storage_path = ADDON.getSetting('storage_path')
auto_download = getAddonSetting('auto_download')
timeswitch_1 = int(ADDON.getSetting('timeswitch_1'))
timeswitch_2 = int(ADDON.getSetting('timeswitch_2'))
timeswitch_3 = int(ADDON.getSetting('timeswitch_3'))
enable_rating_mapper = True if ADDON.getSetting('enable_rating_mapper').upper() == 'TRUE' else False
use_local_sock = True if ADDON.getSetting('use_local_sock').upper() == 'TRUE' else False
tvh_local_sock = ADDON.getSetting('tvh_local_sock')
download_threads = int(ADDON.getSetting('download_threads'))
enable_multithread = True if ADDON.getSetting('enable_multithread').upper() == 'TRUE' else False
## Get Enabled Grabbers
# Divers
enable_grabber_magentaDE = True if ADDON.getSetting('enable_grabber_magentaDE').upper() == 'TRUE' else False
enable_grabber_tvsDE = True if ADDON.getSetting('enable_grabber_tvsDE').upper() == 'TRUE' else False
enable_grabber_swcCH = True if ADDON.getSetting('enable_grabber_swcCH').upper() == 'TRUE' else False
# Horizon
enable_grabber_hznDE = True if ADDON.getSetting('enable_grabber_hznDE').upper() == 'TRUE' else False
enable_grabber_hznAT = True if ADDON.getSetting('enable_grabber_hznAT').upper() == 'TRUE' else False
enable_grabber_hznCH = True if ADDON.getSetting('enable_grabber_hznCH').upper() == 'TRUE' else False
enable_grabber_hznNL = True if ADDON.getSetting('enable_grabber_hznNL').upper() == 'TRUE' else False
enable_grabber_hznPL = True if ADDON.getSetting('enable_grabber_hznPL').upper() == 'TRUE' else False
enable_grabber_hznIE = True if ADDON.getSetting('enable_grabber_hznIE').upper() == 'TRUE' else False
enable_grabber_hznGB = True if ADDON.getSetting('enable_grabber_hznGB').upper() == 'TRUE' else False
enable_grabber_hznSK = True if ADDON.getSetting('enable_grabber_hznSK').upper() == 'TRUE' else False
enable_grabber_hznCZ = True if ADDON.getSetting('enable_grabber_hznCZ').upper() == 'TRUE' else False
enable_grabber_hznHU = True if ADDON.getSetting('enable_grabber_hznHU').upper() == 'TRUE' else False
enable_grabber_hznRO = True if ADDON.getSetting('enable_grabber_hznRO').upper() == 'TRUE' else False
# Zattoo
enable_grabber_zttDE = True if ADDON.getSetting('enable_grabber_zttDE').upper() == 'TRUE' else False
enable_grabber_zttCH = True if ADDON.getSetting('enable_grabber_zttCH').upper() == 'TRUE' else False
enable_grabber_1und1DE = True if ADDON.getSetting('enable_grabber_1und1DE').upper() == 'TRUE' else False
enable_grabber_qlCH = True if ADDON.getSetting('enable_grabber_qlCH').upper() == 'TRUE' else False
enable_grabber_mnetDE = True if ADDON.getSetting('enable_grabber_mnetDE').upper() == 'TRUE' else False
enable_grabber_walyCH = True if ADDON.getSetting('enable_grabber_walyCH').upper() == 'TRUE' else False
enable_grabber_mweltAT = True if ADDON.getSetting('enable_grabber_mweltAT').upper() == 'TRUE' else False
enable_grabber_bbvDE = True if ADDON.getSetting('enable_grabber_bbvDE').upper() == 'TRUE' else False
enable_grabber_vtxCH = True if ADDON.getSetting('enable_grabber_vtxCH').upper() == 'TRUE' else False
enable_grabber_myvisCH = True if ADDON.getSetting('enable_grabber_myvisCH').upper() == 'TRUE' else False
enable_grabber_gvisCH = True if ADDON.getSetting('enable_grabber_gvisCH').upper() == 'TRUE' else False
enable_grabber_sakCH = True if ADDON.getSetting('enable_grabber_sakCH').upper() == 'TRUE' else False
enable_grabber_nettvDE = True if ADDON.getSetting('enable_grabber_nettvDE').upper() == 'TRUE' else False
enable_grabber_eweDE = True if ADDON.getSetting('enable_grabber_eweDE').upper() == 'TRUE' else False
enable_grabber_qttvCH = True if ADDON.getSetting('enable_grabber_qttvCH').upper() == 'TRUE' else False
enable_grabber_saltCH = True if ADDON.getSetting('enable_grabber_saltCH').upper() == 'TRUE' else False
enable_grabber_swbDE = True if ADDON.getSetting('enable_grabber_swbDE').upper() == 'TRUE' else False
enable_grabber_eirIE = True if ADDON.getSetting('enable_grabber_eirIE').upper() == 'TRUE' else False
# Check if any Grabber is enabled
if (enable_grabber_magentaDE or enable_grabber_tvsDE or enable_grabber_swcCH or enable_grabber_hznDE or enable_grabber_hznAT or enable_grabber_hznCH or enable_grabber_hznNL or enable_grabber_hznPL or enable_grabber_hznIE or enable_grabber_hznGB or enable_grabber_hznSK or enable_grabber_hznCZ or enable_grabber_hznHU or enable_grabber_hznRO or enable_grabber_zttDE or enable_grabber_zttCH or enable_grabber_1und1DE or enable_grabber_qlCH or enable_grabber_mnetDE or enable_grabber_walyCH or enable_grabber_mweltAT or enable_grabber_bbvDE or enable_grabber_vtxCH or enable_grabber_myvisCH or enable_grabber_gvisCH or enable_grabber_sakCH or enable_grabber_nettvDE or enable_grabber_eweDE or enable_grabber_qttvCH or enable_grabber_saltCH or enable_grabber_swbDE or enable_grabber_eirIE):
enabled_grabber = True
else:
enabled_grabber = False
guide_temp = os.path.join(datapath, 'guide.xml')
guide_dest = os.path.join(storage_path, 'guide.xml')
grabber_cron = os.path.join(datapath, 'grabber_cron.json')
grabber_cron_tmp = os.path.join(temppath, 'grabber_cron.json')
xmltv_dtd = os.path.join(datapath, 'xmltv.dtd')
## Make a debug logger
def log(message, loglevel=xbmc.LOGDEBUG):
xbmc.log('[{} {}] {}'.format(addon_name, addon_version, message), loglevel)
## Make OSD Notify Messages
OSD = xbmcgui.Dialog()
## MAKE an Monitor
class Monitor(xbmc.Monitor):
def __init__(self):
xbmc.Monitor.__init__(self)
self.settingsChanged = False
def onSettingsChanged(self):
self.settingsChanged = True
monitor = Monitor()
def notify(title, message, icon=xbmcgui.NOTIFICATION_INFO):
OSD.notification(title, message, icon)
def copy_guide_to_destination():
done = xbmcvfs.copy(guide_temp, guide_dest)
if done:
try:
## Write new setting last_download
with open(grabber_cron, 'r', encoding='utf-8') as f:
data = json.load(f)
data['last_download'] = str(int(time.time()))
with open(grabber_cron_tmp, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
## rename temporary file replacing old file
xbmcvfs.copy(grabber_cron_tmp, grabber_cron)
xbmc.sleep(3000)
xbmcvfs.delete(grabber_cron_tmp)
notify(addon_name, loc(32350), icon=xbmcgui.NOTIFICATION_INFO)
log(loc(32350), xbmc.LOGINFO)
except:
log('Worker can´t read cron File, creating new File...'.format(loc(32356)), xbmc.LOGERROR)
with open(grabber_cron, 'w', encoding='utf-8') as f:
f.write(json.dumps({'last_download': str(int(time.time())), 'next_download': str(int(time.time()) + 86400)}))
notify(addon_name, loc(32350), icon=xbmcgui.NOTIFICATION_INFO)
log(loc(32350), xbmc.LOGINFO)
else:
notify(addon_name, loc(32351), icon=xbmcgui.NOTIFICATION_ERROR)
log(loc(32351), xbmc.LOGERROR)
def check_channel_dupes():
with open(guide_temp, encoding='utf-8') as f:
c = Counter(c.strip() for c in f if c.strip()) # for case-insensitive search
dupe = []
for line in c:
if c[line] > 1:
if ('display-name' in line or 'icon src' in line or '</channel' in line):
pass
else:
dupe.append(line + '\n')
dupes = ''.join(dupe)
if (not dupes == ''):
log('{} {}'.format(loc(32400), dupes), xbmc.LOGERROR)
dialog = xbmcgui.Dialog()
ok = dialog.ok('-]ERROR[- {}'.format(loc(32400)), dupes)
if ok:
return False
return False
else:
return True
def run_grabber():
if check_startup():
importlib.reload(xml_structure)
importlib.reload(magenta_DE)
importlib.reload(tvspielfilm_DE)
importlib.reload(swisscom_CH)
importlib.reload(horizon)
importlib.reload(zattoo)
xml_structure.xml_start()
## Check Provider , Create XML Channels
if enable_grabber_magentaDE:
if magenta_DE.startup():
magenta_DE.create_xml_channels()
if enable_grabber_tvsDE:
if tvspielfilm_DE.startup():
tvspielfilm_DE.create_xml_channels()
if enable_grabber_swcCH:
if swisscom_CH.startup():
swisscom_CH.create_xml_channels()
if enable_grabber_hznDE:
if horizon.startup('de'):
horizon.create_xml_channels('de')
if enable_grabber_hznAT:
if horizon.startup('at'):
horizon.create_xml_channels('at')
if enable_grabber_hznCH:
if horizon.startup('ch'):
horizon.create_xml_channels('ch')
if enable_grabber_hznNL:
if horizon.startup('nl'):
horizon.create_xml_channels('nl')
if enable_grabber_hznPL:
if horizon.startup('pl'):
horizon.create_xml_channels('pl')
if enable_grabber_hznIE:
if horizon.startup('ie'):
horizon.create_xml_channels('ie')
if enable_grabber_hznGB:
if horizon.startup('gb'):
horizon.create_xml_channels('gb')
if enable_grabber_hznSK:
if horizon.startup('sk'):
horizon.create_xml_channels('sk')
if enable_grabber_hznCZ:
if horizon.startup('cz'):
horizon.create_xml_channels('cz')
if enable_grabber_hznHU:
if horizon.startup('hu'):
horizon.create_xml_channels('hu')
if enable_grabber_hznRO:
if horizon.startup('ro'):
horizon.create_xml_channels('ro')
if enable_grabber_zttDE:
if zattoo.startup('ztt_de'):
zattoo.create_xml_channels('ztt_de')
if enable_grabber_zttCH:
if zattoo.startup('ztt_ch'):
zattoo.create_xml_channels('ztt_ch')
if enable_grabber_1und1DE:
if zattoo.startup('1und1_de'):
zattoo.create_xml_channels('1und1_de')
if enable_grabber_qlCH:
if zattoo.startup('ql_ch'):
zattoo.create_xml_channels('ql_ch')
if enable_grabber_mnetDE:
if zattoo.startup('mnet_de'):
zattoo.create_xml_channels('mnet_de')
if enable_grabber_walyCH:
if zattoo.startup('walytv_ch'):
zattoo.create_xml_channels('walytv_ch')
if enable_grabber_mweltAT:
if zattoo.startup('meinewelt_at'):
zattoo.create_xml_channels('meinewelt_at')
if enable_grabber_bbvDE:
if zattoo.startup('bbtv_de'):
zattoo.create_xml_channels('bbtv_de')
if enable_grabber_vtxCH:
if zattoo.startup('vtxtv_ch'):
zattoo.create_xml_channels('vtxtv_ch')
if enable_grabber_myvisCH:
if zattoo.startup('myvision_ch'):
zattoo.create_xml_channels('myvision_ch')
if enable_grabber_gvisCH:
if zattoo.startup('glattvision_ch'):
zattoo.create_xml_channels('glattvision_ch')
if enable_grabber_sakCH:
if zattoo.startup('sak_ch'):
zattoo.create_xml_channels('sak_ch')
if enable_grabber_nettvDE:
if zattoo.startup('nettv_de'):
zattoo.create_xml_channels('nettv_de')
if enable_grabber_eweDE:
if zattoo.startup('tvoewe_de'):
zattoo.create_xml_channels('tvoewe_de')
if enable_grabber_qttvCH:
if zattoo.startup('quantum_ch'):
zattoo.create_xml_channels('quantum_ch')
if enable_grabber_saltCH:
if zattoo.startup('salt_ch'):
zattoo.create_xml_channels('salt_ch')
if enable_grabber_swbDE:
if zattoo.startup('tvoswe_de'):
zattoo.create_xml_channels('tvoswe_de')
if enable_grabber_eirIE:
if zattoo.startup('eir_ie'):
zattoo.create_xml_channels('eir_ie')
# Check for Channel Dupes
if check_channel_dupes():
## Create XML Broadcast
if enable_grabber_magentaDE:
if magenta_DE.startup():
magenta_DE.create_xml_broadcast(enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_tvsDE:
if tvspielfilm_DE.startup():
tvspielfilm_DE.create_xml_broadcast(enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_swcCH:
if swisscom_CH.startup():
swisscom_CH.create_xml_broadcast(enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznDE:
if horizon.startup('de'):
horizon.create_xml_broadcast('de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznAT:
if horizon.startup('at'):
horizon.create_xml_broadcast('at', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznCH:
if horizon.startup('ch'):
horizon.create_xml_broadcast('ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznNL:
if horizon.startup('nl'):
horizon.create_xml_broadcast('nl', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznPL:
if horizon.startup('pl'):
horizon.create_xml_broadcast('pl', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznIE:
if horizon.startup('ie'):
horizon.create_xml_broadcast('ie', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznGB:
if horizon.startup('gb'):
horizon.create_xml_broadcast('gb', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznSK:
if horizon.startup('sk'):
horizon.create_xml_broadcast('sk', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznCZ:
if horizon.startup('cz'):
horizon.create_xml_broadcast('cz', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznHU:
if horizon.startup('hu'):
horizon.create_xml_broadcast('hu', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_hznRO:
if horizon.startup('ro'):
horizon.create_xml_broadcast('ro', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_zttDE:
if zattoo.startup('ztt_de'):
zattoo.create_xml_broadcast('ztt_de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_zttCH:
if zattoo.startup('ztt_ch'):
zattoo.create_xml_broadcast('ztt_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_1und1DE:
if zattoo.startup('1und1_de'):
zattoo.create_xml_broadcast('1und1_de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_qlCH:
if zattoo.startup('ql_ch'):
zattoo.create_xml_broadcast('ql_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_mnetDE:
if zattoo.startup('mnet_de'):
zattoo.create_xml_broadcast('mnet_de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_walyCH:
if zattoo.startup('walytv_ch'):
zattoo.create_xml_broadcast('walytv_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_mweltAT:
if zattoo.startup('meinewelt_at'):
zattoo.create_xml_broadcast('meinewelt_at', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_bbvDE:
if zattoo.startup('bbtv_de'):
zattoo.create_xml_broadcast('bbtv_de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_vtxCH:
if zattoo.startup('vtxtv_ch'):
zattoo.create_xml_broadcast('vtxtv_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_myvisCH:
if zattoo.startup('myvision_ch'):
zattoo.create_xml_broadcast('myvision_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_gvisCH:
if zattoo.startup('glattvision_ch'):
zattoo.create_xml_broadcast('glattvision_ch', enable_rating_mapper, thread_temppath,download_threads)
if enable_grabber_sakCH:
if zattoo.startup('sak_ch'):
zattoo.create_xml_broadcast('sak_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_nettvDE:
if zattoo.startup('nettv_de'):
zattoo.create_xml_broadcast('nettv_de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_eweDE:
if zattoo.startup('tvoewe_de'):
zattoo.create_xml_broadcast('tvoewe_de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_qttvCH:
if zattoo.startup('quantum_ch'):
zattoo.create_xml_broadcast('quantum_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_saltCH:
if zattoo.startup('salt_ch'):
zattoo.create_xml_broadcast('salt_ch', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_swbDE:
if zattoo.startup('tvoswe_de'):
zattoo.create_xml_broadcast('tvoswe_de', enable_rating_mapper, thread_temppath, download_threads)
if enable_grabber_eirIE:
if zattoo.startup('eir_ie'):
zattoo.create_xml_broadcast('eir_ie', enable_rating_mapper, thread_temppath, download_threads)
## Finish XML
xml_structure.xml_end()
copy_guide_to_destination()
## Write Guide in TVH Socked
if use_local_sock:
write_to_sock()
def write_to_sock():
if check_startup():
if (use_local_sock and os.path.isfile(guide_temp)):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
epg = open(guide_temp, 'rb')
epg_data = epg.read()
try:
log('{} {}'.format(loc(32380), tvh_local_sock), xbmc.LOGINFO)
notify(addon_name, loc(32380), icon=xbmcgui.NOTIFICATION_INFO)
sock.connect(tvh_local_sock)
sock.sendall(epg_data)
log('{} {}'.format(sock.recv, tvh_local_sock), xbmc.LOGINFO)
except socket.error as e:
notify(addon_name, '{} {}'.format(loc(32379), e), icon=xbmcgui.NOTIFICATION_ERROR)
log('{} {}'.format(loc(32379), e), xbmc.LOGERROR)
finally:
sock.close()
epg.close()
else:
ok = dialog.ok(loc(32119), loc(32409))
if ok:
log(loc(32409), xbmc.LOGERROR)
def worker(timeswitch_1, timeswitch_2, timeswitch_3):
initiate_download = False
## Read Settings for last / next_download
try:
with open(grabber_cron, 'r', encoding='utf-8') as f:
cron = json.load(f)
next_download = cron['next_download']
last_download = cron['last_download']
except:
log('Worker can´t read cron File, creating new File...'.format(loc(32356)), xbmc.LOGERROR)
with open(grabber_cron, 'w', encoding='utf-8') as f:
f.write(json.dumps({'last_download': str(int(time.time())), 'next_download': str(int(time.time()) + 86400)}))
with open(grabber_cron, 'r', encoding='utf-8') as f:
cron = json.load(f)
next_download = cron['next_download']
last_download = cron['last_download']
log('{} {}'.format(loc(32352), datetime.fromtimestamp(int(last_download)).strftime('%d.%m.%Y %H:%M')), xbmc.LOGDEBUG)
if (int(next_download) > int(last_download)):
log('{} {}'.format(loc(32353), datetime.fromtimestamp(int(next_download)).strftime('%d.%m.%Y %H:%M')), xbmc.LOGDEBUG)
if int(next_download) < int(time.time()):
# suggested download time has passed (e.g. system was offline) or time is now, download epg
# and set a new timestamp for the next download
log('{} {}'.format(loc(32352), datetime.fromtimestamp(int(last_download)).strftime('%d.%m.%Y %H:%M')), xbmc.LOGINFO)
log('{} {}'.format(loc(32353), datetime.fromtimestamp(int(next_download)).strftime('%d.%m.%Y %H:%M')), xbmc.LOGINFO)
log('{}'.format(loc(32356)), xbmc.LOGINFO)
initiate_download = True
## If next_download < last_download, initiate an Autodownload
if initiate_download:
notify(addon_name, loc(32357), icon=xbmcgui.NOTIFICATION_INFO)
run_grabber()
## Update Cron Settings
with open(grabber_cron, 'r', encoding='utf-8') as f:
cron = json.load(f)
next_download = cron['next_download']
last_download = cron['last_download']
## Calculate the next_download Setting
# get Settings for daily_1, daily_2, daily_3
today = datetime.today()
now = int(time.time())
calc_daily_1 = datetime(today.year, today.month, day=today.day, hour=timeswitch_1, minute=0, second=0)
calc_daily_2 = datetime(today.year, today.month, day=today.day, hour=timeswitch_2, minute=0, second=0)
calc_daily_3 = datetime(today.year, today.month, day=today.day, hour=timeswitch_3, minute=0, second=0)
try:
daily_1 = int(calc_daily_1.strftime("%s"))
daily_2 = int(calc_daily_2.strftime("%s"))
daily_3 = int(calc_daily_3.strftime("%s"))
except ValueError:
daily_1 = int(time.mktime(calc_daily_1.timetuple()))
daily_2 = int(time.mktime(calc_daily_2.timetuple()))
daily_3 = int(time.mktime(calc_daily_3.timetuple()))
## If sheduleplan for daily 1,2,3 is in the past, plan it for next day
if daily_1 <= now:
daily_1 += 86400
if daily_2 <= now:
daily_2 += 86400
if daily_3 <= now:
daily_3 += 86400
## Find the lowest Integer for next download
next_download = min([int(daily_1), int(daily_2), int(daily_3)])
## Write new setting next_download
with open(grabber_cron, 'w', encoding='utf-8') as f:
f.write(json.dumps({'last_download': str(int(last_download)), 'next_download': str(int(next_download))}))
def check_internet(host="8.8.8.8", port=53, timeout=3):
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except socket.error as ex:
return False
def check_startup():
#Create Tempfolder if not exist
if not os.path.exists(temppath):
os.makedirs(temppath)
if not os.path.exists(thread_temppath):
os.makedirs(thread_temppath)
if storage_path == 'choose':
notify(addon_name, loc(32359), icon=xbmcgui.NOTIFICATION_ERROR)
log(loc(32359), xbmc.LOGERROR)
return False
if not enabled_grabber:
notify(addon_name, loc(32360), icon=xbmcgui.NOTIFICATION_ERROR)
log(loc(32360), xbmc.LOGERROR)
return False
if use_local_sock:
socked_string = '.sock'
if not re.search(socked_string, tvh_local_sock):
notify(addon_name, loc(32378), icon=xbmcgui.NOTIFICATION_ERROR)
log(loc(32378), xbmc.LOGERROR)
return False
#if enable_multithread:
#if (not machine == 'x86_64' and not machine == 'armv7l' and not machine == 'armv8l'):
#log(machine, xbmc.LOGERROR)
#dialog = xbmcgui.Dialog()
#log(loc(32381), xbmc.LOGERROR)
#ok = dialog.ok(addon_name, loc(32381))
#if ok:
#return False
#return False
if enable_multithread:
log(machine, xbmc.LOGERROR)
dialog = xbmcgui.Dialog()
log('Multithreading is currently under Kodi 19 broken, please disable it', xbmc.LOGERROR)
ok = dialog.ok(addon_name, 'Multithreading is currently under Kodi 19 broken, please disable it')
if ok:
return False
return False
## create Crontab File which not exists at first time
if (not os.path.isfile(grabber_cron) or os.stat(grabber_cron).st_size <= 1):
with open(grabber_cron, 'w', encoding='utf-8') as f:
f.write(json.dumps({'last_download': str(int(time.time())), 'next_download': str(int(time.time()) + 86400)}))
## Clean Tempfiles
for file in os.listdir(temppath):
xbmcvfs.delete(os.path.join(temppath, file))
## Check internet Connection
if not check_internet():
retries = 12
while retries > 0:
log(loc(32385), xbmc.LOGINFO)
notify(addon_name, loc(32385), icon=xbmcgui.NOTIFICATION_INFO)
xbmc.sleep(5000)
if check_internet():
log(loc(32386), xbmc.LOGINFO)
notify(addon_name, loc(32386), icon=xbmcgui.NOTIFICATION_INFO)
return True
else:
retries -= 1
if retries == 0:
log(loc(32387), xbmc.LOGERROR)
notify(addon_name, loc(32387), icon=xbmcgui.NOTIFICATION_ERROR)
return False
else:
return True
if __name__ == '__main__':
if check_startup():
try:
dialog = xbmcgui.Dialog()
if sys.argv[1] == 'manual_download':
if not auto_download:
ret = dialog.yesno('Takealug EPG Grabber', loc(32401))
if ret:
notify(addon_name, loc(32376), icon=xbmcgui.NOTIFICATION_INFO)
run_grabber()
elif auto_download:
ok = dialog.ok(addon_name, loc(32414))
if ok:
pass
pass
if sys.argv[1] == 'write_to_sock':
ret = dialog.yesno(loc(32119), loc(32408))
if ret:
write_to_sock()
except IndexError:
while not monitor.waitForAbort(30):
if monitor.settingsChanged:
log('Settings changed Reloading', xbmc.LOGINFO)
auto_download = getAddonSetting('auto_download')
if auto_download:
timeswitch_1 = int(getAddonCronSetting('timeswitch_1'))
timeswitch_2 = int(getAddonCronSetting('timeswitch_2'))
timeswitch_3 = int(getAddonCronSetting('timeswitch_3'))
monitor.settingsChanged = False
if auto_download:
worker(timeswitch_1, timeswitch_2, timeswitch_3)