-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.py
238 lines (192 loc) · 7.54 KB
/
addon.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
import sys
import urlparse
import xbmc
import xbmcaddon
import urllib
import os
import subprocess
import json
__id__ = 'script.audio.scplayer'
__addon__ = xbmcaddon.Addon(__id__)
__language__ = __addon__.getLocalizedString
__settings__ = __addon__.getSetting
__cwd__ = xbmc.translatePath( __addon__.getAddonInfo('path') ).decode("utf-8")
__resource__ = xbmc.translatePath( os.path.join( __cwd__, 'resources', 'lib' ) ).decode("utf-8")
sys.path.append (__resource__)
import xbmcgui
from SpotifyIntegration import SpotifyIntegration
from ShufflePlaybackController import ShufflePlaybackController
from ShufflePlaybackWindow import ShufflePlaybackWindow
import traceback
import websocket
import threading
def log(msg, level = xbmc.LOGWARNING):
xbmc.log((u"### [%s] - %s" % (__baseUrl__,msg,)).encode('utf-8'),level=level )
def buildBaseUrl(query):
if typeOfCall == 'addon':
return __baseUrl__ + '?' + urllib.urlencode(query)
elif typeOfCall == 'service':
baseUrl = __baseUrl__
for key in query:
baseUrl = baseUrl + ',' + query.get(key)
return baseUrl
typeOfCall = 'addon'
addon_handle = None
args = None
ws = None
attempts = 0
wsconnected = False
wsp = threading.Event()
si = None
playbackController = None
playbackWindow = None
wsthread = None
def stop_screensaver(arg1, stop_event):
while (not stop_event.is_set()):
stop_event.wait(60)
if __settings__('32015') == 'true':
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Input.ContextMenu", "id": 1}')
def websocket_ping(arg1, stop_event, ws):
while (not stop_event.is_set()):
stop_event.wait(3)
ws.send(json.dumps({'action':'ping'}).encode('utf-8'))
def websocket_running(arg1, stop_event, ws):
ws.run_forever();
def on_data(ws, data, type, f):
global playbackController
if data is not None:
jsonData = json.loads(data)
if jsonData.get('action') <> 'pong':
print jsonData
playbackController.playback_notify(jsonData.get('action'), jsonData.get('data'))
def on_error(ws, error):
global wsconnected
print 'error'
print error
wsconnected = False
def on_close(ws):
global wsconnected
#print "### closed ###"
wsconnected = False
def on_open(ws):
global wsconnected
global wsp
global si
#print 'OPENED'
wspingthread = threading.Thread(target = websocket_ping, args=(1, wsp, ws))
wspingthread.start()
si.initialize()
wsconnected = True
def initWebSocket():
global ws
global wsp
global si
global wsthread
#print 'initWebSocket'
ws = websocket.WebSocketApp('ws://' + __settings__('hostname') + ':' + __settings__('port') + '/', subprotocols=["binary", "base64"]);
wsthread = threading.Thread(target = websocket_running, args=(1, wsp, ws))
wsthread.start()
si = SpotifyIntegration(ws)
if playbackWindow is not None:
playbackWindow.setSpotifyIntegration(si)
ws.on_data = on_data
ws.on_error = on_error
ws.on_close = on_close
ws.on_open = on_open
return ws
def verifyState(arg1, stop_event):
global attempts
global wsconnected
global ws
global playbackWindow
wsi = None
#print 'verifyState'
while attempts < 6 and (stop_event is None or not stop_event.is_set()):
#print 'verifyState WHILE'
if stop_event is not None:
stop_event.wait(5)
else:
if wsconnected == True:
break
else:
xbmc.sleep(5000)
#print 'verifyState BEFORE IF CONNECTED'
if wsconnected == False:
#print 'verifyState NOT CONNECTED, TRYING TO CONNECT'
wsi = initWebSocket()
#print 'verifyState ADD ATTEMPTS'
attempts = attempts + 1
else:
#print 'verifyState ATTEMPTS 0'
attempts = 0
#print 'verifyState END WHILE'
if wsconnected == False:
xbmcgui.Dialog().ok(__language__(32011), line1=__language__(32012), line2=__language__(32013))
if stop_event is not None:
playbackWindow.close()
return wsi
def intialize_window_data(arg1, stop_event):
global si
stop_event.wait(1)
si.initialize()
__baseUrl__ = sys.argv[0]
if ('plugin://' not in __baseUrl__):
typeOfCall = 'service'
if __name__ == '__main__':
if typeOfCall == 'addon':
addon_handle = int(sys.argv[1])
args = urlparse.parse_qs(sys.argv[2][1:])
elif typeOfCall == 'service':
args = sys.argv[1:]
try:
ar = threading.Event()
thread = threading.Thread(target = stop_screensaver, args=(1, ar))
thread.setDaemon(True)
thread.start()
p = None
try:
if __settings__('runasservice') == 'false':
args = [__cwd__ + '/resources/jdk1.8.0_60/bin/java', '-Xdebug', '-Xrunjdwp:server=y,transport=dt_socket,address=45912,suspend=n']
if __settings__('debug') is not None and __settings__('debug') == 'true':
args.append('-Ddebug=3')
if __settings__('spotifyapppath') is not None and __settings__('spotifyapppath') <> '':
args.append('-Dapp.key=' + __settings__('spotifyapppath'))
args.append('-Dusername=' + __settings__('username'))
args.append('-Dpassword=' + __settings__('password'))
args.append('-Dremember.me=' + __settings__('rememberme'))
args.append('-Dplayer.name=' + __settings__('playername'))
args.append('-jar')
args.append(__cwd__ + '/resources/SCPlayer.jar')
for arg in args:
print arg
p = subprocess.Popen(args, cwd=__cwd__ + '/resources/')
print 'SCPlayer PID : ' + str(p.pid)
pDialog = xbmcgui.DialogProgress()
pDialog.create('SCPlayer', __language__(32014))
pDialog.update(percent=50)
ws = verifyState(1, None)
if wsconnected == True:
pDialog.close()
wsverifythread = threading.Thread(target = verifyState, args=(1, wsp))
wsverifythread.setDaemon(True)
wsverifythread.start()
playbackWindow = ShufflePlaybackWindow("shuffle-playback-window.xml", __cwd__, spotifyIntegration=si)
playbackController = ShufflePlaybackController(playbackWindow)
wdthread = threading.Thread(target = intialize_window_data, args=(1, wsp))
wdthread.start()
playbackWindow.doModal()
ar.set()
wsp.set()
del playbackWindow
del playbackController
else:
pDialog.close()
if __settings__('runasservice') == 'false':
p.terminate()
except OSError:
log('Problem loading Core Player', xbmc.LOGFATAL)
pDialog.close()
sys.exit(-1)
del thread
except:
log("Exception Details: %s" % traceback.format_exc(), xbmc.LOGERROR)