This repository has been archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
scrobbler.py
172 lines (152 loc) · 7.73 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
# -*- coding: utf-8 -*-
#
import os
import xbmc,xbmcaddon,xbmcgui
import threading
import time
from utilities import *
from rating import *
__author__ = "Ralph-Gordon Paul, Adrian Cowan"
__credits__ = ["Ralph-Gordon Paul", "Adrian Cowan", "Justin Nemeth", "Sean Rudford"]
__license__ = "GPL"
__maintainer__ = "Ralph-Gordon Paul"
__email__ = "[email protected]"
__status__ = "Production"
# read settings
__settings__ = xbmcaddon.Addon( "script.traktutilities" )
__language__ = __settings__.getLocalizedString
apikey = '0a698a20b222d0b8637298f6920bf03a' # scrobbling requires this dev key
username = __settings__.getSetting("username")
pwd = sha.new(__settings__.getSetting("password")).hexdigest()
debug = __settings__.getSetting( "debug" )
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
class Scrobbler(threading.Thread):
totalTime = 1
watchedTime = 0
startTime = 0
curVideo = 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
while (not (self.abortRequested or xbmc.abortRequested)):
time.sleep(5) # 1min wait
#Debug("[Scrobbler] Cycling " + str(self.pinging))
if self.pinging:
count += 1
if count>=100:
Debug("[Scrobbler] Pinging watching "+str(self.curVideo))
tmp = time.time()
self.watchedTime += tmp - self.startTime
self.startTime = tmp
self.startedWatching()
count = 0
else:
count = 0
Debug("Scrobbler stopping")
def playbackStarted(self, data):
self.curVideo = data['item']
if self.curVideo <> None:
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 = getPlaylistLengthFromXBMCPlayer(data['player']['playerid'])
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 playbackPaused(self):
if self.startTime <> 0:
self.watchedTime += time.time() - self.startTime
Debug("[Scrobbler] Paused after: "+str(self.watchedTime))
self.startTime = 0
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
def startedWatching(self):
scrobbleMovieOption = __settings__.getSetting("scrobble_movie")
scrobbleEpisodeOption = __settings__.getSetting("scrobble_episode")
if self.curVideo['type'] == 'movie' and scrobbleMovieOption == 'true':
match = getMovieDetailsFromXbmc(self.curVideo['id'], ['imdbnumber','title','year'])
if match == None:
return
responce = watchingMovieOnTrakt(match['imdbnumber'], match['title'], match['year'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if responce != None:
Debug("[Scrobbler] Watch responce: "+str(responce));
elif self.curVideo['type'] == 'episode' and scrobbleEpisodeOption == 'true':
match = getEpisodeDetailsFromXbmc(self.curVideo['id'], ['showtitle', 'season', 'episode'])
if match == None:
return
responce = watchingEpisodeOnTrakt(None, match['showtitle'], None, match['season'], match['episode'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if responce != None:
Debug("[Scrobbler] Watch responce: "+str(responce));
def stoppedWatching(self):
scrobbleMovieOption = __settings__.getSetting("scrobble_movie")
scrobbleEpisodeOption = __settings__.getSetting("scrobble_episode")
if self.curVideo['type'] == 'movie' and scrobbleMovieOption == 'true':
responce = cancelWatchingMovieOnTrakt()
if responce != None:
Debug("[Scrobbler] Cancel watch responce: "+str(responce));
elif self.curVideo['type'] == 'episode' and scrobbleEpisodeOption == 'true':
responce = cancelWatchingEpisodeOnTrakt()
if responce != None:
Debug("[Scrobbler] Cancel watch responce: "+str(responce));
def scrobble(self):
scrobbleMovieOption = __settings__.getSetting("scrobble_movie")
scrobbleEpisodeOption = __settings__.getSetting("scrobble_episode")
if self.curVideo['type'] == 'movie' and scrobbleMovieOption == 'true':
match = getMovieDetailsFromXbmc(self.curVideo['id'], ['imdbnumber','title','year'])
if match == None:
return
responce = scrobbleMovieOnTrakt(match['imdbnumber'], match['title'], match['year'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if responce != None:
Debug("[Scrobbler] Scrobble responce: "+str(responce));
elif self.curVideo['type'] == 'episode' and scrobbleEpisodeOption == 'true':
match = getEpisodeDetailsFromXbmc(self.curVideo['id'], ['showtitle', 'season', 'episode'])
if match == None:
return
responce = scrobbleEpisodeOnTrakt(None, match['showtitle'], None, match['season'], match['episode'], self.totalTime/60, int(100*self.watchedTime/self.totalTime))
if responce != None:
Debug("[Scrobbler] Scrobble responce: "+str(responce));
def check(self):
__settings__ = xbmcaddon.Addon( "script.traktutilities" ) #read settings again, encase they have changed
scrobbleMinViewTimeOption = __settings__.getSetting("scrobble_min_view_time")
if (self.watchedTime/self.totalTime)*100>=float(scrobbleMinViewTimeOption):
self.scrobble()
else:
self.stoppedWatching()