forked from ruuk/script.module.youtube.dl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
67 lines (52 loc) · 1.89 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
# -*- coding: utf-8 -*-
import sys
import json
import binascii
import xbmc
from lib.yd_private_libs import util, servicecontrol, jsonqueue
sys.path.insert(0, util.MODULE_PATH)
import YDStreamExtractor # noqa E402
import threading # noqa E402
import AddonSignals
class Service():
def __init__(self):
self.downloadCount = 0
self.controller = servicecontrol.ServiceControl()
AddonSignals.registerSlot('script.module.youtube.dl', 'DOWNLOAD_STOP', self.stopDownload)
self.start()
def stopDownload(self, args):
YDStreamExtractor._cancelDownload()
def getNextQueuedDownload(self):
try:
dataHEX = jsonqueue.XBMCJsonRAFifoQueue(util.QUEUE_FILE).pop()
if not dataHEX:
return None
dataJSON = binascii.unhexlify(dataHEX)
self.downloadCount += 1
util.LOG('Loading from queue. #{0} this session'.format(self.downloadCount))
return json.loads(dataJSON)
except:
import traceback
traceback.print_exc()
return None
def start(self):
if self.controller.status == 'ACTIVE':
return
try:
self.controller.status = 'ACTIVE'
self._start()
finally:
self.controller.status = ''
def _start(self):
util.LOG('DOWNLOAD SERVICE: START')
info = self.getNextQueuedDownload()
monitor = xbmc.Monitor()
while info and not monitor.abortRequested():
t = threading.Thread(target=YDStreamExtractor._handleDownload, args=(
info['data'],), kwargs={'path': info['path'], 'duration': info['duration'], 'bg': True})
t.start()
while t.isAlive() and not monitor.abortRequested():
xbmc.sleep(100)
info = self.getNextQueuedDownload()
util.LOG('DOWNLOAD SERVICE: FINISHED')
Service()