forked from MediaBrowser/MediaBrowser.Kodi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
363 lines (293 loc) · 13.5 KB
/
service.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
import xbmc
import xbmcgui
import xbmcaddon
import urllib
import httplib
import os
import time
import requests
import socket
import threading
import json
from datetime import datetime
import xml.etree.ElementTree as xml
import mimetypes
from threading import Thread
from urlparse import parse_qs
from urllib import urlretrieve
from random import randint
import random
import urllib2
__cwd__ = xbmcaddon.Addon(id='plugin.video.xbmb3c').getAddonInfo('path')
__addon__ = xbmcaddon.Addon(id='plugin.video.xbmb3c')
__language__ = __addon__.getLocalizedString
BASE_RESOURCE_PATH = xbmc.translatePath( os.path.join( __cwd__, 'resources', 'lib' ) )
sys.path.append(BASE_RESOURCE_PATH)
base_window = xbmcgui.Window( 10000 )
from InfoUpdater import InfoUpdaterThread
from NextUpItems import NextUpUpdaterThread
from SuggestedItems import SuggestedUpdaterThread
from RandomItems import RandomInfoUpdaterThread
from ArtworkLoader import ArtworkRotationThread
from ThemeMusic import ThemeMusicThread
from RecentItems import RecentInfoUpdaterThread
from InProgressItems import InProgressUpdaterThread
from WebSocketClient import WebSocketThread
from ClientInformation import ClientInformation
from MenuLoad import LoadMenuOptionsThread
from ImageProxy import MyHandler
from ImageProxy import ThreadingHTTPServer
_MODE_BASICPLAY=12
def getAuthHeader():
addonSettings = xbmcaddon.Addon(id='plugin.video.xbmb3c')
deviceName = addonSettings.getSetting('deviceName')
deviceName = deviceName.replace("\"", "_") # might need to url encode this as it is getting added to the header and is user entered data
clientInfo = ClientInformation()
txt_mac = clientInfo.getMachineId()
version = clientInfo.getVersion()
userid = xbmcgui.Window( 10000 ).getProperty("userid")
authString = "MediaBrowser UserId=\"" + userid + "\",Client=\"XBMC\",Device=\"" + deviceName + "\",DeviceId=\"" + txt_mac + "\",Version=\"" + version + "\""
headers = {'Accept-encoding': 'gzip', 'Authorization' : authString}
xbmc.log("XBMB3C Authentication Header : " + str(headers))
return headers
# start some worker threads
newInProgressThread = None
if __addon__.getSetting('useInProgressUpdater') == "true":
newInProgressThread = InProgressUpdaterThread()
newInProgressThread.start()
else:
xbmc.log("XBMB3C Service InProgressUpdater Disabled")
newRecentInfoThread = None
if __addon__.getSetting('useRecentInfoUpdater') == "true":
newRecentInfoThread = RecentInfoUpdaterThread()
newRecentInfoThread.start()
else:
xbmc.log("XBMB3C Service RecentInfoUpdater Disabled")
newRandomInfoThread = None
if __addon__.getSetting('useRandomInfo') == "true":
newRandomInfoThread = RandomInfoUpdaterThread()
newRandomInfoThread.start()
else:
xbmc.log("XBMB3C Service RandomInfo Disabled")
newNextUpThread = None
if __addon__.getSetting('useNextUp') == "true":
newNextUpThread = NextUpUpdaterThread()
newNextUpThread.start()
else:
xbmc.log("XBMB3C Service NextUp Disabled")
newSuggestedThread = None
if __addon__.getSetting('useSuggested') == "true":
newSuggestedThread = SuggestedUpdaterThread()
newSuggestedThread.start()
else:
xbmc.log("XBMB3C Service Suggested Disabled")
newWebSocketThread = None
if __addon__.getSetting('useWebSocketRemote') == "true":
newWebSocketThread = WebSocketThread()
newWebSocketThread.start()
else:
xbmc.log("XBMB3C Service WebSocketRemote Disabled")
newMenuThread = None
if __addon__.getSetting('useMenuLoader') == "true":
newMenuThread = LoadMenuOptionsThread()
newMenuThread.start()
else:
xbmc.log("XBMB3C Service MenuLoader Disabled")
artworkRotationThread = None
if __addon__.getSetting('useBackgroundLoader') == "true":
artworkRotationThread = ArtworkRotationThread()
artworkRotationThread.start()
else:
xbmc.log("XBMB3C Service BackgroundLoader Disabled")
newThemeMusicThread = None
if __addon__.getSetting('useThemeMusic') == "true":
newThemeMusicThread = ThemeMusicThread()
newThemeMusicThread.start()
else:
xbmc.log("XBMB3C Service ThemeMusic Disabled")
newInfoThread = None
if __addon__.getSetting('useInfoLoader') == "true":
newInfoThread = InfoUpdaterThread()
newInfoThread.start()
else:
xbmc.log("XBMB3C Service InfoLoader Disabled")
###############################################
# start the image proxy server
###############################################
keepServing = True
def startImageProxyServer():
xbmc.log("XBMB3 -> HTTP Image Proxy Server Starting")
server = ThreadingHTTPServer(("",15001), MyHandler)
while (keepServing):
server.handle_request()
xbmc.log("XBMB3 -> HTTP Image Proxy Server EXITING")
Thread(target=startImageProxyServer).start()
def deleteItem (url):
return_value = xbmcgui.Dialog().yesno(__language__(30091),__language__(30092))
if return_value:
xbmc.log('Deleting via URL: ' + url)
progress = xbmcgui.DialogProgress()
progress.create(__language__(30052), __language__(30053))
resp = requests.delete(url, data='', headers=getAuthHeader())
deleteSleep=0
while deleteSleep<10:
xbmc.sleep(1000)
deleteSleep=deleteSleep+1
progress.update(deleteSleep*10,__language__(30053))
progress.close()
xbmc.executebuiltin("Container.Refresh")
return 1
else:
return 0
def markWatched(url):
xbmc.log('XBMB3C Service -> Marking watched via: ' + url)
resp = requests.post(url, data='', headers=getAuthHeader())
def markUnWatched(url):
xbmc.log('XBMB3C Service -> Marking watched via: ' + url)
resp = requests.delete(url, data='', headers=getAuthHeader())
def setPosition (url, method):
xbmc.log('XBMB3C Service -> Setting position via: ' + url)
if method == 'POST':
resp = requests.post(url, data='', headers=getAuthHeader())
elif method == 'DELETE':
resp = requests.delete(url, data='', headers=getAuthHeader())
def stopTranscoding(url):
xbmc.log('XBMB3C Service -> Stopping transcoding: ' + url)
resp = requests.delete(url, data='', headers=getAuthHeader())
def hasData(data):
if(data == None or len(data) == 0 or data == "None"):
return False
else:
return True
def stopAll(played_information):
if(len(played_information) == 0):
return
addonSettings = xbmcaddon.Addon(id='plugin.video.xbmb3c')
xbmc.log ("XBMB3C Service -> played_information : " + str(played_information))
for item_url in played_information:
data = played_information.get(item_url)
if(data != None):
xbmc.log ("XBMB3C Service -> item_url : " + item_url)
xbmc.log ("XBMB3C Service -> item_data : " + str(data))
watchedurl = data.get("watchedurl")
positionurl = data.get("positionurl")
deleteurl = data.get("deleteurl")
runtime = data.get("runtime")
currentPossition = data.get("currentPossition")
item_id = data.get("item_id")
if(currentPossition != None and hasData(runtime) and hasData(positionurl) and hasData(watchedurl)):
runtimeTicks = int(runtime)
xbmc.log ("XBMB3C Service -> runtimeticks:" + str(runtimeTicks))
percentComplete = (currentPossition * 10000000) / runtimeTicks
markPlayedAt = float(addonSettings.getSetting("markPlayedAt")) / 100
xbmc.log ("XBMB3C Service -> Percent Complete:" + str(percentComplete) + " Mark Played At:" + str(markPlayedAt))
if (percentComplete > markPlayedAt):
gotDeleted = 0
if(deleteurl != None and deleteurl != ""):
xbmc.log ("XBMB3C Service -> Offering Delete:" + str(deleteurl))
gotDeleted = deleteItem(deleteurl)
if(gotDeleted == 0):
setPosition(positionurl + '/Progress?PositionTicks=0', 'POST')
if(newWebSocketThread != None):
newWebSocketThread.playbackStopped(item_id, str(0))
markWatched(watchedurl)
else:
#markUnWatched(watchedurl) # this resets the LastPlayedDate and that causes issues with sortby PlayedDate so I removed it for now
if(newWebSocketThread != None):
newWebSocketThread.playbackStopped(item_id, str(int(currentPossition * 10000000)))
setPosition(positionurl + '?PositionTicks=' + str(int(currentPossition * 10000000)), 'DELETE')
if(newNextUpThread != None):
newNextUpThread.updateNextUp()
if(artworkRotationThread != None):
artworkRotationThread.updateActionUrls()
played_information.clear()
# stop transcoding - todo check we are actually transcoding?
clientInfo = ClientInformation()
txt_mac = clientInfo.getMachineId()
url = ("http://%s:%s/mediabrowser/Videos/ActiveEncodings" % (addonSettings.getSetting('ipaddress'), addonSettings.getSetting('port')))
url = url + '?DeviceId=' + txt_mac
stopTranscoding(url)
class Service( xbmc.Player ):
played_information = {}
def __init__( self, *args ):
xbmc.log("XBMB3C Service -> starting monitor service")
self.played_information = {}
pass
def onPlayBackStarted( self ):
# Will be called when xbmc starts playing a file
stopAll(self.played_information)
currentFile = xbmc.Player().getPlayingFile()
xbmc.log("XBMB3C Service -> onPlayBackStarted" + currentFile)
WINDOW = xbmcgui.Window( 10000 )
watchedurl = WINDOW.getProperty(currentFile+"watchedurl")
deleteurl = WINDOW.getProperty(currentFile+"deleteurl")
positionurl = WINDOW.getProperty(currentFile+"positionurl")
runtime = WINDOW.getProperty(currentFile+"runtimeticks")
item_id = WINDOW.getProperty(currentFile+"item_id")
# reset all these so they dont get used is xbmc plays a none
# xbmb3c MB item
# WINDOW.setProperty(currentFile+"watchedurl", "")
# WINDOW.setProperty(currentFile+"deleteurl", "")
# WINDOW.setProperty(currentFile+"positionurl", "")
# WINDOW.setProperty(currentFile+"runtimeticks", "")
# WINDOW.setProperty(currentFile+"item_id", "")
if(item_id == None or len(item_id) == 0):
return
if(newWebSocketThread != None):
newWebSocketThread.playbackStarted(item_id)
if (watchedurl != "" and positionurl != ""):
data = {}
data["watchedurl"] = watchedurl
data["deleteurl"] = deleteurl
data["positionurl"] = positionurl
data["runtime"] = runtime
data["item_id"] = item_id
self.played_information[currentFile] = data
xbmc.log("XBMB3C Service -> ADDING_FILE : " + currentFile)
xbmc.log("XBMB3C Service -> ADDING_FILE : " + str(self.played_information))
# reset in progress possition
setPosition(positionurl + '/Progress?PositionTicks=0', 'POST')
def onPlayBackEnded( self ):
# Will be called when xbmc stops playing a file
xbmc.log("XBMB3C Service -> onPlayBackEnded")
stopAll(self.played_information)
def onPlayBackStopped( self ):
# Will be called when user stops xbmc playing a file
xbmc.log("XBMB3C Service -> onPlayBackStopped")
stopAll(self.played_information)
monitor = Service()
lastProgressUpdate = datetime.today()
addonSettings = xbmcaddon.Addon(id='plugin.video.xbmb3c')
if socket.gethostname() != None and socket.gethostname() != '' and addonSettings.getSetting("deviceName") == 'XBMB3C':
addonSettings.setSetting("deviceName", socket.gethostname())
while not xbmc.abortRequested:
if xbmc.Player().isPlaying():
try:
playTime = xbmc.Player().getTime()
currentFile = xbmc.Player().getPlayingFile()
if(monitor.played_information.get(currentFile) != None):
monitor.played_information[currentFile]["currentPossition"] = playTime
# send update
td = datetime.today() - lastProgressUpdate
secDiff = td.seconds
if(secDiff > 10):
if(monitor.played_information.get(currentFile) != None and monitor.played_information.get(currentFile).get("item_id") != None):
item_id = monitor.played_information.get(currentFile).get("item_id")
if(newWebSocketThread != None):
newWebSocketThread.sendProgressUpdate(item_id, str(int(playTime * 10000000)))
lastProgressUpdate = datetime.today()
except Exception, e:
xbmc.log("XBMB3C Service -> Exception in Playback Monitor : " + str(e))
pass
xbmc.sleep(1000)
xbmcgui.Window(10000).setProperty("XBMB3C_Service_Timestamp", str(int(time.time())))
# stop the WebSocket client
if(newWebSocketThread != None):
newWebSocketThread.stopClient()
# stop the image proxy
keepServing = False
try:
requesthandle = urllib.urlopen("http://localhost:15001/?id=dummy&type=Primary", proxies={})
except:
xbmc.log("XBMB3C Service -> Tried to stop image proxy server but it was already stopped")
xbmc.log("XBMB3C Service -> Service shutting down")