-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmonitor.py
67 lines (56 loc) · 1.8 KB
/
monitor.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 os
import shutil
import threading
import xbmc
import xbmcvfs
import xbmcaddon
from http_server import get_http_server
try:
xbmc.translatePath = xbmcvfs.translatePath
except AttributeError:
pass
class BilibiliMonitor(xbmc.Monitor):
def __init__(self, *args, **kwargs):
self.addon_id = 'plugin.video.bili'
self._httpd_port = int(xbmcaddon.Addon(self.addon_id).getSetting('server_port'))
self._httpd_address = '0.0.0.0'
self.httpd = None
self.httpd_thread = None
self.start_httpd()
def start_httpd(self):
if not self.httpd:
self.httpd = get_http_server(address=self._httpd_address, port=self._httpd_port)
if self.httpd:
self.httpd_thread = threading.Thread(target=self.httpd.serve_forever)
self.httpd_thread.daemon = True
self.httpd_thread.start()
def shutdown_httpd(self):
if self.httpd:
self.httpd.shutdown()
self.httpd.socket.close()
self.httpd_thread.join()
self.httpd_thread = None
self.httpd = None
def restart_httpd(self):
self.shutdown_httpd()
self.start_httpd()
def remove_temp_dir(self):
try:
path = xbmc.translatePath('special://temp/%s' % self.addon_id).decode('utf-8')
except AttributeError:
path = xbmc.translatePath('special://temp/%s' % self.addon_id)
if os.path.isdir(path):
try:
xbmcvfs.rmdir(path, force=True)
except:
pass
if os.path.isdir(path):
try:
shutil.rmtree(path)
except:
pass
if os.path.isdir(path):
return False
else:
return True