-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_old.py
188 lines (158 loc) · 6.09 KB
/
service_old.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
# -*- coding: utf-8 -*-
# import xbmcgui
from urlparse import parse_qsl
import threading
import urllib
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import kodiservice
server = None
class MyHandler(BaseHTTPRequestHandler):
global server
API = None
def do_GET(self):
import control
try:
if 'channel' in self.path:
req = self.path.replace('/channel?', '')
params = dict(parse_qsl(req))
print(params)
ch = kodiservice.getchannel(control.profile, params['id'], control.db)
self.send_response(301)
self.send_header('Location', ch.source)
self.end_headers()
self.finish()
elif 'playlist' in self.path:
m3u = kodiservice.getm3u(control.profile, control.db)
self.send_response(200)
self.send_header('Content-type', 'application/x-mpegURL')
self.send_header('Connection', 'close')
self.send_header('Content-Length', len(m3u))
self.end_headers()
self.wfile.write(m3u)
self.finish()
elif 'epg' in self.path:
self.send_response(301)
self.send_header('Location', control.epg_url)
self.end_headers()
self.finish()
elif 'stop' in self.path:
msg = 'Stopping ...'
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Connection', 'close')
self.send_header('Content-Length', len(msg))
self.end_headers()
self.wfile.write(msg.encode('utf-8'))
server.socket.close()
elif 'online' in self.path:
msg = 'Yes. I am.'
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Connection', 'close')
self.send_header('Content-Length', len(msg))
self.end_headers()
self.wfile.write(msg.encode('utf-8'))
except Exception as e:
control.sendError(str(e))
print('ToyaGO PVR: ' + str(e))
# xbmcgui.Dialog().notification("ToyaGO PVR", str(e), xbmcgui.NOTIFICATION_ERROR);
class AsyncCall(object):
def __init__(self, fnc, callback=None):
self.Callable = fnc
self.Callback = callback
def __call__(self, *args, **kwargs):
self.Thread = threading.Thread(target=self.run, name=self.Callable.__name__, args=args, kwargs=kwargs)
self.Thread.start()
return self
def wait(self, timeout=None):
self.Thread.join(timeout)
if self.Thread.isAlive():
raise TimeoutError()
else:
return self.Result
def run(self, *args, **kwargs):
self.Result = self.Callable(*args, **kwargs)
if self.Callback:
self.Callback(self.Result)
class AsyncMethod(object):
def __init__(self, fnc, callback=None):
self.Callable = fnc
self.Callback = callback
def __call__(self, *args, **kwargs):
return AsyncCall(self.Callable, self.Callback)(*args, **kwargs)
def Async(fnc=None, callback=None):
if fnc == None:
def AddAsyncCallback(fnc):
return AsyncMethod(fnc, callback)
return AddAsyncCallback
else:
return AsyncMethod(fnc, callback)
@Async
def startServer():
global server;
import control
server_enable = control.server_enable;
if server_enable == 'true':
port = int(control.server_port);
try:
server = SocketServer.TCPServer(('', port), MyHandler);
server.serve_forever();
except KeyboardInterrupt:
if server != None:
server.socket.close();
def stopServer():
import control
port = control.server_port;
try:
url = urllib.urlopen('http://localhost:' + str(port) + '/stop');
code = url.getcode();
except Exception as e:
control.sendError(str(e))
print('ToyaGO PVR: ' + str(e))
# xbmcgui.Dialog().notification("ToyaGO PVR", str(e), xbmcgui.NOTIFICATION_ERROR);
return;
def serverOnline():
import control
port = control.server_port;
try:
url = urllib.urlopen('http://localhost:' + str(port) + '/online');
code = url.getcode();
if code == 200:
return True;
except Exception as e:
return False;
return False;
def keepSession():
sessionThread = threading.Thread(target=kodiservice.keepSession) # , args=(10,)
sessionThread.daemon = True
sessionThread.start()
def getepg(profile, xmltv_dir, db, intepg):
if 'true' in intepg:
kodiservice.updateepg(profile, db)
kodiservice.createxmltv(profile, xmltv_dir, db)
kodiservice.updateepgdetails(profile, db)
kodiservice.createxmltv(profile, xmltv_dir, db)
else:
kodiservice.internetepg(profile, xmltv_dir)
if __name__ == '__main__':
import control
# control.runCreator()
kodiservice.initdb(control.profile, control.db)
kodiservice.updatechannels(control.deviceid, control.user, control.password, control.token, control.profile, control.db)
# kodiservice.createm3u(control.profile, control.m3u_dir, control.db)
if control.server_enable:
startServer()
kodiservice.createm3u(control.profile, control.m3u_dir, control.db)
try:
kodiservice.internetepg(control.profile, control.xmltv_dir)
# kodiservice.updateepg(control.profile, control.db)
# kodiservice.createxmltv(control.profile, control.xmltv_dir, control.db)
# kodiservice.updateepgdetails(control.profile, control.db)
# kodiservice.createxmltv(control.profile, control.xmltv_dir, control.db)
except Exception as e:
print('EPG Process Error: ' + str(e))
control.sendError('EPG Process Error')
# control.sendInfo('Before Keep Session Process')
keepSession()
# control.sendInfo('After Keep Session Process')