summaryrefslogtreecommitdiff
path: root/plugins/Scrobbler.py
blob: c29b6be40c6833a51330fbb931b0a7955fb5e85b (plain)
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
import time
import datetime

from clPlugin import *

SCROBBLER_USERNAME_DEFAULT=''
SCROBBLER_PASSWORD_DEFAULT=''

# TODO cached failed submissions

class pluginScrobbler(Plugin):
	submitted=False
	time=None
	loggedIn=False
	def __init__(self, winMain):
		Plugin.__init__(self, winMain, 'Scrobbler')
		self.addMontyListener('onSongChange', self.onSongChange)
		self.addMontyListener('onTimeChange', self.onTimeChange)

	def _load(self):
		self._login()
	
	def _login(self):
		self.submitting=False
		self.loggedIn=False
		if self._username():
			self.normal("logging in %s"%(self._username()))
			try:
				login(self._username(), self._password())
				self.loggedIn=True
			except Exception, e:
				self.normal("failed to login: "+str(e))
		else:
			self.debug("no username provided, not logging in")

	def _username(self):
		return self.getSetting('username')
	def _password(self):
		return self.getSetting('password')
	def onTimeChange(self, params):
		if self.submitted==False and self.loggedIn:
			song=monty.getCurrentSong()
			if song.getTag('time')>30:
				if int(params['newTime'])>int(song.getTag('time'))/2 \
						or int(params['newTime'])>240:
							self.normal("submitting song")
							submit(song.getArtist(), song.getTitle(), self.time, 'P', '', song.getTag('time'), song.getAlbum(), song.getTrack(), False)
							self.debug("flushing ...")
							try:
								flush()
								self.submitted=True
							except SessionError, e:
								self.important("failed to submit song1 - %s"%(e))
								self.extended("Logging in ...")
								self._login()
								try:
									flush()
									self.submitted=True
								except Exception, e:
									self.important("failed to submit song2 - %s"%(e))
							if self.submitted:
								self.debug("flushed")
							else:
								self.important("failed to submit the song!")
		
	def onSongChange(self, params):
		if self.loggedIn==False:
			return
		self.time=int(time.mktime(datetime.utcnow().timetuple()))
		self.submitted=False
		song=monty.getCurrentSong()
		# for loop: in case of SessionError, we need to retry once ...
		for i in [0, 1]:
			try:
				self.extended("submitting now playing")
				now_playing(song.getArtist(), song.getTitle(), song.getAlbum(), "", song.getTrack())
				self.debug("submitted")
				break
			except AuthError, e:
				self.important("failed to submit playing song - %s"%(e))
				break
			except SessionError, e:
				self.normal("session error")
				self._login()
	def _getSettings(self):
		return [
			['scrobbler.username', 'Username', 'Username to submit to last.fm.', QtGui.QLineEdit(self._username())],
			['scrobbler.password', 'Password', 'Password to user to submit. Note that the password is stored *unencrypted* to file.', QtGui.QLineEdit(self._password())],
		]
	def afterSaveSettings(self):
		self._login()
	def getInfo(self):
		return "Submits tracks to last.fm"




# Big thank you,
# http://exhuma.wicked.lu/projects/python/scrobbler/ !"

"""
A pure-python library to assist sending data to AudioScrobbler (the LastFM
backend)
"""
import urllib, urllib2
from time import mktime
from datetime import datetime, timedelta
from md5 import md5

SESSION_ID = None
POST_URL	= None
NOW_URL	= None
HARD_FAILS = 0
LAST_HS	= None	# Last handshake time
HS_DELAY	= 0		# wait this many seconds until next handshake
SUBMIT_CACHE = []
MAX_CACHE	= 5		# keep only this many songs in the cache
PROTOCOL_VERSION = '1.2'

class BackendError(Exception):
	"Raised if the AS backend does something funny"
	pass
class AuthError(Exception):
	"Raised on authencitation errors"
	pass
class PostError(Exception):
	"Raised if something goes wrong when posting data to AS"
	pass
class SessionError(Exception):
	"Raised when problems with the session exist"
	pass
class ProtocolError(Exception):
	"Raised on general Protocol errors"
	pass

