-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
630 lines (574 loc) · 19.3 KB
/
server.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#!/usr/bin/env python
#encoding:utf-8
from __future__ import unicode_literals
import sys
import os
import hashlib
import shutil
import json
import jsonschema
import cherrypy
from cherrypy.lib import auth_digest
import threading
import datetime
import tzlocal
import tempfile
import time
import JSAG3
import Downloader
import torrentSearch
import Transferer
import Notificator
import tvShowList
import myTvDB
import ActivityLog
import utils.TSWdirectories
import ConfTest
import Server
from cherrypy.process.plugins import Daemonizer
from cherrypy.process.plugins import PIDFile
curPath = os.path.dirname(os.path.realpath(__file__))
directories = utils.TSWdirectories(curPath+'/utils/directory.conf')
tmpPath = os.path.abspath(directories['tmp_path'])
PIDFile(cherrypy.engine, tmpPath + '/TSW2.PID').subscribe()
threadCollection=[]
def buffyThreadSlayer():
for t in threadCollection:
if not t.isAlive():
t.join()
testPath = unicode(tempfile.mkdtemp())
def _stop():
print("Removing {0}".format(testPath))
shutil.rmtree(testPath)
cherrypy.engine.signal_handler.set_handler(signal=15,listener=_stop)
cherrypy.engine.signal_handler.subscribe()
class Root(object):
@cherrypy.expose
def default(self):
return ''
@cherrypy.popargs('tvshow')
class TvShow(object):
def __init__(self,tvshowlist,downloader,searcher):
self.tvshowlist = tvshowlist
self.downloader=downloader
self.searcher=searcher
def _error(self,errorNo,errorDesc):
return {"status":errorNo,"error":errorDesc.encode("utf8")}
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self,tvshow=None,**params):
if cherrypy.request.method == 'GET':
if tvshow is None:
return self._QUERY()
else:
return self._GET(tvshow)
elif cherrypy.request.method == 'POST':
return self._POST(tvshow=tvshow,**params)
elif cherrypy.request.method == 'PUT':
return self._PUT(tvshow=tvshow,**params)
elif cherrypy.request.method == 'DELETE':
return self._DELETE(tvshow=tvshow)
def _QUERY(self):
return {
"status":200,
"error":"TvShow list retrieved in `data`",
"data":json.loads(
json.dumps(
self.tvshowlist.getValue(hidePassword=True),
default=Server.json_serial
)
),
"debug":[]
}
def _GET(self,tvshow=None):
tvshow = int(tvshow)
tvshowschedule = self.tvshowlist.getTvShow(tvshow)
if tvshowschedule is None:
return {
"status":400,
"error":"TvShow {0} not scheduled".format(unicode(tvshow)),
"debug":[]
}
return {
"status":200,
"error":"TvShow list retrieved in `data`",
"data":json.loads(
json.dumps(
self.tvshowlist.getTvShow(tvshow).getValue(hidePassword=True),
default=Server.json_serial
)
),
"debug":[]
}
def _POST(self,tvshow=None,**kwargs):
if 'title' in kwargs.keys():
seriesname = kwargs['title']
if tvshow is not None:
serie = int(tvshow)
else:
serie = seriesname
else:
serie = unicode(kwargs)
seriesname = serie
t = myTvDB.myTvDB()
try:
tvShow = t[serie]
except Exception as e:
return self._error(400,e[0]+unicode(serie)+unicode(seriesname.lower()))
if tvShow.data['seriesname'].lower() == seriesname.lower():
try:
self.tvshowlist.add(int(serie))
self.tvshowlist.save()
tvShow = self.tvshowlist.getTvShow(int(serie))
except Exception as e:
return self._error(400,e[0])
return {"status":200,"error":"TvShow {0} added".format(seriesname),"data":json.loads(json.dumps(tvShow.getValue(),default=Server.json_serial))}
return {"status":401,"error":"Unknown TvShow: '{0}'".format(seriesname)}
def _PUT(self,tvshow=None, **kwargs):
print(kwargs)
if tvshow is not None:
tvShowID = int(tvshow)
nextUpdate=None
status = None
season = None
episode=None
pattern=None
emails=None
keywords=None
if 'season' in kwargs.keys() and 'episode' in kwargs.keys():
season = int(kwargs['season'])
episode = int(kwargs['episode'])
t = myTvDB.myTvDB()
try:
t[tvShowID]
except:
return {"status":400,"error":"TvShow {0} does not exist".format(unicode(tvShowID))}
try:
t[tvShowID][season][episode]
except:
return {"status":400,"error":"No episode S{1:02}E{2:02} for TV show {0}".format(t[tvShowID].data['seriesname'],unicode(season),unicode(episode))}
status = 0
if 'pattern' in kwargs.keys():
pattern = unicode(kwargs['pattern'])
if 'emails[]' in kwargs.keys():
if isinstance(kwargs['emails[]'],basestring):
emails = [kwargs['emails[]']]
else:
emails = kwargs['emails[]']
if 'keywords[]' in kwargs.keys():
if isinstance(kwargs['keywords[]'],basestring):
keywords = [kwargs['keywords[]']]
else:
keywords = kwargs['keywords[]']
if any(item in ['season','episode','keywords[]','pattern','force'] for item in kwargs.keys()):
nextUpdate = datetime.datetime.now(tzlocal.get_localzone())
tvShow = self.tvshowlist.getTvShow(tvShowID)
if tvShow is None:
raise Exception("{0} not found in {1}".format(tvShowID,list(self.tvshowlist)))
fname = ""
self.tvshowlist.lock.acquire()
try:
tvShow.set(
status=status,
season=season,
episode=episode,
nextUpdate=nextUpdate,
pattern=pattern,
emails=emails,
keywords=keywords
)
self.tvshowlist.save()
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
finally:
self.tvshowlist.lock.release()
if fname == "":
return {
"status":200,
"error":"TvShow {0} updated".format(
tvShow['info']['seriesname']
),
"data":json.loads(
json.dumps(
tvShow.getValue(),
default=Server.json_serial
)
)
}
else:
return self._error(400,"{0} - {1}:{2}".format(e[0],fname,exc_tb.tb_lineno))
else:
return self._error(400,"Unknown TV Show")
def _DELETE(self,tvshow=None):
if tvshow is not None:
try:
tvShowID = int(tvshow)
t = myTvDB.myTvDB()
tvShow = t[tvShowID]
self.tvshowlist.delete(tvShowID)
self.tvshowlist.save()
return {"status":200,"error":"TvShow {0} deleted".format(tvShow['seriesname'])}
except Exception as e:
return self._error(400,e[0])
else:
return self._error(400,"Unknown TV Show")
class serv_TvShowList(object):
def __init__(self,tvshowlist,downloader,searcher):
self.tvshowlist = tvshowlist
self.downloader=downloader
self.searcher=searcher
self.checkModification()
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self):
return {"status":400,"error":""}
def _error(self,errorNo,errorDesc):
return {"status":errorNo,"error":errorDesc.encode("utf8")}
def _add(self,tvShowID,tvShowName):
self.checkModification()
try:
self.tvshowlist.add(int(tvShowID))
self.tvshowlist.save()
tvShow = self.tvshowlist.getTvShow(int(tvShowID))
except Exception as e:
return self._error(400,e[0])
return {"status":200,"error":"TvShow {0} added".format(tvShowName),"data":json.loads(json.dumps(tvShow.getValue(),default=Server.json_serial))}
@cherrypy.expose
@cherrypy.tools.json_out()
def add(self, **kwargs):
self.checkModification()
if 'title' in kwargs.keys():
seriesname = kwargs['title']
if 'seriesid' in kwargs.keys():
serie = int(kwargs['seriesid'])
else:
serie = seriesname
else:
serie = unicode(kwargs)
seriesname = serie
t = myTvDB.myTvDB()
try:
tvShow = t[serie]
except Exception as e:
return self._error(400,e[0]+unicode(serie)+unicode(seriesname.lower()))
if tvShow.data['seriesname'].lower() == seriesname.lower():
#cherrypy.request.params['tvShowID'] = tvShow.data['id']
return self._add(tvShow.data['id'],tvShow.data['seriesname'])
return {"status":401,"error":"Unknown TvShow: '{0}'".format(seriesname)}
@cherrypy.expose
@cherrypy.tools.json_out()
def forceUpdate(self):
for tvShow in self.tvshowlist:
self.update(tvShowID=tvShow['seriesid'],force=True)
return {"status":200,"error":"All TvShows will be updated now"}
@cherrypy.expose
@cherrypy.tools.json_out()
def update(self, **kwargs):
if 'tvShowID' in kwargs.keys():
tvShowID = int(kwargs['tvShowID'])
nextUpdate=None
status = None
season = None
episode=None
pattern=None
emails=None
keywords=None
if 'season' in kwargs.keys() and 'episode' in kwargs.keys():
season = int(kwargs['season'])
episode = int(kwargs['episode'])
t = myTvDB.myTvDB()
try:
t[tvShowID]
except:
return {"status":400,"error":"TvShow {0} does not exist".format(unicode(tvShowID))}
try:
t[tvShowID][season][episode]
except:
return {"status":400,"error":"No episode S{1:02}E{2:02} for TV show {0}".format(t[tvShowID].data['seriesname'],unicode(season),unicode(episode))}
status = 0
if 'pattern' in kwargs.keys():
pattern = unicode(kwargs['pattern'])
if 'emails[]' in kwargs.keys():
if isinstance(kwargs['emails[]'],basestring):
emails = [kwargs['emails[]']]
else:
emails = kwargs['emails[]']
if 'keywords[]' in kwargs.keys():
if isinstance(kwargs['keywords[]'],basestring):
keywords = [kwargs['keywords[]']]
else:
keywords = kwargs['keywords[]']
if any(item in ['season','episode','keywords[]','pattern','force'] for item in kwargs.keys()):
nextUpdate = datetime.datetime.now(tzlocal.get_localzone())
tvShow = self.tvshowlist.getTvShow(tvShowID)
if tvShow is None:
raise Exception("{0} not found in {1}".format(tvShowID,list(self.tvshowlist)))
fname = ""
self.tvshowlist.lock.acquire()
try:
self.checkModification()
tvShow.set(
status=status,
season=season,
episode=episode,
nextUpdate=nextUpdate,
pattern=pattern,
emails=emails,
keywords=keywords
)
self.tvshowlist.save()
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
finally:
self.tvshowlist.lock.release()
if fname == "":
return {
"status":200,
"error":"TvShow {0} updated".format(
tvShow['info']['seriesname']
),
"data":json.loads(
json.dumps(
tvShow.getValue(),
default=Server.json_serial
)
)
}
else:
return self._error(400,"{0} - {1}:{2}".format(e[0],fname,exc_tb.tb_lineno))
else:
return self._error(400,"Unknown TV Show")
@cherrypy.expose
@cherrypy.tools.json_out()
def pushTorrent(self, **kwargs):
if 'tvShowID' in kwargs.keys() and self.tvshowlist.inList(int(kwargs['tvShowID'])):
tvShowID = int(kwargs['tvShowID'])
if 'torrentFile' in kwargs.keys():
if not isinstance(kwargs['torrentFile'],basestring):
tvShow = self.tvshowlist.getTvShow(int(tvShowID))
try:
tmpfile = tempfile.gettempdir() + '/out.torrent'
with open(tmpfile, 'wb') as f:
f.write(kwargs['torrentFile'].file.read())
tvShow.pushTorrent(filename=tmpfile,downloader=self.downloader)
return {"status":200,"error":"TvShow {0} updated".format(tvShow['info']['seriesname'])}
except Exception as e:
return self._error(400,e[0])
else:
return self._error(400,"Unable to upload file")
else:
return self._error(400,"No file provided")
else:
return self._error(400,"Unknown TV Show")
@cherrypy.expose
@cherrypy.tools.json_out()
def delete(self,tvShowID=-1):
self.checkModification()
try:
tvShowID = int(tvShowID)
t = myTvDB.myTvDB()
tvShow = t[tvShowID]
self.tvshowlist.delete(tvShowID)
self.tvshowlist.save()
return {"status":200,"error":"TvShow {0} deleted".format(tvShow['seriesname'])}
except Exception as e:
return self._error(400,e[0])
@cherrypy.expose
@cherrypy.tools.json_out()
def list(self):
self.checkModification()
return {"status":200,"error":"TvShow list retrieved in `data`","data":json.loads(json.dumps(self.tvshowlist.getValue(hidePassword=True),default=Server.json_serial)),"debug":[]}
def checkModification(self):
"""if not hasattr(self,"lastModified") or os.path.getmtime(self.tvshowlist.filename) != self.lastModified:
self.tvshowlist.addData(dataFile=curPath+"/series.json")
self.lastModified = os.path.getmtime(self.tvshowlist.filename)"""
@cherrypy.expose
def progression(self,tvShowID=-1):
tvShowID = int(tvShowID)
tvShow = self.tvshowlist.getTvShow(tvShowID)
if tvShow is None:
cherrypy.response.headers["Content-Type"] = "application/json"
return self._error(400,"Unknown TV Show")
cherrypy.response.headers["Content-Type"] = "text/event-stream"
cherrypy.response.headers['Cache-Control'] = 'no-cache'
cherrypy.response.headers['Connection'] = 'keep-alive'
def content():
yield "retry: 5000\r\n"
while True:
data = "Event: progression\r\ndata: " + json.dumps(tvShow.get_progression(self.downloader)) + "\n\n"
yield data
time.sleep(5)
return content()
progression._cp_config = {'response.stream': True, 'tools.encode.encoding':'utf-8'}
@cherrypy.expose
@cherrypy.tools.json_out()
def setDefaultKeywords(self,tvShowID=-1):
params = {'tvShowID':tvShowID}
keywords = self.searcher.data.setdefault('defaultKeywords',[])
params['keywords[]'] = keywords
return self.update(**params)
class updateData(object):
def __init__(self,config,testPath=None):
self.config = config
self.testPath = testPath
if testPath is not None:
className = self.config.__module__.split(".")[:-1]
self.className = '.'.join(className)
self.dataFile = os.path.join(testPath,"test.json")
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self,**params):
try:
self.config.updateData(json.loads(params['data'.encode('utf8')]))
if self.testPath is not None:
buffyThreadSlayer()
c=ConfTest.ConfTest(dataFile=self.dataFile,confFile=self.config.dataFile,resultPath=self.testPath,verbosity=True)
c.addTest('.'.join([self.className,'ConfTest']))
t = threading.Thread(target=c.runAll)
t.daemon=True
threadCollection.append(t)
t.start()
return {
"status":200,
"error":"Configuration updated!".encode("utf8"),
"id":params['id'.encode('utf8')],
"data":json.loads(params['data'.encode('utf8')])
}
except jsonschema.ValidationError as e:
return {"status":400,"error":"Form validation failed. The main cause of this error can be one of keywords is empty".encode("utf8")}
except Exception as e:
return {"status":400,"error":e[0].encode("utf8")}
@cherrypy.expose
@cherrypy.tools.json_out()
def confTest(self,**params):
if self.testPath is not None:
c=ConfTest.ConfTest(dataFile=self.dataFile,confFile=self.config.dataFile,resultPath=self.testPath,verbosity=True)
index = c.getTestIndex('.'.join([self.className,'ConfTest']))
if index is not None:
data = c['suite'][index]
filename = os.path.join(self.testPath,self.className)
if os.path.isfile(filename):
with open(filename,'r') as fd:
content = fd.read()
data["content"] = content
return json.loads(json.dumps(data,default=Server.json_serial))
class streamGetSeries(object):
def __init__(self,tvshowlist,downloader,testPath=None,testFiles=None):
self.tvshowlist = tvshowlist
self.downloader = downloader
self.testPath = testPath
self.testFiles = [] if testFiles is None else testFiles
self.testFilesMd5 = dict()
@cherrypy.expose
def index(self):
cherrypy.response.headers["Content-Type"] = "text/event-stream"
cherrypy.response.headers['Cache-Control'] = 'no-cache'
cherrypy.response.headers['Connection'] = 'keep-alive'
def content():
yield "retry: 10000\r\n"
while True:
for tvshow in self.tvshowlist:
data = 'id: progression\r\ndata: {\r\n'
data += 'data: "' + unicode(tvshow.seriesid) + '":' + unicode(tvshow.get_progression(self.downloader)) + "\r\n"
data += 'data: }\n\n'
yield data
#data = "id: server-time\r\ndata: " + time.ctime(os.path.getmtime(confPath+"/series.json")) + "\n\n"
data = "id: server-time\r\ndata: " + Server.md5sum(confPath+"/series.json") + "\n\n"
if self.testPath is not None:
for testFile in self.testFiles:
filename = os.path.join(self.testPath,testFile)
if os.path.isfile(filename):
if (testFile not in self.testFilesMd5.keys()
or self.testFilesMd5[testFile] != Server.md5sum(filename)):
data = "id: conf-test\r\ndata: " + testFile + "\n\n"
self.testFilesMd5[testFile] = Server.md5sum(filename)
yield data
time.sleep(10)
return content()
index._cp_config = {'response.stream': True, 'tools.encode.encoding':'utf-8'}
webPath = os.path.abspath(directories['web_path'])
confPath = os.path.abspath(directories['etc_path'])
#if __name__ == '__main__':
def main():
conf = {
"/".encode('utf8') : {
'tools.staticdir.on': True,
'tools.staticdir.root':webPath,
'tools.staticdir.dir': '.',
'tools.staticdir.index': 'index.html',
'tools.trailing_slash.on' : False,
'tools.caching.on' : False
},
"/status.json".encode('utf8'): {
"tools.staticfile.on": True,
"tools.staticfile.filename": webPath + "/tvShowSchedule/status.json"
},
'/favicon.ico'.encode('utf8'):
{
'tools.staticfile.on': True,
'tools.staticfile.filename': webPath + '/static/favicon.ico'
},
'/api/tvshow'.encode('utf8'):
{
#'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
}
}
torrentsearch = torrentSearch.torrentSearch("torrentSearch",dataFile=confPath+"/config.json",verbosity=False)
downloader = Downloader.Downloader("downloader",dataFile=confPath+"/config.json")
transferer = Transferer.Transferer("transferer",dataFile=confPath+"/config.json")
tvshowlist = tvShowList.tvShowList(id="tvShowList",tvShows=confPath+"/series.json",banner_dir=webPath+"/static",verbosity=False)
notificator = Notificator.Notificator(id="notificator",dataFile=confPath+"/config.json",verbosity=False)
activitylog = ActivityLog.ActivityLog(confPath+"/activityLog.db",verbosity=False)
root = Root()
root.update = Root()
root.livesearch = Server.LiveSearch()
root.tvshowlist = serv_TvShowList(tvshowlist=tvshowlist,downloader=downloader,searcher=torrentsearch)
root.activitylog = Server.ActivityLog(activitylog)
root.api = Root()
root.api.tvshow = TvShow(tvshowlist=tvshowlist,downloader=downloader,searcher=torrentsearch)
root.streamGetSeries = streamGetSeries(
tvshowlist=tvshowlist,
downloader=downloader,
testPath =testPath,
testFiles =['torrentSearch','Downloader']
)
root.users = Server.Users()
root.groups = Server.Groups()
cherrypy.config["tools.encode.on"] = True
cherrypy.config["tools.encode.encoding"] = "utf-8"
cherrypy.config['engine.autoreload_on'] = False
cherrypy.config['server.socket_port'] = 1205
cherrypy.config['server.socket_host'] = '0.0.0.0'.encode('utf8')
root = torrentsearch.getRoot(root)
conf = torrentsearch.getConf(conf)
root.update.torrentSearch = updateData(torrentsearch,testPath)
root = downloader.getRoot(root)
conf = downloader.getConf(conf)
root.update.downloader = updateData(downloader,testPath)
root = transferer.getRoot(root)
conf = transferer.getConf(conf)
root.update.transferer = updateData(transferer)
root = notificator.getRoot(root)
conf = notificator.getConf(conf)
root.update.notificator = updateData(notificator)
wd = cherrypy.process.plugins.BackgroundTask(
interval=10,
function=Server.backgoundProcess,
kwargs={
"tvshowlist":tvshowlist,
"downloader":downloader,
"transferer":transferer,
"searcher":torrentsearch,
"notificator":notificator,
"activitylog":activitylog,
"force":False
}
)
wd.start()
wd = cherrypy.process.plugins.BackgroundTask(
interval=1,
function=buffyThreadSlayer
)
cherrypy.quickstart(root,"/".encode('utf8'),conf)