forked from razzeee/script.trakt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrobbler.py
executable file
·294 lines (250 loc) · 11.3 KB
/
scrobbler.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
# -*- coding: utf-8 -*-
#
import xbmc
import time
import utilities
import tagging
from utilities import Debug
from rating import ratingCheck
class Scrobbler():
traktapi = None
isPlaying = False
isPaused = False
isMultiPartEpisode = False
lastMPCheck = 0
curMPEpisode = 0
videoDuration = 1
watchedTime = 0
pausedAt = 0
curVideo = None
curVideoInfo = None
playlistLength = 1
playlistIndex = 0
markedAsWatched = []
traktSummaryInfo = None
def __init__(self, api):
self.traktapi = api
def _currentEpisode(self, watchedPercent, episodeCount):
split = (100 / episodeCount)
for i in range(episodeCount - 1, 0, -1):
if watchedPercent >= (i * split):
return i
return 0
def update(self, forceCheck = False):
if not xbmc.Player().isPlayingVideo():
return
if self.isPlaying:
t = xbmc.Player().getTime()
l = xbmc.PlayList(xbmc.PLAYLIST_VIDEO).getposition()
if self.playlistIndex == l:
self.watchedTime = t
else:
Debug("[Scrobbler] Current playlist item changed! Not updating time! (%d -> %d)" % (self.playlistIndex, l))
if 'id' in self.curVideo and self.isMultiPartEpisode:
# do transition check every minute
if (time.time() > (self.lastMPCheck + 60)) or forceCheck:
self.lastMPCheck = time.time()
watchedPercent = (self.watchedTime / self.videoDuration) * 100
epIndex = self._currentEpisode(watchedPercent, self.curVideo['multi_episode_count'])
if self.curMPEpisode != epIndex:
# current episode in multi-part episode has changed
Debug("[Scrobbler] Attempting to scrobble episode part %d of %d." % (self.curMPEpisode + 1, self.curVideo['multi_episode_count']))
# recalculate watchedPercent and duration for multi-part, and scrobble
adjustedDuration = int(self.videoDuration / self.curVideo['multi_episode_count'])
duration = adjustedDuration / 60
watchedPercent = ((self.watchedTime - (adjustedDuration * self.curMPEpisode)) / adjustedDuration) * 100
response = self.traktapi.scrobbleEpisode(self.curVideoInfo, watchedPercent)
if response != None:
Debug("[Scrobbler] Scrobble response: %s" % str(response))
# update current information
self.curMPEpisode = epIndex
self.curVideoInfo = utilities.getEpisodeDetailsFromXbmc(self.curVideo['multi_episode_data'][self.curMPEpisode], ['showtitle', 'season', 'episode', 'tvshowid', 'uniqueid'])
def playbackStarted(self, data):
Debug("[Scrobbler] playbackStarted(data: %s)" % data)
if not data:
return
self.curVideo = data
self.curVideoInfo = None
# {"jsonrpc":"2.0","method":"Player.OnPlay","params":{"data":{"item":{"type":"movie"},"player":{"playerid":1,"speed":1},"title":"Shooter","year":2007},"sender":"xbmc"}}
# {"jsonrpc":"2.0","method":"Player.OnPlay","params":{"data":{"episode":3,"item":{"type":"episode"},"player":{"playerid":1,"speed":1},"season":4,"showtitle":"24","title":"9:00 A.M. - 10:00 A.M."},"sender":"xbmc"}}
if 'type' in self.curVideo:
Debug("[Scrobbler] Watching: %s" % self.curVideo['type'])
if not xbmc.Player().isPlayingVideo():
Debug("[Scrobbler] Suddenly stopped watching item")
return
xbmc.sleep(1000) # Wait for possible silent seek (caused by resuming)
try:
self.watchedTime = xbmc.Player().getTime()
self.videoDuration = xbmc.Player().getTotalTime()
except Exception, e:
Debug("[Scrobbler] Suddenly stopped watching item: %s" % e.message)
self.curVideo = None
return
if self.videoDuration == 0:
if utilities.isMovie(self.curVideo['type']):
self.videoDuration = 90
elif utilities.isEpisode(self.curVideo['type']):
self.videoDuration = 30
else:
self.videoDuration = 1
self.playlistLength = len(xbmc.PlayList(xbmc.PLAYLIST_VIDEO))
self.playlistIndex = xbmc.PlayList(xbmc.PLAYLIST_VIDEO).getposition()
if (self.playlistLength == 0):
Debug("[Scrobbler] Warning: Cant find playlist length, assuming that this item is by itself")
self.playlistLength = 1
self.traktSummaryInfo = None
self.isMultiPartEpisode = False
if utilities.isMovie(self.curVideo['type']):
if 'id' in self.curVideo:
self.curVideoInfo = utilities.getMovieDetailsFromXbmc(self.curVideo['id'], ['imdbnumber', 'title', 'year'])
if utilities.getSettingAsBool('rate_movie'):
# pre-get sumamry information, for faster rating dialog.
Debug("[Scrobbler] Movie rating is enabled, pre-fetching summary information.")
imdb_id = self.curVideoInfo['imdbnumber']
if imdb_id.startswith("tt") or imdb_id.isdigit():
self.traktSummaryInfo = self.traktapi.getMovieSummary(self.curVideoInfo['imdbnumber'])
self.traktSummaryInfo['xbmc_id'] = self.curVideo['id']
else:
self.curVideoInfo['imdbnumber'] = None
Debug("[Scrobbler] Can not get summary information for '%s (%d)' as is has no valid id, will retry during a watching call." % (self.curVideoInfo['title'], self.curVideoInfo['year']))
elif 'title' in self.curVideo and 'year' in self.curVideo:
self.curVideoInfo = {}
self.curVideoInfo['imdbnumber'] = None
self.curVideoInfo['title'] = self.curVideo['title']
self.curVideoInfo['year'] = self.curVideo['year']
elif utilities.isEpisode(self.curVideo['type']):
if 'id' in self.curVideo:
self.curVideoInfo = utilities.getEpisodeDetailsFromXbmc(self.curVideo['id'], ['showtitle', 'season', 'episode', 'tvshowid', 'uniqueid'])
if not self.curVideoInfo: # getEpisodeDetailsFromXbmc was empty
Debug("[Scrobbler] Episode details from XBMC was empty, ID (%d) seems invalid, aborting further scrobbling of this episode." % self.curVideo['id'])
self.curVideo = None
self.isPlaying = False
self.watchedTime = 0
return
if utilities.getSettingAsBool('rate_episode'):
# pre-get sumamry information, for faster rating dialog.
Debug("[Scrobbler] Episode rating is enabled, pre-fetching summary information.")
tvdb_id = self.curVideoInfo['tvdb_id']
if tvdb_id.isdigit() or tvdb_id.startswith("tt"):
self.traktSummaryInfo = self.traktapi.getEpisodeSummary(tvdb_id, self.curVideoInfo['season'], self.curVideoInfo['episode'])
else:
self.curVideoInfo['tvdb_id'] = None
Debug("[Scrobbler] Can not get summary information for '%s - S%02dE%02d' as it has no valid id, will retry during a watching call." % (self.curVideoInfo['showtitle'], self.curVideoInfo['season'], self.curVideoInfo['episode']))
elif 'showtitle' in self.curVideo and 'season' in self.curVideo and 'episode' in self.curVideo:
self.curVideoInfo = {}
self.curVideoInfo['tvdb_id'] = None
self.curVideoInfo['year'] = None
if 'year' in self.curVideo:
self.curVideoInfo['year'] = self.curVideo['year']
self.curVideoInfo['showtitle'] = self.curVideo['showtitle']
self.curVideoInfo['season'] = self.curVideo['season']
self.curVideoInfo['episode'] = self.curVideo['episode']
if 'multi_episode_count' in self.curVideo:
self.isMultiPartEpisode = True
self.markedAsWatched = []
episode_count = self.curVideo['multi_episode_count']
for i in range(episode_count):
self.markedAsWatched.append(False)
self.isPlaying = True
self.isPaused = False
self.scrobble('start')
def playbackResumed(self):
if not self.isPlaying:
return
Debug("[Scrobbler] playbackResumed()")
if self.isPaused:
p = time.time() - self.pausedAt
Debug("[Scrobbler] Resumed after: %s" % str(p))
self.pausedAt = 0
self.isPaused = False
self.update(True)
self.scrobble('start')
def playbackPaused(self):
if not self.isPlaying:
return
Debug("[Scrobbler] playbackPaused()")
self.update(True)
Debug("[Scrobbler] Paused after: %s" % str(self.watchedTime))
self.isPaused = True
self.pausedAt = time.time()
self.scrobble('pause')
def playbackSeek(self):
if not self.isPlaying:
return
Debug("[Scrobbler] playbackSeek()")
self.scrobble('start')
def playbackEnded(self):
if not self.isPlaying:
return
Debug("[Scrobbler] playbackEnded()")
if self.curVideo == None:
Debug("[Scrobbler] Warning: Playback ended but video forgotten.")
return
self.isPlaying = False
self.markedAsWatched = []
if self.watchedTime != 0:
if 'type' in self.curVideo:
self.scrobble('stop')
ratingCheck(self.curVideo['type'], self.traktSummaryInfo, self.watchedTime, self.videoDuration, self.playlistLength)
self.watchedTime = 0
self.isMultiPartEpisode = False
self.traktSummaryInfo = None
self.curVideo = None
self.playlistLength = 0
self.playlistIndex = 0
def scrobble(self, status):
if not self.curVideoInfo:
return
Debug("[Scrobbler] scrobble()")
scrobbleMovieOption = utilities.getSettingAsBool('scrobble_movie')
scrobbleEpisodeOption = utilities.getSettingAsBool('scrobble_episode')
duration = self.videoDuration / 60
watchedPercent = (self.watchedTime / self.videoDuration) * 100
if utilities.isMovie(self.curVideo['type']) and scrobbleMovieOption:
response = self.traktapi.scrobbleMovie(self.curVideoInfo, watchedPercent, status)
if not response is None and 'status' in response:
if response['status'] == "success":
self.watchlistTagCheck()
response['title'] = response['movie']['title']
response['year'] = response['movie']['year']
self.scrobbleNotification(response)
Debug("[Scrobbler] Scrobble response: %s" % str(response))
elif response['status'] == "failure":
Debug("[Scrobbler] Movie '%s' was not found on trakt.tv, possible malformed XBMC metadata." % self.curVideoInfo['title'])
elif utilities.isEpisode(self.curVideo['type']) and scrobbleEpisodeOption:
if self.isMultiPartEpisode:
Debug("[Scrobbler] Multi-part episode, scrobbling part %d of %d." % (self.curMPEpisode + 1, self.curVideo['multi_episode_count']))
adjustedDuration = int(self.videoDuration / self.curVideo['multi_episode_count'])
duration = adjustedDuration / 60
watchedPercent = ((self.watchedTime - (adjustedDuration * self.curMPEpisode)) / adjustedDuration) * 100
response = self.traktapi.scrobbleEpisode(self.curVideoInfo, watchedPercent, status)
if not response is None and 'status' in response:
if response['status'] == "success":
response['episode']['season'] = response['season']
self.scrobbleNotification(response)
Debug("[Scrobbler] Scrobble response: %s" % str(response))
elif response['status'] == "failure":
Debug("[Scrobbler] Show '%s' was not found on trakt.tv, possible malformed XBMC metadata." % self.curVideoInfo['showtitle'])
def scrobbleNotification(self, info):
if not self.curVideoInfo:
return
if utilities.getSettingAsBool("scrobble_notification"):
s = utilities.getFormattedItemName(self.curVideo['type'], info)
utilities.notification(utilities.getString(1049), s)
def watchlistTagCheck(self):
if not utilities.isMovie(self.curVideo['type']):
return
if not 'id' in self.curVideo:
return
if not (tagging.isTaggingEnabled() and tagging.isWatchlistsEnabled()):
return
id = self.curVideo['id']
result = utilities.getMovieDetailsFromXbmc(id, ['tag'])
if result:
tags = result['tag']
if tagging.hasTraktWatchlistTag(tags):
tags.remove(tagging.listToTag("Watchlist"))
s = utilities.getFormattedItemName(self.curVideo['type'], self.curVideoInfo)
tagging.xbmcSetTags(id, self.curVideo['type'], s, tags)
else:
utilities.Debug("No data was returned from XBMC, aborting tag udpate.")