-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathservice.py
More file actions
94 lines (76 loc) · 3.08 KB
/
service.py
File metadata and controls
94 lines (76 loc) · 3.08 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import threading
from kodi_six import xbmc, xbmcgui
from .logging import Log
from .configs import getConfig, writeConfig
from .common import Settings
from .proxy import ProxyTCPD
class BackgroundService(xbmc.Monitor):
def __init__(self):
super(BackgroundService, self).__init__()
self._s = Settings()
self.freqCheck = 60
self.freqExport = 86400 # 24 * 60 * 60 seconds
self.lastCheck = 0
self.lastExport = float(getConfig('last_wl_export', '0'))
self.wl_export = self._s.wl_export
self.proxy = ProxyTCPD(self._s)
writeConfig('loginlock', '')
writeConfig('proxyaddress', '127.0.0.1:{}'.format(self.proxy.port))
Log('Service: Proxy bound to {}'.format(self._s.proxyaddress))
self.proxy_thread = threading.Thread(target=self.proxy.serve_forever)
def onSettingsChanged(self):
super(BackgroundService, self).__init__()
xbmc.sleep(500)
self.wl_export = self._s.wl_export
def start(self):
self.proxy.server_activate()
self.proxy.timeout = 1
self.proxy_thread.start()
Log('Service: Proxy server started')
from time import time
Log('Service started')
while not self.abortRequested():
ct = time()
if (ct >= (self.lastCheck + self.freqCheck)) and self.wl_export:
self.lastCheck = ct
self.export_watchlist(ct)
if self.waitForAbort(5):
break
Log('Service stopped')
self.stop()
def stop(self):
self.proxy.shutdown()
self.proxy.server_close()
self.proxy_thread.join()
Log('Service: Proxy server stopped')
def export_watchlist(self, cur_time=0):
""" Export the watchlist every self.freqExport seconds """
if cur_time >= (self.freqExport + self.lastExport):
Log('Service: Exporting the Watchlist')
self.lastExport = cur_time
xbmc.executebuiltin('RunPlugin(plugin://plugin.video.amazon-test/?mode=exportWatchlist)')
class SettingsMonitor(xbmc.Monitor):
def __init__(self):
super(SettingsMonitor, self).__init__()
self.mon_prop = 'amazon_settingsmonitor_state'
self.window = xbmcgui.Window(10000)
self._abort = False
def onSettingsChanged(self):
super(SettingsMonitor, self).__init__()
Log('Service: Settings changed', Log.DEBUG)
xbmc.sleep(500)
self._abort = True
def start(self):
if self.window.getProperty(self.mon_prop) == '1':
return
self.window.setProperty(self.mon_prop, '1')
Log('Service: Settings Monitor started ', Log.DEBUG)
while not self.abortRequested() and not self._abort:
if self.waitForAbort(2):
break
self.window.setProperty(self.mon_prop, '')
Log('Service: Settings Monitor stopped ', Log.DEBUG)
exit()