-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
467 lines (372 loc) · 13.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
# coding: utf-8
import math, urllib
import log
try:
import xbmc, xbmcaddon, xbmcgui
except ImportError:
pass
from time import time
from time import asctime
from time import localtime
from time import strftime
from time import gmtime
from time import sleep
import filesystem
try:
import player
except ImportError:
pass
import xml.etree.ElementTree as ET
_ADDON_NAME = 'script.media.aggregator'
try:
_addon = xbmcaddon.Addon(id=_ADDON_NAME)
_addondir = xbmc.translatePath(_addon.getAddonInfo('profile')).decode('utf-8')
except:
pass
# ------------------------------------------------------------------------------------------------------------------- #
class AddonRO(object):
def __init__(self, xml_filename='settings.xml'):
self._addon_xml = filesystem.join(_addondir, xml_filename)
self.check_exists()
self.load()
def check_exists(self):
pass
def load(self):
if not filesystem.exists(self._addon_xml):
self.root = None
self.mtime = 0
return
with filesystem.fopen(self._addon_xml, 'r') as f:
content = f.read()
self.root = ET.fromstring(content)
self.mtime = filesystem.getmtime(self._addon_xml)
# get setting no caching
def getSetting(self, s):
if not filesystem.exists(self._addon_xml):
return u''
if self.mtime != filesystem.getmtime(self._addon_xml):
self.load()
for item in self.root:
if item.get('id') == s:
return item.get('value').encode('utf-8')
return u''
# ------------------------------------------------------------------------------------------------------------------- #
class Addon(AddonRO):
@staticmethod
def _xml(data):
return data.replace("&", "&").replace("<", "<").replace("\"", """).replace(">", ">")
def check_exists(self):
if not filesystem.exists(self._addon_xml):
with filesystem.fopen(self._addon_xml, 'w') as f:
f.write('<settings>\n')
f.write('</settings>\n')
def setSetting(self, id, val):
# not work in Python 2.6
# item = self.root.find("./setting[@id='%s']" % str(id))
item = None
settings = self.root.findall("setting")
for setting in settings:
if setting.attrib.get('id') == id:
item = setting
break
if item is not None:
item.set('value', str(val))
else:
ET.SubElement(self.root, 'setting', attrib={'id': str(id), 'value': str(val)})
with filesystem.fopen(self._addon_xml, 'w') as f:
f.write('<settings>\n')
for item in self.root:
f.write(' <setting id="%s" value="%s" />\n' % (Addon._xml(item.get('id')), Addon._xml(item.get('value'))))
f.write('</settings>\n')
self.mtime = filesystem.getmtime(self._addon_xml)
# ------------------------------------------------------------------------------------------------------------------- #
def addon_data_path():
if _addon.getSetting('data_path'):
return _addon.getSetting('data_path')
else:
return _addondir
# ------------------------------------------------------------------------------------------------------------------- #
def call_bg(action, params = {}):
params['action'] = action
for key, value in params.iteritems():
if isinstance(value, unicode):
params[key] = value.encode('utf-8')
from plugin import RunPlugin
RunPlugin(**params)
def update_case():
# Init
if not hasattr(update_case, 'first_start'):
update_case.first_start = True
update_case.first_start_time = time()
update_case.prev_generate_time = update_case.first_start_time
if not hasattr(update_case, 'last_scheduled_day'):
update_case.last_scheduled_day = None
try:
every = int(_addon.getSetting('service_generate_persistent_every')) * 3600 # seconds
delay_startup = int(_addon.getSetting('delay_startup')) * 60
except ValueError:
every = 8 * 3600
delay_startup = 0
if _addon.getSetting('role').decode('utf-8') == u'клиент':
return
# User action
path = filesystem.join(addon_data_path(), 'start_generate')
if filesystem.exists(path):
log.debug('User action!!!')
filesystem.remove(path)
call_bg('update_service', {'show_progress': True})
update_case.first_start = False
return
# Startup
if time() > update_case.first_start_time + delay_startup and update_case.first_start:
if _addon.getSetting('service_startup') == 'true':
try:
log.debug("Persistent Update Service starting...")
log.debug(_addon.getSetting('service_startup'))
#update_service(show_progress=False)
call_bg('update_service', { 'show_progress': False })
except BaseException as e:
log.print_tb(e)
finally:
update_case.first_start = False
# Persistent
if time() >= update_case.prev_generate_time + every: # verification
if _addon.getSetting('service_generate_persistent') == 'true':
try:
update_case.prev_generate_time = time()
#update_service(show_progress=False)
call_bg('update_service', {'show_progress': False})
log.debug('Update List at %s' % asctime(localtime(update_case.prev_generate_time)))
log.debug('Next Update in %s' % strftime("%H:%M:%S", gmtime(every)))
except BaseException as e:
log.print_tb(e)
finally:
update_case.first_start = False
# Schedule
if _addon.getSetting('service_generate_schedule') == 'true':
try:
schedule_hour = _addon.getSetting('service_generate_schedule_time').split(':')[0]
except:
schedule_hour = None
import datetime
now = datetime.datetime.now()
if str(now.hour) == schedule_hour and update_case.last_scheduled_day != now.day:
try:
update_case.prev_generate_time = time()
update_case.last_scheduled_day = now.day
call_bg('update_service', {'show_progress': False})
log.debug('Update scheduled at %s' % asctime(localtime(update_case.prev_generate_time)))
except BaseException as e:
log.print_tb(e)
finally:
update_case.first_start = False
# ------------------------------------------------------------------------------------------------------------------- #
def scrape_case():
# Init
if not hasattr(scrape_case, 'prev_scrape_time'):
try:
#scrape_nnm()
call_bg('scrape_nnm')
log.debug('scrape_nnm at %s' % asctime())
except BaseException as e:
log.print_tb(e)
scrape_case.prev_scrape_time = time()
scrape_every = 30 * 60
if time() >= scrape_case.prev_scrape_time + scrape_every:
try:
scrape_case.prev_scrape_time = time()
#scrape_nnm()
call_bg('scrape_nnm')
log.debug('scrape_nnm at %s' % asctime())
except BaseException as e:
log.print_tb(e)
# ------------------------------------------------------------------------------------------------------------------- #
def add_media_case():
if _addon.getSetting('role').decode('utf-8') == u'клиент':
return
path = filesystem.join(addon_data_path(), 'add_media')
if filesystem.exists(path):
try:
with filesystem.fopen(path, 'r') as f:
while True:
try:
title = f.readline().strip(' \n\t\r').decode('utf-8')
imdb = f.readline().strip(' \n\t\r')
log.debug('add_media_case: ' + imdb)
log.debug(title)
if title and imdb:
call_bg( 'add_media_process', {'title': title, 'imdb': imdb} )
else:
break
except BaseException as e:
log.print_tb(e)
except BaseException as e:
log.print_tb(e)
finally:
filesystem.remove(path)
# ------------------------------------------------------------------------------------------------------------------- #
def main():
# import vsdbg
# vsdbg._attach(False)
#global _addon
# write addon info to log
try:
log.debug('********* Starting Media Aggregator ********************')
version = xbmcaddon.Addon().getAddonInfo('version')
log.debug('* Version {}'.format(version))
log.debug('********************************************************')
except:
pass
path = filesystem.join(addon_data_path(), 'update_library_next_start')
if filesystem.exists(path):
log.debug('User action!!! update_library_next_start')
from plugin import UpdateVideoLibrary
UpdateVideoLibrary()
filesystem.remove(path)
cnt = 0
while not xbmc.abortRequested:
try:
scrape_case()
update_case()
add_media_case()
except BaseException as e:
log.print_tb(e)
finally:
sleep(1)
if cnt % 3600 == 0:
log.debug("I'm alive at %s" % asctime())
cnt += 1
log.debug('service exit')
# ------------------------------------------------------------------------------------------------------------------- #
def start_generate():
path = filesystem.join(addon_data_path(), 'start_generate')
if not filesystem.exists(path):
with filesystem.fopen(path, 'w'):
pass
# ------------------------------------------------------------------------------------------------------------------- #
def update_library_next_start():
path = filesystem.join(addon_data_path(), 'update_library_next_start')
if not filesystem.exists(path):
with filesystem.fopen(path, 'w'):
pass
# ------------------------------------------------------------------------------------------------------------------- #
def add_media(title, imdb, settings):
ended_path = filesystem.join(addon_data_path(), imdb + '.ended')
if filesystem.exists(ended_path):
try:
filesystem.remove(ended_path)
except: pass
path = filesystem.join(addon_data_path(), 'add_media')
log.debug(path)
# if not filesystem.exists(path):
# with filesystem.fopen(path, 'w'):
# pass
if filesystem.exists(path):
with filesystem.fopen(path, 'r') as f:
s = f.read()
if imdb.encode('utf-8') in s:
return
with filesystem.fopen(path, 'a+') as f:
log.debug('writing...')
seq = [title.encode('utf-8') + '\n', imdb.encode('utf-8') + '\n']
f.writelines(seq)
import xbmcgui
from settings import _addon_name
class RemoteDialogProgress(xbmcgui.DialogProgressBG):
def __init__(self, *args, **kwargs):
self.progress_file_path = filesystem.join(addon_data_path(), '.'.join([imdb, 'progress']))
return super(RemoteDialogProgress, self).__init__(*args, **kwargs)
def Refresh(self):
if filesystem.exists(self.progress_file_path):
try:
try:
with filesystem.fopen(self.progress_file_path, 'r') as progress_file:
args = progress_file.read().split('\n')
except:
args = [0]
args[0] = int(args[0])
self.update(*args)
except: pass
info_dialog = RemoteDialogProgress()
info_dialog.create(_addon_name)
strm_path = filesystem.join(addon_data_path(), imdb + '.strm_path')
for cnt in range(300):
info_dialog.Refresh()
#---------------------------------
"""
if filesystem.exists(strm_path):
with filesystem.fopen(strm_path, 'r') as f:
source = f.read() # utf-8
if source and source.endswith('.strm'):
strm_path = filesystem.join(settings.base_path(), source)
if filesystem.exists(strm_path):
with filesystem.fopen(strm_path, 'r') as strm:
url = strm.read()
xbmc.executebuiltin('RunPlugin("%s")' % url)
"""
#---------------------------------
if filesystem.exists(ended_path):
with filesystem.fopen(ended_path, 'r') as f:
dlg = xbmcgui.Dialog()
count = f.read()
try:
count = int(count)
except ValueError: count = 0
if not xbmc.Player().isPlaying():
if count:
dlg.notification(_addon_name, u'"%s" добавлено в библиотеку, найдено %d источников.' % (title, count), time=10000)
xbmc.executebuiltin('Container.Refresh')
from plugin import RunPlugin
RunPlugin(action='add_media', title=title.encode('utf-8'),
imdb=imdb, strm=strm_path.encode('utf-8'),
norecursive=True)
else:
dlg.notification(_addon_name,
u'"%s" не добавлено в библиотеку, Источники не найдены.' % title,
time=10000)
try:
filesystem.remove(ended_path)
except: pass
break
sleep(1)
info_dialog.close()
# ------------------------------------------------------------------------------------------------------------------- #
def save_dbs():
path = filesystem.join(_addondir, 'dbversions')
with filesystem.save_make_chdir_context(path, 'service.save_dbs'):
for fn in filesystem.listdir(path):
filesystem.remove(fn)
log_dir = xbmc.translatePath('special://logpath').decode('utf-8')
log_path = filesystem.join(log_dir, 'kodi.log')
log.debug(log_path)
with filesystem.fopen(log_path, 'r') as lf:
for line in lf.readlines():
if 'Running database version' in line:
log.debug(line)
name = line.split(' ')[-1].strip('\r\n\t ').decode('utf-8')
with filesystem.fopen(name, 'w'):
pass
# ------------------------------------------------------------------------------------------------------------------- #
def create_mark_file():
import urllib2, shutil
path = filesystem.join(_addondir, 'version_latest')
if not filesystem.exists(path):
try:
with filesystem.fopen(path, 'w') as f:
f.write('test')
if filesystem.exists(path):
url = 'https://github.com/vadyur/script.media.aggregator/releases/download/ver_0.15.2/version_latest'
response = urllib2.urlopen(url)
log.debug(response.read())
except BaseException as e:
log.print_tb(e)
pass
# ------------------------------------------------------------------------------------------------------------------- #
if __name__ == '__main__':
try:
create_mark_file()
save_dbs()
except BaseException as e:
log.print_tb(e)
pass
main()