forked from trakt/script.trakt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
433 lines (374 loc) · 16 KB
/
script.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
# -*- coding: utf-8 -*-
import utilities as utils
import xbmc
import sys
import queue
import tagging
import time
from traktContextMenu import traktContextMenu
try:
import simplejson as json
except ImportError:
import json
def getMediaType():
if xbmc.getCondVisibility('Container.Content(tvshows)'):
return "show"
elif xbmc.getCondVisibility('Container.Content(seasons)'):
return "season"
elif xbmc.getCondVisibility('Container.Content(episodes)'):
return "episode"
elif xbmc.getCondVisibility('Container.Content(movies)'):
return "movie"
else:
return None
def getArguments():
data = None
default_actions = {0: "sync", 1: "managelists"}
default = utils.getSettingAsInt('default_action')
if len(sys.argv) == 1:
data = {'action': default_actions[default]}
else:
data = {}
for item in sys.argv:
values = item.split("=")
if len(values) == 2:
data[values[0].lower()] = values[1]
data['action'] = data['action'].lower()
return data
def Main():
args = getArguments()
data = {}
if args['action'] == 'contextmenu':
buttons = []
media_type = getMediaType()
if utils.getSettingAsBool('tagging_enable'):
if utils.isMovie(media_type):
buttons.append("itemlists")
dbid = int(xbmc.getInfoLabel('ListItem.DBID'))
result = utils.getMovieDetailsFromXbmc(dbid, ['tag'])
if tagging.hasTraktWatchlistTag(result['tag']):
buttons.append("removefromlist")
else:
buttons.append("addtolist")
elif utils.isShow(media_type):
buttons.append("itemlists")
dbid = int(xbmc.getInfoLabel('ListItem.DBID'))
result = utils.getShowDetailsFromXBMC(dbid, ['tag'])
if tagging.hasTraktWatchlistTag(result['tag']):
buttons.append("removefromlist")
else:
buttons.append("addtolist")
if media_type in ['movie', 'show', 'episode']:
buttons.append("rate")
if media_type in ['movie', 'show', 'season', 'episode']:
buttons.append("togglewatched")
if utils.getSettingAsBool('tagging_enable'):
buttons.append("managelists")
buttons.append("updatetags")
buttons.append("sync")
contextMenu = traktContextMenu(media_type=media_type, buttons=buttons)
contextMenu.doModal()
_action = contextMenu.action
del contextMenu
if _action is None:
return
utils.Debug("'%s' selected from trakt.tv action menu" % _action)
args['action'] = _action
if _action in ['addtolist', 'removefromlist']:
args['list'] = "watchlist"
if args['action'] == 'sync':
data = {'action': 'manualSync'}
data['silent'] = False
if 'silent' in args:
data['silent'] = (args['silent'].lower() == 'true')
data['library'] = "all"
if 'library' in args and args['library'] in ['episodes', 'movies']:
data['library'] = args['library']
elif args['action'] == 'loadsettings':
data = {'action': 'loadsettings', 'force': True}
utils.notification(utils.getString(1201), utils.getString(1111))
elif args['action'] == 'settings':
data = {'action': 'settings'}
elif args['action'] in ['rate', 'unrate']:
data = {}
data['action'] = args['action']
media_type = None
if 'media_type' in args and 'dbid' in args:
media_type = args['media_type']
try:
data['dbid'] = int(args['dbid'])
except ValueError:
utils.Debug("Manual %s triggered for library item, but DBID is invalid." % args['action'])
return
elif 'media_type' in args and 'remoteid' in args:
media_type = args['media_type']
data['remoteid'] = args['remoteid']
if 'season' in args:
if not 'episode' in args:
utils.Debug("Manual %s triggered for non-library episode, but missing episode number." % args['action'])
return
try:
data['season'] = int(args['season'])
data['episode'] = int(args['episode'])
except ValueError:
utilities.Debug("Error parsing season or episode for manual %s" % args['action'])
return
else:
media_type = getMediaType()
if not utils.isValidMediaType(media_type):
utils.Debug("Error, not in video library.")
return
data['dbid'] = int(xbmc.getInfoLabel('ListItem.DBID'))
if media_type is None:
utils.Debug("Manual %s triggered on an unsupported content container." % args['action'])
elif utils.isValidMediaType(media_type):
data['media_type'] = media_type
if 'dbid' in data:
utils.Debug("Manual %s of library '%s' with an ID of '%s'." % (args['action'], media_type, data['dbid']))
if utils.isMovie(media_type):
result = utils.getMovieDetailsFromXbmc(data['dbid'], ['imdbnumber', 'title', 'year'])
if not result:
utils.Debug("No data was returned from XBMC, aborting manual %s." % args['action'])
return
data['imdbnumber'] = result['imdbnumber']
elif utils.isShow(media_type):
result = utils.getShowDetailsFromXBMC(data['dbid'], ['imdbnumber', 'tag'])
if not result:
utils.Debug("No data was returned from XBMC, aborting manual %s." % args['action'])
return
data['imdbnumber'] = result['imdbnumber']
data['tag'] = result['tag']
elif utils.isEpisode(media_type):
result = utils.getEpisodeDetailsFromXbmc(data['dbid'], ['showtitle', 'season', 'episode', 'tvshowid'])
if not result:
utils.Debug("No data was returned from XBMC, aborting manual %s." % args['action'])
return
data['tvdb_id'] = result['tvdb_id']
data['season'] = result['season']
data['episode'] = result['episode']
else:
if 'season' in data:
utils.Debug("Manual %s of non-library '%s' S%02dE%02d, with an ID of '%s'." % (args['action'], media_type, data['season'], data['episode'], data['remoteid']))
data['tvdb_id'] = data['remoteid']
else:
utils.Debug("Manual %s of non-library '%s' with an ID of '%s'." % (args['action'], media_type, data['remoteid']))
data['imdbnumber'] = data['remoteid']
if args['action'] == 'rate' and 'rating' in args:
if args['rating'] in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']:
data['rating'] = int(args['rating'])
data = {'action': 'manualRating', 'ratingData': data}
else:
utils.Debug("Manual %s of '%s' is unsupported." % (args['action'], media_type))
elif args['action'] == 'togglewatched':
media_type = getMediaType()
if media_type in ['movie', 'show', 'season', 'episode']:
data = {}
data['media_type'] = media_type
if utils.isMovie(media_type):
dbid = int(xbmc.getInfoLabel('ListItem.DBID'))
result = utils.getMovieDetailsFromXbmc(dbid, ['imdbnumber', 'title', 'year', 'playcount'])
if result:
if result['playcount'] == 0:
data['id'] = result['imdbnumber']
else:
utils.Debug("Movie alread marked as watched in XBMC.")
else:
utils.Debug("Error getting movie details from XBMC.")
return
elif utils.isEpisode(media_type):
dbid = int(xbmc.getInfoLabel('ListItem.DBID'))
result = utils.getEpisodeDetailsFromXbmc(dbid, ['showtitle', 'season', 'episode', 'tvshowid', 'playcount'])
if result:
if result['playcount'] == 0:
data['id'] = result['tvdb_id']
data['season'] = result['season']
data['episode'] = result['episode']
else:
utils.Debug("Episode already marked as watched in XBMC.")
else:
utils.Debug("Error getting episode details from XBMC.")
return
elif utils.isSeason(media_type):
showID = None
showTitle = xbmc.getInfoLabel('ListItem.TVShowTitle')
result = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShows', 'params': {'properties': ['title', 'imdbnumber', 'year']}, 'id': 0})
if result and 'tvshows' in result:
for show in result['tvshows']:
if show['title'] == showTitle:
showID = show['tvshowid']
data['id'] = show['imdbnumber']
break
else:
utils.Debug("Error getting TV shows from XBMC.")
return
season = xbmc.getInfoLabel('ListItem.Season')
if season == "":
season = 0
else:
season = int(season)
result = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodes', 'params': {'tvshowid': showID, 'season': season, 'properties': ['season', 'episode', 'playcount']}, 'id': 0})
if result and 'episodes' in result:
episodes = []
for episode in result['episodes']:
if episode['playcount'] == 0:
episodes.append(episode['episode'])
if len(episodes) == 0:
utils.Debug("'%s - Season %d' is already marked as watched." % (showTitle, season))
return
data['season'] = season
data['episodes'] = episodes
else:
utils.Debug("Error getting episodes from '%s' for Season %d" % (showTitle, season))
return
elif utils.isShow(media_type):
dbid = int(xbmc.getInfoLabel('ListItem.DBID'))
result = utils.getShowDetailsFromXBMC(dbid, ['year', 'imdbnumber'])
if not result:
utils.Debug("Error getting show details from XBMC.")
return
showTitle = result['label']
data['id'] = result['imdbnumber']
result = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodes', 'params': {'tvshowid': dbid, 'properties': ['season', 'episode', 'playcount']}, 'id': 0})
if result and 'episodes' in result:
i = 0
s = {}
for e in result['episodes']:
season = str(e['season'])
if not season in s:
s[season] = []
if e['playcount'] == 0:
s[season].append(e['episode'])
i = i + 1
if i == 0:
utils.Debug("'%s' is already marked as watched." % showTitle)
return
data['seasons'] = dict((k, v) for k, v in s.iteritems() if v)
else:
utils.Debug("Error getting episode details for '%s' from XBMC." % showTitle)
return
if len(data) > 1:
utils.Debug("Marking '%s' with the following data '%s' as watched on trakt.tv" % (media_type, str(data)))
data['action'] = 'markWatched'
# execute toggle watched action
xbmc.executebuiltin("Action(ToggleWatched)")
elif args['action'] == 'updatetags':
data = {'action': 'updatetags'}
elif args['action'] == 'managelists':
data = {'action': 'managelists'}
elif args['action'] == 'timertest':
utils.Debug("Timing JSON requests.")
import time
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShowDetails', 'params':{'tvshowid': 254, 'properties': ['tag']}, 'id': 1})
#data = utils.getShowDetailsFromXBMC(254, ['tag', 'imdbnumber'])
e = time.time() - t
utils.Debug("VideoLibrary.GetTVShowDetails with tags: %0.3f seconds." % e)
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetMovieDetails', 'params':{'movieid': 634, 'properties': ['tag']}, 'id': 1})
#data = utils.getMovieDetailsFromXbmc(634, ['tag', 'imdbnumber', 'title', 'year'])
e = time.time() - t
utils.Debug("VideoLibrary.GetMovieDetails with tags: %0.3f seconds." % e)
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShows', 'params': {'properties': ['tag', 'title', 'imdbnumber', 'year']}, 'id': 0})
e = time.time() - t
utils.Debug("VideoLibrary.GetTVShows with tags: %0.3f seconds, %d items at %0.5f seconds per item" % (e, len(data['tvshows']), e / len(data['tvshows'])))
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'id': 0, 'method': 'VideoLibrary.GetMovies', 'params': {'properties': ['tag', 'title', 'imdbnumber', 'year']}})
e = time.time() - t
utils.Debug("VideoLibrary.GetMovies with tags: %0.3f seconds, %d items at %0.5f seconds per item" % (e, len(data['movies']), e / len(data['movies'])))
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShows', 'params': {'properties': ['title', 'imdbnumber', 'year']}, 'id': 0})
e = time.time() - t
utils.Debug("VideoLibrary.GetTVShows without tags: %0.3f seconds, %d items at %0.5f seconds per item" % (e, len(data['tvshows']), e / len(data['tvshows'])))
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'id': 0, 'method': 'VideoLibrary.GetMovies', 'params': {'properties': ['title', 'imdbnumber', 'year']}})
e = time.time() - t
utils.Debug("VideoLibrary.GetMovies without tags: %0.3f seconds, %d items at %0.5f seconds per item" % (e, len(data['movies']), e / len(data['movies'])))
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShowDetails', 'params':{'tvshowid': 254, 'properties': ['imdbnumber']}, 'id': 1})
#data = utils.getShowDetailsFromXBMC(254, ['imdbnumber'])
e = time.time() - t
utils.Debug("VideoLibrary.GetTVShowDetails without tags: %0.3f seconds." % e)
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetMovieDetails', 'params':{'movieid': 634, 'properties': ['imdbnumber', 'title', 'year']}, 'id': 1})
#data = utils.getMovieDetailsFromXbmc(634, ['imdbnumber', 'title', 'year'])
e = time.time() - t
utils.Debug("VideoLibrary.GetMovieDetails without tags: %0.3f seconds." % e)
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShows', 'params': {'properties': ['title', 'imdbnumber', 'year']}, 'id': 0})
data = data['tvshows']
for item in data:
item_data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShowDetails', 'params':{'tvshowid': item['tvshowid'], 'properties': ['tag']}, 'id': 1})
item['tag'] = item_data['tvshowdetails']['tag']
e = time.time() - t
utils.Debug("VideoLibrary.GetTVShows with tags from loop: %0.3f seconds, %d items at %0.5f seconds per item" % (e, len(data), e / len(data)))
t = time.time()
data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetMovies', 'params': {'properties': ['title', 'imdbnumber', 'year']}, 'id': 0})
data = data['movies']
for item in data:
item_data = utils.xbmcJsonRequest({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetMovieDetails', 'params':{'movieid': item['movieid'], 'properties': ['tag']}, 'id': 1})
item['tag'] = item_data['moviedetails']['tag']
e = time.time() - t
utils.Debug("VideoLibrary.GetMovies with tags from: %0.3f seconds, %d items at %0.5f seconds per item." % (e, len(data), e / len(data)))
elif args['action'] in ['itemlists', 'addtolist', 'removefromlist']:
data = {}
data['action'] = args['action']
media_type = None
dbid = None
if 'media_type' in args and 'dbid' in args:
media_type = args['media_type']
try:
dbid = int(args['dbid'])
except ValueError:
utils.Debug("'%s' triggered for library item, but DBID is invalid." % args['action'])
return
else:
media_type = getMediaType()
if not media_type in ['movie', 'show']:
utils.Debug("Error, not in video library.")
return
try:
dbid = int(xbmc.getInfoLabel('ListItem.DBID'))
except ValueError:
utils.Debug("'%s' triggered for library item, but there is a problem with ListItem.DBID." % args['action'])
return
if not media_type in ['movie', 'show']:
utils.Debug("'%s' is not a valid media type for '%s'." % (media_type, args['action']))
return
if args['action'] in ['addtolist', 'removefromlist']:
if 'list' in args:
data['list'] = args['list']
else:
utils.Debug("'%s' requires a list parameter." % data['action'])
data['type'] = media_type
if utils.isMovie(media_type):
result = utils.getMovieDetailsFromXbmc(dbid, ['imdbnumber', 'title', 'year', 'tag'])
if not result:
utils.Debug("Error getting movie details from XBMC.")
return
data['tag'] = result['tag']
data['movieid'] = result['movieid']
data['title'] = result['title']
data['year'] = result['year']
if result['imdbnumber'].startswith("tt"):
data['imdb_id'] = result['imdbnumber']
elif result['imdbnumber'].isdigit():
data['tmdb_id'] = result['imdbnumber']
elif utils.isShow(media_type):
result = utils.getShowDetailsFromXBMC(dbid, ['imdbnumber', 'title', 'tag'])
if not result:
utils.Debug("Error getting show details from XBMC.")
return
data['tag'] = result['tag']
data['tvshowid'] = result['tvshowid']
data['title'] = result['title']
if result['imdbnumber'].startswith("tt"):
data['imdb_id'] = result['imdbnumber']
elif result['imdbnumber'].isdigit():
data['tvdb_id'] = result['imdbnumber']
q = queue.SqliteQueue()
if 'action' in data:
utils.Debug("Queuing for dispatch: %s" % data)
q.append(data)
if __name__ == '__main__':
Main()