forked from trakt/script.trakt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
587 lines (502 loc) · 21.2 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
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
# -*- coding: utf-8 -*-
import xbmc
import threading
from time import time
import queue
import globals
import utilities
from traktapi import traktAPI
from rating import rateMedia, rateOnTrakt
from scrobbler import Scrobbler
from tagging import Tagger
from sync import Sync
try:
import simplejson as json
except ImportError:
import json
class traktService:
scrobbler = None
tagger = None
updateTagsThread = None
watcher = None
syncThread = None
dispatchQueue = queue.SqliteQueue()
_interval = 10 * 60 # how often to send watching call
def __init__(self):
threading.Thread.name = 'trakt'
def _dispatchQueue(self, data):
utilities.Debug("Queuing for dispatch: %s" % data)
self.dispatchQueue.append(data)
def _dispatch(self, data):
utilities.Debug("Dispatch: %s" % data)
action = data['action']
if action == 'started':
del data['action']
self.scrobbler.playbackStarted(data)
self.watcher = threading.Timer(self._interval, self.doWatching)
self.watcher.name = "trakt-watching"
self.watcher.start()
elif action == 'ended' or action == 'stopped':
self.scrobbler.playbackEnded()
if self.watcher:
if self.watcher.isAlive():
self.watcher.cancel()
self.watcher = None
elif action == 'paused':
self.scrobbler.playbackPaused()
elif action == 'resumed':
self.scrobbler.playbackResumed()
elif action == 'seek' or action == 'seekchapter':
self.scrobbler.playbackSeek()
elif action == 'databaseUpdated':
if utilities.getSettingAsBool('sync_on_update'):
utilities.Debug("Performing sync after library update.")
self.doSync()
elif action == 'scanStarted':
pass
elif action == 'settingsChanged':
utilities.Debug("Settings changed, reloading.")
globals.traktapi.updateSettings()
self.tagger.updateSettings()
elif action == 'markWatched':
del data['action']
self.doMarkWatched(data)
elif action == 'manualRating':
ratingData = data['ratingData']
self.doManualRating(ratingData)
elif action == 'manualSync':
if not self.syncThread.isAlive():
utilities.Debug("Performing a manual sync.")
self.doSync(manual=True, silent=data['silent'], library=data['library'])
else:
utilities.Debug("There already is a sync in progress.")
elif action == 'updatetags':
if self.updateTagsThread and self.updateTagsThread.isAlive():
utilities.Debug("Currently updating tags already.")
else:
self.updateTagsThread = threading.Thread(target=self.tagger.updateTagsFromTrakt, name="trakt-updatetags")
self.updateTagsThread.start()
elif action == 'managelists':
self.tagger.manageLists()
elif action == 'itemlists':
del data['action']
self.tagger.itemLists(data)
elif action == 'addtolist':
del data['action']
list = data['list']
del data['list']
self.tagger.manualAddToList(list, data)
elif action == 'removefromlist':
del data['action']
list = data['list']
del data['list']
self.tagger.manualRemoveFromList(list, data)
elif action == 'loadsettings':
force = False
if 'force' in data:
force = data['force']
globals.traktapi.getAccountSettings(force)
elif action == 'settings':
utilisites.showSettings()
else:
utilities.Debug("Unknown dispatch action, '%s'." % action)
def run(self):
startup_delay = utilities.getSettingAsInt('startup_delay')
if startup_delay:
utilities.Debug("Delaying startup by %d seconds." % startup_delay)
xbmc.sleep(startup_delay * 1000)
utilities.Debug("Service thread starting.")
# purge queue before doing anything
self.dispatchQueue.purge()
# queue a loadsettings action
self.dispatchQueue.append({'action': 'loadsettings'})
# setup event driven classes
self.Player = traktPlayer(action = self._dispatchQueue)
self.Monitor = traktMonitor(action = self._dispatchQueue)
# init traktapi class
globals.traktapi = traktAPI()
# init sync thread
self.syncThread = syncThread()
# init scrobbler class
self.scrobbler = Scrobbler(globals.traktapi)
# init tagging class
self.tagger = Tagger(globals.traktapi)
# start loop for events
while (not xbmc.abortRequested):
while len(self.dispatchQueue) and (not xbmc.abortRequested):
data = self.dispatchQueue.get()
utilities.Debug("Queued dispatch: %s" % data)
self._dispatch(data)
if xbmc.Player().isPlayingVideo():
self.scrobbler.update()
xbmc.sleep(500)
# we are shutting down
utilities.Debug("Beginning shut down.")
# check if watcher is set and active, if so, cancel it.
if self.watcher:
if self.watcher.isAlive():
self.watcher.cancel()
# delete player/monitor
del self.Player
del self.Monitor
# check update tags thread.
if self.updateTagsThread and self.updateTagsThread.isAlive():
self.updateTagsThread.join()
# check if sync thread is running, if so, join it.
if self.syncThread.isAlive():
self.syncThread.join()
def doWatching(self):
# check if we're still playing a video
if not xbmc.Player().isPlayingVideo():
self.watcher = None
return
# call watching method
self.scrobbler.watching()
# start a new timer thread
self.watcher = threading.Timer(self._interval, self.doWatching)
self.watcher.name = "trakt-watching"
self.watcher.start()
def doManualRating(self, data):
action = data['action']
media_type = data['media_type']
summaryInfo = None
if not utilities.isValidMediaType(media_type):
utilities.Debug("doManualRating(): Invalid media type '%s' passed for manual %s." % (media_type, action))
return
if not data['action'] in ['rate', 'unrate']:
utilities.Debug("doManualRating(): Unknown action passed.")
return
if 'dbid' in data:
utilities.Debug("Getting data for manual %s of library '%s' with ID of '%s'" % (action, media_type, data['dbid']))
elif 'remoteitd' in data:
if 'season' in data:
utilities.Debug("Getting data for manual %s of non-library '%s' S%02dE%02d, with ID of '%s'." % (action, media_type, data['season'], data['episode'], data['remoteid']))
else:
utilities.Debug("Getting data for manual %s of non-library '%s' with ID of '%s'" % (action, media_type, data['remoteid']))
if utilities.isEpisode(media_type):
summaryInfo = globals.traktapi.getEpisodeSummary(data['tvdb_id'], data['season'], data['episode'])
elif utilities.isShow(media_type):
summaryInfo = globals.traktapi.getShowSummary(data['imdbnumber'])
elif utilities.isMovie(media_type):
summaryInfo = globals.traktapi.getMovieSummary(data['imdbnumber'])
if not summaryInfo is None:
if utilities.isMovie(media_type) or utilities.isShow(media_type):
summaryInfo['xbmc_id'] = data['dbid']
if action == 'rate':
if not 'rating' in data:
rateMedia(media_type, summaryInfo)
else:
rateMedia(media_type, summaryInfo, rating=data['rating'])
elif action == 'unrate':
rateMedia(media_type, summaryInfo, unrate=True)
else:
utilities.Debug("doManualRating(): Summary info was empty, possible problem retrieving data from trakt.tv")
def doMarkWatched(self, data):
media_type = data['media_type']
simulate = utilities.getSettingAsBool('simulate_sync')
markedNotification = utilities.getSettingAsBool('show_marked_notification')
if utilities.isMovie(media_type):
summaryInfo = globals.traktapi.getMovieSummary(data['id'])
if summaryInfo:
if not summaryInfo['watched']:
s = utilities.getFormattedItemName(media_type, summaryInfo)
utilities.Debug("doMarkWatched(): '%s' is not watched on trakt, marking it as watched." % s)
movie = {}
movie['imdb_id'] = data['id']
movie['title'] = summaryInfo['title']
movie['year'] = summaryInfo['year']
movie['plays'] = 1
movie['last_played'] = int(time())
params = {'movies': [movie]}
utilities.Debug("doMarkWatched(): %s" % str(params))
if not simulate:
result = globals.traktapi.updateSeenMovie(params)
if result:
if markedNotification:
utilities.notification(utilities.getString(1550), s)
else:
utilities.notification(utilities.getString(1551), s)
else:
if markedNotification:
utilities.notification(utilities.getString(1550), s)
elif utilities.isEpisode(media_type):
summaryInfo = globals.traktapi.getEpisodeSummary(data['id'], data['season'], data['episode'])
if summaryInfo:
if not summaryInfo['episode']['watched']:
s = utilities.getFormattedItemName(media_type, summaryInfo)
utilities.Debug("doMarkWathced(): '%s' is not watched on trakt, marking it as watched." % s)
params = {}
params['imdb_id'] = summaryInfo['show']['imdb_id']
params['tvdb_id'] = summaryInfo['show']['tvdb_id']
params['title'] = summaryInfo['show']['title']
params['year'] = summaryInfo['show']['year']
params['episodes'] = [{'season': data['season'], 'episode': data['episode']}]
utilities.Debug("doMarkWatched(): %s" % str(params))
if not simulate:
result = globals.traktapi.updateSeenEpisode(params)
if result:
if markedNotification:
utilities.notification(utilities.getString(1550), s)
else:
utilities.notification(utilities.getString(1551), s)
else:
if markedNotification:
utilities.notification(utilities.getString(1550), s)
elif utilities.isSeason(media_type):
showInfo = globals.traktapi.getShowSummary(data['id'])
if not showInfo:
return
summaryInfo = globals.traktapi.getSeasonInfo(data['id'], data['season'])
if summaryInfo:
showInfo['season'] = data['season']
s = utilities.getFormattedItemName(media_type, showInfo)
params = {}
params['imdb_id'] = showInfo['imdb_id']
params['tvdb_id'] = showInfo['tvdb_id']
params['title'] = showInfo['title']
params['year'] = showInfo['year']
params['episodes'] = []
for ep in summaryInfo:
if ep['episode'] in data['episodes']:
if not ep['watched']:
params['episodes'].append({'season': ep['season'], 'episode': ep['episode']})
utilities.Debug("doMarkWatched(): '%s - Season %d' has %d episode(s) that are going to be marked as watched." % (showInfo['title'], data['season'], len(params['episodes'])))
if len(params['episodes']) > 0:
utilities.Debug("doMarkWatched(): %s" % str(params))
if not simulate:
result = globals.traktapi.updateSeenEpisode(params)
if result:
if markedNotification:
utilities.notification(utilities.getString(1550), utilities.getString(1552) % (len(params['episodes']), s))
else:
utilities.notification(utilities.getString(1551), utilities.getString(1552) % (len(params['episodes']), s))
else:
if markedNotification:
utilities.notification(utilities.getString(1550), utilities.getString(1552) % (len(params['episodes']), s))
elif utilities.isShow(media_type):
summaryInfo = globals.traktapi.getShowSummary(data['id'], extended=True)
if summaryInfo:
s = utilities.getFormattedItemName(media_type, summaryInfo)
params = {}
params['imdb_id'] = summaryInfo['imdb_id']
params['tvdb_id'] = summaryInfo['tvdb_id']
params['title'] = summaryInfo['title']
params['year'] = summaryInfo['year']
params['episodes'] = []
for season in summaryInfo['seasons']:
for ep in season['episodes']:
if str(season['season']) in data['seasons']:
if ep['episode'] in data['seasons'][str(season['season'])]:
if not ep['watched']:
params['episodes'].append({'season': ep['season'], 'episode': ep['episode']})
utilities.Debug("doMarkWatched(): '%s' has %d episode(s) that are going to be marked as watched." % (summaryInfo['title'], len(params['episodes'])))
if len(params['episodes']) > 0:
utilities.Debug("doMarkWatched(): %s" % str(params))
if not simulate:
result = globals.traktapi.updateSeenEpisode(params)
if result:
if markedNotification:
utilities.notification(utilities.getString(1550), utilities.getString(1552) % (len(params['episodes']), s))
else:
utilities.notification(utilities.getString(1551), utilities.getString(1552) % (len(params['episodes']), s))
else:
if markedNotification:
utilities.notification(utilities.getString(1550), utilities.getString(1552) % (len(params['episodes']), s))
def doSync(self, manual=False, silent=False, library="all"):
self.syncThread = syncThread(manual, silent, library)
self.syncThread.start()
class syncThread(threading.Thread):
_isManual = False
def __init__(self, isManual=False, runSilent=False, library="all"):
threading.Thread.__init__(self)
self.name = "trakt-sync"
self._isManual = isManual
self._runSilent = runSilent
self._library = library
def run(self):
sync = Sync(show_progress=self._isManual, run_silent=self._runSilent, library=self._library, api=globals.traktapi)
sync.sync()
if utilities.getSettingAsBool('tagging_enable') and utilities.getSettingAsBool('tagging_tag_after_sync'):
q = queue.SqliteQueue()
q.append({'action': 'updatetags'})
class traktMonitor(xbmc.Monitor):
def __init__(self, *args, **kwargs):
xbmc.Monitor.__init__(self)
self.action = kwargs['action']
utilities.Debug("[traktMonitor] Initalized.")
# called when database gets updated and return video or music to indicate which DB has been changed
def onDatabaseUpdated(self, database):
if database == 'video':
utilities.Debug("[traktMonitor] onDatabaseUpdated(database: %s)" % database)
data = {'action': 'databaseUpdated'}
self.action(data)
# called when database update starts and return video or music to indicate which DB is being updated
def onDatabaseScanStarted(self, database):
if database == "video":
utilities.Debug("[traktMonitor] onDatabaseScanStarted(database: %s)" % database)
data = {'action': 'scanStarted'}
self.action(data)
def onSettingsChanged(self):
data = {'action': 'settingsChanged'}
self.action(data)
class traktPlayer(xbmc.Player):
_playing = False
plIndex = None
def __init__(self, *args, **kwargs):
xbmc.Player.__init__(self)
self.action = kwargs['action']
utilities.Debug("[traktPlayer] Initalized.")
# called when xbmc starts playing a file
def onPlayBackStarted(self):
xbmc.sleep(1000)
self.type = None
self.id = None
# only do anything if we're playing a video
if self.isPlayingVideo():
# get item data from json rpc
result = utilities.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'Player.GetItem', 'params': {'playerid': 1}, 'id': 1})
utilities.Debug("[traktPlayer] onPlayBackStarted() - %s" % result)
# check for exclusion
_filename = None
try:
_filename = self.getPlayingFile()
except:
utilities.Debug("[traktPlayer] onPlayBackStarted() - Exception trying to get playing filename, player suddenly stopped.")
return
if utilities.checkScrobblingExclusion(_filename):
utilities.Debug("[traktPlayer] onPlayBackStarted() - '%s' is in exclusion settings, ignoring." % _filename)
return
self.type = result['item']['type']
data = {'action': 'started'}
# check type of item
if self.type == 'unknown':
# do a deeper check to see if we have enough data to perform scrobbles
utilities.Debug("[traktPlayer] onPlayBackStarted() - Started playing a non-library file, checking available data.")
season = xbmc.getInfoLabel('VideoPlayer.Season')
episode = xbmc.getInfoLabel('VideoPlayer.Episode')
showtitle = xbmc.getInfoLabel('VideoPlayer.TVShowTitle')
year = xbmc.getInfoLabel('VideoPlayer.Year')
utilities.Debug("[traktPlayer] info - showtitle:"+ showtitle +", Year:"+ year +", Season:"+ season +", Episode:"+ episode)
if season and episode and showtitle:
# we have season, episode and show title, can scrobble this as an episode
self.type = 'episode'
data['type'] = 'episode'
data['season'] = int(season)
data['episode'] = int(episode)
data['showtitle'] = showtitle
data['title'] = xbmc.getInfoLabel('VideoPlayer.Title')
if year.isdigit():
data['year'] = year
utilities.Debug("[traktPlayer] onPlayBackStarted() - Playing a non-library 'episode' - %s - S%02dE%02d - %s." % (data['showtitle'], data['season'], data['episode'], data['title']))
elif year and not season and not showtitle:
# we have a year and no season/showtitle info, enough for a movie
self.type = 'movie'
data['type'] = 'movie'
data['year'] = int(year)
data['title'] = xbmc.getInfoLabel('VideoPlayer.Title')
utilities.Debug("[traktPlayer] onPlayBackStarted() - Playing a non-library 'movie' - %s (%d)." % (data['title'], data['year']))
elif showtitle:
title, season, episode = utilities.regex_tvshow(False, showtitle)
data['type'] = 'episode'
data['season'] = int(season)
data['episode'] = int(episode)
data['showtitle'] = title
data['title'] = title
utilities.Debug("[traktPlayer] onPlayBackStarted() - Title:"+title+", showtitle:"+showtitle+", season:"+season+", episode:"+episode)
else:
utilities.Debug("[traktPlayer] onPlayBackStarted() - Non-library file, not enough data for scrobbling, skipping.")
return
elif self.type == 'episode' or self.type == 'movie':
# get library id
self.id = result['item']['id']
data['id'] = self.id
data['type'] = self.type
if self.type == 'episode':
utilities.Debug("[traktPlayer] onPlayBackStarted() - Doing multi-part episode check.")
result = utilities.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodeDetails', 'params': {'episodeid': self.id, 'properties': ['tvshowid', 'season', 'episode']}, 'id': 1})
if result:
utilities.Debug("[traktPlayer] onPlayBackStarted() - %s" % result)
tvshowid = int(result['episodedetails']['tvshowid'])
season = int(result['episodedetails']['season'])
episode = int(result['episodedetails']['episode'])
episode_index = episode - 1
result = utilities.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodes', 'params': {'tvshowid': tvshowid, 'season': season, 'properties': ['episode', 'file'], 'sort': {'method': 'episode'}}, 'id': 1})
if result:
utilities.Debug("[traktPlayer] onPlayBackStarted() - %s" % result)
# make sure episodes array exists in results
if 'episodes' in result:
multi = []
for i in range(episode_index, result['limits']['total']):
if result['episodes'][i]['file'] == result['episodes'][episode_index]['file']:
multi.append(result['episodes'][i]['episodeid'])
else:
break
if len(multi) > 1:
data['multi_episode_data'] = multi
data['multi_episode_count'] = len(multi)
utilities.Debug("[traktPlayer] onPlayBackStarted() - This episode is part of a multi-part episode.")
else:
utilities.Debug("[traktPlayer] onPlayBackStarted() - This is a single episode.")
else:
utilities.Debug("[traktPlayer] onPlayBackStarted() - Video type '%s' unrecognized, skipping." % self.type)
return
pl = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
plSize = len(pl)
if plSize > 1:
pos = pl.getposition()
if not self.plIndex is None:
utilities.Debug("[traktPlayer] onPlayBackStarted() - User manually skipped to next (or previous) video, forcing playback ended event.")
self.onPlayBackEnded()
self.plIndex = pos
utilities.Debug("[traktPlayer] onPlayBackStarted() - Playlist contains %d item(s), and is currently on item %d" % (plSize, (pos + 1)))
self._playing = True
# send dispatch
self.action(data)
# called when xbmc stops playing a file
def onPlayBackEnded(self):
if self._playing:
utilities.Debug("[traktPlayer] onPlayBackEnded() - %s" % self.isPlayingVideo())
self._playing = False
self.plIndex = None
data = {'action': 'ended'}
self.action(data)
# called when user stops xbmc playing a file
def onPlayBackStopped(self):
if self._playing:
utilities.Debug("[traktPlayer] onPlayBackStopped() - %s" % self.isPlayingVideo())
self._playing = False
self.plIndex = None
data = {'action': 'stopped'}
self.action(data)
# called when user pauses a playing file
def onPlayBackPaused(self):
if self._playing:
utilities.Debug("[traktPlayer] onPlayBackPaused() - %s" % self.isPlayingVideo())
data = {'action': 'paused'}
self.action(data)
# called when user resumes a paused file
def onPlayBackResumed(self):
if self._playing:
utilities.Debug("[traktPlayer] onPlayBackResumed() - %s" % self.isPlayingVideo())
data = {'action': 'resumed'}
self.action(data)
# called when user queues the next item
def onQueueNextItem(self):
if self._playing:
utilities.Debug("[traktPlayer] onQueueNextItem() - %s" % self.isPlayingVideo())
# called when players speed changes. (eg. user FF/RW)
def onPlayBackSpeedChanged(self, speed):
if self._playing:
utilities.Debug("[traktPlayer] onPlayBackSpeedChanged(speed: %s) - %s" % (str(speed), self.isPlayingVideo()))
# called when user seeks to a time
def onPlayBackSeek(self, time, offset):
if self._playing:
utilities.Debug("[traktPlayer] onPlayBackSeek(time: %s, offset: %s) - %s" % (str(time), str(offset), self.isPlayingVideo()))
data = {'action': 'seek', 'time': time, 'offset': offset}
self.action(data)
# called when user performs a chapter seek
def onPlayBackSeekChapter(self, chapter):
if self._playing:
utilities.Debug("[traktPlayer] onPlayBackSeekChapter(chapter: %s) - %s" % (str(chapter), self.isPlayingVideo()))
data = {'action': 'seekchapter', 'chapter': chapter}
self.action(data)