-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.py
147 lines (114 loc) · 3.35 KB
/
library.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
# xbmc
import xbmc, xbmcgui, xbmcaddon
# python standart library
import time
import socket
import json
import re
import traceback
import top
# application
from mythread import MyThread
from utilities import *
from cache import *
ABORT_REQUESTED = False
class RPCListener(MyThread):
def __init__(self, cache):
super(RPCListener, self).__init__()
self.cache = cache
self.sock = socket.socket()
self.sock.settimeout(5)
self.connected = False
self._stop = False
sleepTime = 100
t = time.time()
port = get_api_port()
while sleepTime<500 and (not self.connected or ABORT_REQUESTED or xbmc.abortRequested):
try:
self.sock.connect(("localhost", port))
except Exception, exc:
log('%0.2f %s' % (time.time() - t, str(exc)))
xbmc.sleep(int(sleepTime))
sleepTime *= 1.5
else:
self._log.info('Connected to %d' % port)
self.connected = True
self.sock.setblocking(True)
def process(self, data):
pass
def run(self):
global ABORT_REQUESTED
if not self.connected:
self._log.error('RPC Listener cannot run, there is not connection to xbmc')
return False
while not self._stop:
data = self.sock.recv(8192)
data = data.replace('}{', '},{')
datapack='[%s]' % data
# self._log.debug('SynopsiTV: {0}'.format(str(data)))
try:
data_json = json.loads(datapack)
except ValueError, e:
self._log.error('RPC ERROR:' + unicode(e))
self._log.error('RPC ERROR DATA:' + unicode(data))
continue
for request in data_json:
method = request.get("method")
if method == "System.OnQuit":
self._stop = True
ABORT_REQUESTED = True
break
else:
self.process(request)
self.sock.close()
self._log.info('Library thread end')
def process(self, data):
methodName = data['method'].replace('.', '_')
method = getattr(self, methodName, None)
if method == None:
self._log.warn('Unknown method: ' + methodName)
return
self._log.debug(str(data))
# Try to call that method
try:
method(data)
except:
self._log.error('Error in method "' + methodName + '"')
self._log.error(traceback.format_exc())
# http://wiki.xbmc.org/index.php?title=JSON-RPC_API/v4
class RPCListenerHandler(RPCListener):
"""
RPCListenerHandler defines event handler methods that are autotically called from parent class's RPCListener
"""
def __init__(self, cache):
super(RPCListenerHandler, self).__init__(cache)
def _xbmc_time2sec(self, time):
return time["hours"] * 3600 + time["minutes"] * 60 + time["seconds"] + time["milliseconds"] / 1000
# NOT USED NOW
def playerEvent(self, data):
self._log.debug(dump(data))
def VideoLibrary_OnUpdate(self, data):
i = data['params']['data']['item']
self.cache.addorupdate(i['type'], i['id'])
def VideoLibrary_OnRemove(self, data):
d = data['params']['data']
self.cache.remove(d['type'], d['id'])
def Player_OnPlay(self, data):
self.playerEvent(data)
def Player_OnStop(self, data):
self.playerEvent(data)
def Player_OnSeek(self, data):
position = self._xbmc_time2sec(data['params']['data']['player']['time'])
top.player.playerEventSeek(position)
def Player_OnPause(self, data):
self.playerEvent(data)
pass
def Player_OnResume(self, data):
self.playerEvent(data)
pass
def GUI_OnScreenSaverActivated(self, data):
self.playerEvent(data)
pass
def GUI_OnScreenSaverDeactivated(self, data):
self.playerEvent(data)
pass