forked from trakt/script.trakt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrobbler.py
executable file
·211 lines (193 loc) · 8.35 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
# -*- coding: utf-8 -*-
#
import xbmc
import xbmcaddon
import sys
import threading
import time
import utilities
from utilities import Debug
from rating import ratingCheck
# read settings
__settings__ = xbmcaddon.Addon("script.trakt")
__language__ = __settings__.getLocalizedString
class Scrobbler(threading.Thread):
totalTime = 1
watchedTime = 0
startTime = 0
pausedTime = 0
curVideo = None
curVideoData = None
pinging = False
playlistLength = 1
abortRequested = False
def run(self):
# When requested ping trakt to say that the user is still watching the item
count = 0
Debug("Scrobbler starting.")
while (not (self.abortRequested or xbmc.abortRequested)):
xbmc.sleep(5000) # sleep for 5 seconds
if self.pinging and xbmc.Player().isPlayingVideo():
count += 1
self.watchedTime = xbmc.Player().getTime()
self.startTime = time.time()
if count >= 100:
self.startedWatching()
count = 0
else:
count = 0
Debug("Scrobbler stopping")
def playbackStarted(self, data):
if self.curVideo != None and self.curVideo != data['item']:
self.playbackEnded()
self.curVideo = data['item']
self.curVideoData = data
if self.curVideo != 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: #and 'id' in self.curVideo:
Debug("[Scrobbler] Watching: "+self.curVideo['type']) #+" - "+str(self.curVideo['id']))
try:
if not xbmc.Player().isPlayingVideo():
Debug("[Scrobbler] Suddenly stopped watching item")
return
time.sleep(1) # Wait for possible silent seek (caused by resuming)
self.watchedTime = xbmc.Player().getTime()
self.totalTime = xbmc.Player().getTotalTime()
if self.totalTime == 0:
if self.curVideo['type'] == 'movie':
self.totalTime = 90
elif self.curVideo['type'] == 'episode':
self.totalTime = 30
else:
self.totalTime = 1
#self.playlistLength = utilities.getPlaylistLengthFromXBMCPlayer(data['player']['playerid'])
# playerid 1 is video.
self.playlistLength = utilities.getPlaylistLengthFromXBMCPlayer(1)
if (self.playlistLength == 0):
Debug("[Scrobbler] Warning: Cant find playlist length?!, assuming that this item is by itself")
self.playlistLength = 1
except:
Debug("[Scrobbler] Suddenly stopped watching item, or error: "+str(sys.exc_info()[0]))
self.curVideo = None
self.startTime = 0
return
self.startTime = time.time()
self.startedWatching()
self.pinging = True
else:
self.curVideo = None
self.startTime = 0
def playbackResumed(self):
if self.pausedTime != 0:
p = time.time() - self.pausedTime
Debug("[Scrobbler] Resumed after: %s" % str(p))
self.pausedTime = 0
self.startedWatching()
def playbackPaused(self):
if self.startTime != 0:
self.watchedTime += time.time() - self.startTime
Debug("[Scrobbler] Paused after: "+str(self.watchedTime))
self.startTime = 0
self.pausedTime = time.time()
def playbackEnded(self):
if self.startTime != 0:
if self.curVideo == None:
Debug("[Scrobbler] Warning: Playback ended but video forgotten")
return
self.watchedTime += time.time() - self.startTime
self.pinging = False
if self.watchedTime != 0:
if 'type' in self.curVideo: #and 'id' in self.curVideo:
self.check()
ratingCheck(self.curVideo, self.watchedTime, self.totalTime, self.playlistLength)
self.watchedTime = 0
self.startTime = 0
self.curVideo = None
def startedWatching(self):
scrobbleMovieOption = __settings__.getSetting("scrobble_movie")
scrobbleEpisodeOption = __settings__.getSetting("scrobble_episode")
if self.curVideo['type'] == 'movie' and scrobbleMovieOption == 'true':
match = None
if 'id' in self.curVideo:
match = utilities.getMovieDetailsFromXbmc(self.curVideo['id'], ['imdbnumber', 'title', 'year'])
elif 'title' in self.curVideoData and 'year' in self.curVideoData:
match = {}
match['imdbnumber'] = ''
match['title'] = self.curVideoData['title']
match['year'] = self.curVideoData['year']
if match == None:
return
response = utilities.watchingMovieOnTrakt(match['imdbnumber'], match['title'], match['year'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if response != None:
Debug("[Scrobbler] Watch response: "+str(response))
elif self.curVideo['type'] == 'episode' and scrobbleEpisodeOption == 'true':
match = None
if 'id' in self.curVideo:
match = utilities.getEpisodeDetailsFromXbmc(self.curVideo['id'], ['showtitle', 'season', 'episode', 'tvshowid', 'uniqueid'])
elif 'showtitle' in self.curVideoData and 'season' in self.curVideoData and 'episode' in self.curVideoData:
match = {}
match['tvdb_id'] = None
match['year'] = None
match['showtitle'] = self.curVideoData['showtitle']
match['season'] = self.curVideoData['season']
match['episode'] = self.curVideoData['episode']
match['uniqueid'] = self.curVideoData['uniqueid']['unknown']
if match == None:
return
response = utilities.watchingEpisodeOnTrakt(match['tvdb_id'], match['showtitle'], match['year'], match['season'], match['episode'], match['uniqueid']['unknown'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if response != None:
Debug("[Scrobbler] Watch response: "+str(response))
def stoppedWatching(self):
scrobbleMovieOption = __settings__.getSetting("scrobble_movie")
scrobbleEpisodeOption = __settings__.getSetting("scrobble_episode")
if self.curVideo['type'] == 'movie' and scrobbleMovieOption == 'true':
response = utilities.cancelWatchingMovieOnTrakt()
if response != None:
Debug("[Scrobbler] Cancel watch response: "+str(response))
elif self.curVideo['type'] == 'episode' and scrobbleEpisodeOption == 'true':
response = utilities.cancelWatchingEpisodeOnTrakt()
if response != None:
Debug("[Scrobbler] Cancel watch response: "+str(response))
def scrobble(self):
scrobbleMovieOption = __settings__.getSetting("scrobble_movie")
scrobbleEpisodeOption = __settings__.getSetting("scrobble_episode")
if self.curVideo['type'] == 'movie' and scrobbleMovieOption == 'true':
match = None
if 'id' in self.curVideo:
match = utilities.getMovieDetailsFromXbmc(self.curVideo['id'], ['imdbnumber', 'title', 'year'])
elif 'title' in self.curVideoData and 'year' in self.curVideoData:
match = {}
match['imdbnumber'] = ''
match['title'] = self.curVideoData['title']
match['year'] = self.curVideoData['year']
if match == None:
return
response = utilities.scrobbleMovieOnTrakt(match['imdbnumber'], match['title'], match['year'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if response != None:
Debug("[Scrobbler] Scrobble response: "+str(response))
elif self.curVideo['type'] == 'episode' and scrobbleEpisodeOption == 'true':
match = None
if 'id' in self.curVideo:
match = utilities.getEpisodeDetailsFromXbmc(self.curVideo['id'], ['showtitle', 'season', 'episode', 'tvshowid', 'uniqueid'])
elif 'showtitle' in self.curVideoData and 'season' in self.curVideoData and 'episode' in self.curVideoData:
match = {}
match['tvdb_id'] = None
match['year'] = None
match['showtitle'] = self.curVideoData['showtitle']
match['season'] = self.curVideoData['season']
match['episode'] = self.curVideoData['episode']
match['uniqueid'] = self.curVideoData['uniqueid']['unknown']
if match == None:
return
response = utilities.scrobbleEpisodeOnTrakt(match['tvdb_id'], match['showtitle'], match['year'], match['season'], match['episode'], match['uniqueid']['unknown'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if response != None:
Debug("[Scrobbler] Scrobble response: "+str(response))
def check(self):
__settings__ = xbmcaddon.Addon("script.trakt") #read settings again, encase they have changed
scrobbleMinViewTimeOption = __settings__.getSetting("scrobble_min_view_time")
Debug("watched: " + str(self.watchedTime) + " / " + str(self.totalTime))
if (self.watchedTime/self.totalTime)*100>=float(scrobbleMinViewTimeOption):
self.scrobble()
else:
self.stoppedWatching()