def login( user, password, client=('tst', '1.0') ):
	"""Authencitate with AS (The Handshake)

	@param user:	 The username
	@param password: The password
	@param client:	Client information (see http://www.audioscrobbler.net/development/protocol/ for more info)
	@type	client:	Tuple: (client-id, client-version)"""
	global LAST_HS, SESSION_ID, POST_URL, NOW_URL, HARD_FAILS, HS_DELAY, PROTOCOL_VERSION

	if LAST_HS is not None:
		next_allowed_hs = LAST_HS + timedelta(seconds=HS_DELAY)
		if datetime.now() < next_allowed_hs:
		 delta = next_allowed_hs - datetime.now()
		 raise ProtocolError("""Please wait another %d seconds until next handshake (login) attempt.""" % delta.seconds)

	LAST_HS = datetime.now()

	tstamp = int(mktime(datetime.now().timetuple()))
	url	= "http://post.audioscrobbler.com/"
	pwhash = md5(password).hexdigest()
	token	= md5( "%s%d" % (pwhash, int(tstamp))).hexdigest()
	values = {
		 'hs': 'true',
		 'p' : PROTOCOL_VERSION,
		 'c': client[0],
		 'v': client[1],
		 'u': user,
		 't': tstamp,
		 'a': token
		 }
	data = urllib.urlencode(values)
	req = urllib2.Request("%s?%s" % (url, data) )
	response = urllib2.urlopen(req)
	result = response.read()
	lines = result.split('\n')


	if lines[0] == 'BADAUTH':
		raise AuthError('Bad username/password')

	elif lines[0] == 'BANNED':
		raise Exception('''This client-version was banned by Audioscrobbler. Please contact the author of this module!''')

	elif lines[0] == 'BADTIME':
		raise ValueError('''Your system time is out of sync with Audioscrobbler.Consider using an NTP-client to keep you system time in sync.''')

	elif lines[0].startswith('FAILED'):
		handle_hard_error()
		raise BackendError("Authencitation with AS failed. Reason: %s" %
			lines[0])

	elif lines[0] == 'OK':
		# wooooooohooooooo. We made it!
		SESSION_ID = lines[1]
		NOW_URL	= lines[2]
		POST_URL	= lines[3]
		HARD_FAILS = 0

	else:
		# some hard error
		handle_hard_error()

def handle_hard_error():
	"Handles hard errors."
	global SESSION_ID, HARD_FAILS, HS_DELAY

	if HS_DELAY == 0:
		HS_DELAY = 60
	elif HS_DELAY < 120*60:
		HS_DELAY *= 2
	if HS_DELAY > 120*60:
		HS_DELAY = 120*60

	HARD_FAILS += 1
	if HARD_FAILS == 3:
		SESSION_ID = None

def now_playing( artist, track, album="", length="", trackno="", mbid="" ):
	"""Tells audioscrobbler what is currently running in your player. This won't
	affect the user-profile on last.fm. To do submissions, use the "submit"
	method

	@param artist:	The artist name
	@param track:	The track name
	@param album:	The album name
	@param length:	The song length in seconds
	@param trackno: The track number
	@param mbid:	The MusicBrainz Track ID
	@return: True on success, False on failure"""

	global SESSION_ID, NOW_URL

	if SESSION_ID is None:
		raise AuthError("Please 'login()' first. (No session available)")

	if POST_URL is None:
		raise PostError("Unable to post data. Post URL was empty!")

	if length != "" and type(length) != type(1):
		raise TypeError("length should be of type int")

	if trackno != "" and type(trackno) != type(1):
		raise TypeError("trackno should be of type int")

	values = {'s': SESSION_ID,
			 'a': unicode(artist).encode('utf-8'),
			 't': unicode(track).encode('utf-8'),
			 'b': unicode(album).encode('utf-8'),
			 'l': length,
			 'n': trackno,
			 'm': mbid }

	data = urllib.urlencode(values)
	req = urllib2.Request(NOW_URL, data)
	response = urllib2.urlopen(req)
	result = response.read()

	if result.strip() == "OK":
		return True
	elif result.strip() == "BADSESSION" :
		raise SessionError('Invalid session')
	else:
		return False

def submit(artist, track, time, source='P', rating="", length="", album="",
		trackno="", mbid="", autoflush=False):
	"""Append a song to the submission cache. Use 'flush()' to send the cache to
	AS. You can also set "autoflush" to True.

	From the Audioscrobbler protocol docs:
	---------------------------------------------------------------------------

	The client should monitor the user's interaction with the music playing
	service to whatever extent the service allows. In order to qualify for
	submission all of the following criteria must be met:

	1. The track must be submitted once it has finished playing. Whether it has
		finished playing naturally or has been manually stopped by the user is
		irrelevant.
	2. The track must have been played for a duration of at least 240 seconds or
		half the track's total length, whichever comes first. Skipping or pausing
		the track is irrelevant as long as the appropriate amount has been played.
	3. The total playback time for the track must be more than 30 seconds. Do
		not submit tracks shorter than this.
	4. Unless the client has been specially configured, it should not attempt to
		interpret filename information to obtain metadata instead of tags (ID3,
		etc).

	@param artist: Artist name
	@param track:	Track name
	@param time:	Time the track *started* playing in the UTC timezone (see
					datetime.utcnow()).

					Example: int(time.mktime(datetime.utcnow()))
	@param source: Source of the track. One of:
					'P': Chosen by the user
					'R': Non-personalised broadcast (e.g. Shoutcast, BBC Radio 1)
					'E': Personalised recommendation except Last.fm (e.g.
						Pandora, Launchcast)
					'L': Last.fm (any mode). In this case, the 5-digit Last.fm
						recommendation key must be appended to this source ID to
						prove the validity of the submission (for example,
						"L1b48a").
					'U': Source unknown
	@param rating: The rating of the song. One of:
					'L': Love (on any mode if the user has manually loved the
						track)
					'B': Ban (only if source=L)
					'S': Skip (only if source=L)
					'':	Not applicable
	@param length: The song length in seconds
	@param album:	The album name
	@param trackno:The track number
	@param mbid:	MusicBrainz Track ID
	@param autoflush: Automatically flush the cache to AS?
	"""

	global SUBMIT_CACHE, MAX_CACHE

	source = source.upper()
	rating = rating.upper()

	if source == 'L' and (rating == 'B' or rating == 'S'):
		raise ProtocolError("""You can only use rating 'B' or 'S' on source 'L'.See the docs!""")

	if source == 'P' and length == '':
		raise ProtocolError("""Song length must be specified when using 'P' as source!""")

	if type(time) != type(1):
		raise ValueError("""The time parameter must be of type int (unix timestamp). Instead it was %s""" % time)

	SUBMIT_CACHE.append(
		 { 'a': unicode(artist).encode('utf-8'),
			't': unicode(track).encode('utf-8'),
			'i': time,
			'o': source,
			'r': rating,
			'l': length,
			'b': unicode(album).encode('utf-8'),
			'n': trackno,
			'm': mbid
			}
		 )

	if autoflush or len(SUBMIT_CACHE) >= MAX_CACHE:
		flush()

def flush():
	"Sends the cached songs to AS."
	global SUBMIT_CACHE

	values = {}

	for i, item in enumerate(SUBMIT_CACHE):
		for key in item:
		 values[key + "[%d]" % i] = item[key]

	values['s'] = SESSION_ID

	data = urllib.urlencode(values)
	req = urllib2.Request(POST_URL, data)
	response = urllib2.urlopen(req)
	result = response.read()
	lines = result.split('\n')

	if lines[0] == "OK":
		SUBMIT_CACHE = []
		return True
	elif lines[0] == "BADSESSION" :
		raise SessionError('Invalid session')
	elif lines[0].startswith('FAILED'):
		handle_hard_error()
		raise BackendError("Authencitation with AS failed. Reason: %s" %
			lines[0])
	else:
		# some hard error
		handle_hard_error()
		return False


if __name__ == "__main__":
	login( 'user', 'password' )
	submit(
		 'De/Vision',
		 'Scars',
		 1192374052,
		 source='P',
		 length=3*60+44
		 )
	submit(
		 'Spineshank',
		 'Beginning of the End',
		 1192374052+(5*60),
		 source='P',
		 length=3*60+32
		 )
	submit(
		 'Dry Cell',
		 'Body Crumbles',
		 1192374052+(10*60),
		 source='P',
		 length=3*60+3
		 )
	print flush()