New API.
[clinton/xbmc-groove.git] / resources / lib / GroovesharkAPI.py
1 import socket, hmac, urllib, urllib2, pprint, md5, uuid, re, hashlib, time, random, os, pickle
2
3 # GrooveAPI constants
4 THUMB_URL = 'http://beta.grooveshark.com/static/amazonart/'
5 THUMB_URL_DEFAULT = 'http://grooveshark.com/webincludes/logo/Grooveshark_Logo_No-Text.png'
6 SONG_LIMIT = 25
7 ALBUM_LIMIT = 15
8 ARTIST_LIMIT = 15
9
10 # GrooveSong constants
11 DOMAIN = "grooveshark.com"
12 HOME_URL = "http://listen." + DOMAIN
13 TOKEN_URL = "http://cowbell." + DOMAIN + "/more.php"
14 API_URL = "http://cowbell." + DOMAIN + "/more.php"
15 SERVICE_URL = "http://cowbell." + DOMAIN + "/service.php"
16
17 CLIENT_NAME = "gslite" #htmlshark #jsqueue
18 CLIENT_VERSION = "20101012.37" #"20100831.25"
19 HEADERS = {"Content-Type": "application/json",
20 "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)",
21 "Referer": "http://listen.grooveshark.com/main.swf?cowbell=fe87233106a6cef919a1294fb2c3c05f"}
22
23 RE_SESSION = re.compile('"sessionID":"\s*?([A-z0-9]+)"') #re.compile('sessionID:\s*?\'([A-z0-9]+)\',')
24 RANDOM_CHARS = "1234567890abcdef"
25
26 # Get a song
27 class GrooveSong:
28
29 def __init__(self, cacheDir):
30
31 import simplejson
32 self.simplejson = simplejson
33
34 self.cacheDir = cacheDir
35 self._lastTokenTime = 0
36 self.uuid = self._getUUID()
37 self.sessionID = self._getSession(HOME_URL)
38 if self.sessionID == None:
39 raise StandardError("Cannot get session id")
40
41 # The actual call to the API
42 def _callRemote(self, method, params, type="default"):
43 postData = {
44 "header": {
45 "client": CLIENT_NAME,
46 "clientRevision": CLIENT_VERSION,
47 "uuid": self.uuid,
48 "session": self.sessionID},
49 "country": {"IPR":"1021", "ID":"223", "CC1":"0", "CC2":"0", "CC3":"0", "CC4":"2147483648"},
50 "privacy": 1,
51 "parameters": params,
52 "method": method}
53
54 token = self._getMethodToken(method)
55 if token == None:
56 raise StandardError("Cannot get token")
57
58 postData["header"]["token"] = token
59 if type == "service":
60 url = SERVICE_URL + "?" + method
61 else:
62 url = API_URL + "?" + method
63
64 postData = self.simplejson.dumps(postData)
65 print "GrooveSong URL: " + url
66 request = urllib2.Request(url, postData, HEADERS)
67
68 response = urllib2.urlopen(request).read()
69 try:
70 response = self.simplejson.loads(response)
71 print "GrooveSong Response..."
72 pprint.pprint(response)
73 except:
74 raise StandardError("API error: " + response)
75 try:
76 response["fault"]
77 except KeyError:
78 return response
79 else:
80 raise StandardError("API error: " + response["fault"]["message"])
81
82 # Generate a random uuid
83 def _getUUID(self):
84 return str(uuid.uuid4())
85
86 # Make a token ready for a request header
87 def _getMethodToken(self, method):
88 if (time.time() - self._lastTokenTime) >= 1000:
89 self._token = self._getCommunicationToken()
90 if self._token == None:
91 return None
92 self._lastTokenTime = time.time()
93
94 randomChars = ""
95 while 6 > len(randomChars):
96 randomChars = randomChars + random.choice(RANDOM_CHARS)
97
98 token = hashlib.sha1(method + ":" + self._token + ":quitStealinMahShit:" + randomChars).hexdigest()
99 return randomChars + token
100
101 # Generate a communication token
102 def _getCommunicationToken(self):
103 params = {"secretKey": self._getSecretKey(self.sessionID)}
104 postData = {
105 "header": {
106 "client": CLIENT_NAME,
107 "clientRevision": CLIENT_VERSION,
108 "uuid": self.uuid,
109 "session": self.sessionID},
110 "country": {"IPR":"1021", "ID":"223", "CC1":"0", "CC2":"0", "CC3":"0", "CC4":"2147483648"},
111 "privacy": 1,
112 "parameters": params,
113 "method": "getCommunicationToken"}
114
115 postData = self.simplejson.dumps(postData)
116 request = urllib2.Request(TOKEN_URL, postData, HEADERS)
117 response = urllib2.urlopen(request).read()
118 try:
119 response = self.simplejson.loads(response)
120 except:
121 raise StandardError("API error: " + response)
122 try:
123 response["fault"]
124 except KeyError:
125 return response["result"]
126 else:
127 return None
128
129 # Generate a secret key from a sessionID
130 def _getSecretKey(self, sessionID):
131 return hashlib.md5(sessionID).hexdigest()
132
133 # Get a session id from some HTML
134 def _getSession(self, html):
135 html = urllib2.urlopen(HOME_URL).read()
136 session = RE_SESSION.search(html)
137 if session:
138 return session.group(1)
139 else:
140 return None
141
142 # Gets a stream key and host to get song content
143 def _getStreamDetails(self, songID):
144 params = {
145 "songID": songID,
146 "prefetch": False,
147 "mobile": False,
148 "country": {"IPR":"1021","ID":"223", "CC1":"0", "CC2":"0", "CC3":"0", "CC4":"2147483648"}
149 }
150 response = self._callRemote("getStreamKeyFromSongIDEx", params)
151 self._lastStreamKey = response["result"]["streamKey"]
152 self._lastStreamServer = response["result"]["ip"]
153 self._lastStreamServerID = response["result"]["streamServerID"]
154
155 # Tells Grooveshark you have downloaded a song
156 def _markSongDownloaded(self, songID):
157 params = {
158 "streamKey": self._lastStreamKey,
159 "streamServerID": self._lastStreamServerID,
160 "songID": songID}
161 self._callRemote("markSongDownloaded", params)
162
163 # Download a song to a temporary file
164 def getSongURL(self, songID):
165 filename = os.path.join(self.cacheDir, songID + '.mp3')
166 if os.path.isfile(filename) == False:
167 self._getStreamDetails(songID)
168 postData = {"streamKey": self._lastStreamKey}
169 postData = urllib.urlencode(postData)
170 urllib.FancyURLopener().retrieve( "http://" + self._lastStreamServer + "/stream.php", filename, data=postData)
171 self._markSongDownloaded(songID)
172 return filename
173
174
175 # Main API
176 class GrooveAPI:
177
178 sessionID = ''
179 userID = 0
180 host = 'api.grooveshark.com'
181 lastSessionTime = 0
182
183 # Constructor
184 def __init__(self, cacheDir):
185 import simplejson
186 self.simplejson = simplejson
187 socket.setdefaulttimeout(40)
188 self.cacheDir = cacheDir
189 self._getSavedSessionID()
190 # session ids last 1 week
191 if self.sessionID == '' or time.time()- self.lastSessionTime >= 6*86400:
192 self.sessionID = self._getSessionID()
193 if self.sessionID == '':
194 raise StandardError('Failed to get session id')
195 else:
196 print "New session id: " + self.sessionID
197 self._setSavedSessionID()
198
199 # Sort keys
200 def _keySort(self, d):
201 return [(k,d[k]) for k in sorted(d.keys())]
202
203 # Make a message sig
204 def _createMessageSig(self, method, params, secret):
205 args = self._keySort(params);
206 data = '';
207 for arg in args:
208 data += str(arg[0])
209 data += str(arg[1])
210 data = method + data
211
212 h = hmac.new(secret, data)
213 return h.hexdigest()
214
215 # The actual call to the API
216 def _callRemote(self, method, params = {}):
217 url = 'http://%s/ws/2.1/?method=%s&%s&wsKey=wordpress&sig=%s&format=json' % (self.host, method, urllib.urlencode(params), self._createMessageSig(method, params, 'd6c59291620c6eaa5bf94da08fae0ecc'))
218 print url
219 req = urllib2.Request(url)
220 response = urllib2.urlopen(req)
221 result = response.read()
222 pprint.pprint(result)
223 response.close()
224 try:
225 result = self.simplejson.loads(result)
226 return result
227 except:
228 return []
229
230 # Get a session id
231 def _getSessionID(self):
232 params = {}
233 result = self._callRemote('startSession', params)
234 self.lastSessionTime = time.time()
235 return result['result']['sessionID']
236
237 def _getSavedSessionID(self):
238 path = os.path.join(self.cacheDir, 'session', 'session.dmp')
239 try:
240 f = open(path, 'rb')
241 session = pickle.load(f)
242 self.sessionID = session['sessionID']
243 self.lastSessionTime = session['lastSessionTime']
244 f.close()
245 except:
246 self.sessionID = ''
247 self.lastSessionTime = 0
248 pass
249
250 def _setSavedSessionID(self):
251 try:
252 dir = os.path.join(self.cacheDir, 'session')
253 # Create the 'data' directory if it doesn't exist.
254 if not os.path.exists(dir):
255 os.makedirs(dir)
256 path = os.path.join(dir, 'session.dmp')
257 f = open(path, 'wb')
258 session = { 'sessionID' : self.sessionID, 'lastSessionTime' : self.lastSessionTime}
259 pickle.dump(session, f, protocol=pickle.HIGHEST_PROTOCOL)
260 f.close()
261 except:
262 print "An error occured during save session"
263 pass
264
265 # Make user authentication token
266 def _getUserToken(self, username, password):
267 return md5.new(username.lower() + md5.new(password).hexdigest()).hexdigest()
268
269 # Authenticates the user for current API session
270 def _authenticateUser(self, username, token):
271 params = {'sessionID': self.sessionID, 'username': username, 'token': token}
272 result = self._callRemote('authenticateUser', params)
273 return result['result']['UserID']
274
275 # Login
276 def login(self, username, password):
277 token = self._getUserToken(username, password)
278 self.userID = self._authenticateUser(username, token)
279 return self.userID
280
281 # Logs the user out
282 def logout(self):
283 self._callRemote('logout', {'sessionID' : self.sessionID})
284
285 # Return user id
286 def getUserID(self):
287 return self.userID
288
289 # Search for albums
290 def getArtistSearchResults(self, query, limit=ARTIST_LIMIT):
291 result = self._callRemote('getArtistSearchResults', {'query' : query,'limit' : limit})
292 if 'result' in result:
293 return self._parseArtists(result)
294 else:
295 return []
296
297 # Search for albums
298 def getAlbumSearchResults(self, query, limit=ALBUM_LIMIT):
299 result = self._callRemote('getAlbumSearchResults', {'query' : query,'limit' : limit})
300 if 'result' in result:
301 return self._parseAlbums(result)
302 else:
303 return []
304
305 # Search for songs
306 def getSongSearchResults(self, query, limit=SONG_LIMIT):
307 result = self._callRemote('getSongSearchResultsEx', {'query' : query, 'limit' : limit})
308 if 'result' in result:
309 return self._parseSongs(result)
310 else:
311 return []
312
313 # Gets the popular songs
314 def getPopularSongsToday(self, limit=SONG_LIMIT):
315 result = self._callRemote('getPopularSongsToday', {'limit' : limit})
316 if 'result' in result:
317 return self._parseSongs(result)
318 else:
319 return []
320
321 # Gets the favorite songs of the logged-in user
322 def getUserFavoriteSongs(self):
323 if (self.userID == 0):
324 return [];
325 result = self._callRemote('getUserFavoriteSongs', {'sessionID' : self.sessionID})
326 if 'result' in result:
327 return self._parseSongs(result)
328 else:
329 return []
330
331 # Add song to user favorites
332 def addUserFavoriteSong(self, songID):
333 if (self.userID == 0):
334 return False;
335 result = self._callRemote('addUserFavoriteSong', {'sessionID' : self.sessionID, 'songID' : songID})
336 return result['result']['success']
337
338 # Get the url to link to a song on Grooveshark
339 def getSongURLFromSongID(self, songID):
340 song = GrooveSong(self.cacheDir)
341 return song.getSongURL(songID)
342
343 # Get the url to link to a song on Grooveshark
344 def getSongInfo(self, songID):
345 result = self._callRemote('getSongInfoEx', {'songID' : songID})
346 if 'result' in result and 'SongID' in result['result']:
347 info = result['result']
348 if 'CoverArtFilename' in info and info['CoverArtFilename'] != None:
349 info['CoverArtFilename'] = THUMB_URL+info['CoverArtFilename'].encode('ascii', 'ignore')
350 else:
351 info['CoverArtFilename'] = THUMB_URL_DEFAULT
352 return info
353 else:
354 return ''
355
356 # Gets the playlists of the logged-in user
357 def getUserPlaylists(self):
358 if (self.userID == 0):
359 return [];
360 result = self._callRemote('getUserPlaylists', {'sessionID' : self.sessionID})
361 if 'result' in result:
362 return self._parsePlaylists(result)
363 else:
364 return []
365
366 # Creates a playlist with songs
367 def createPlaylist(self, name, songIDs):
368 result = self._callRemote('createPlaylist', {'name' : name, 'songIDs' : songIDs, 'sessionID' : self.sessionID})
369 if 'result' in result and result['result']['success'] == True:
370 return result['result']['PlaylistID']
371 elif 'errors' in result:
372 return 0
373
374 # Sets the songs for a playlist
375 def setPlaylistSongs(self, playlistID, songIDs):
376 result = self._callRemote('setPlaylistSongs', {'playlistID' : playlistID, 'songIDs' : songIDs, 'sessionID' : self.sessionID})
377 if 'result' in result and result['result']['success'] == True:
378 return True;
379 else:
380 return False;
381
382 # Gets the songs of a playlist
383 def getPlaylistSongs(self, playlistID):
384 result = self._callRemote('getPlaylistSongs', {'playlistID' : playlistID});
385 if 'result' in result:
386 return self._parseSongs(result)
387 else:
388 return []
389
390 # Extract song data
391 def _parseSongs(self, items):
392 if 'result' in items:
393 i = 0
394 list = []
395 if 'songs' in items['result']:
396 l = len(items['result']['songs'])
397 index = 'songs'
398 elif 'song' in items['result']:
399 l = 1
400 index = 'song'
401 else:
402 l = len(items['result'])
403 index = ''
404 while(i < l):
405 if index == 'songs':
406 s = items['result'][index][i]
407 elif index == 'song':
408 s = items['result'][index]
409 else:
410 s = items['result'][i]
411 if 'CoverArtFilename' not in s:
412 info = self.getSongInfo(s['SongID'])
413 coverart = info['CoverArtFilename']
414 elif s['CoverArtFilename'] != None:
415 coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
416 else:
417 coverart = THUMB_URL_DEFAULT
418 list.append([s['SongName'].encode('ascii', 'ignore'),\
419 s['SongID'],\
420 s['AlbumName'].encode('ascii', 'ignore'),\
421 s['AlbumID'],\
422 s['ArtistName'].encode('ascii', 'ignore'),\
423 s['ArtistID'],\
424 coverart])
425 i = i + 1
426 return list
427 else:
428 return []
429
430 # Extract artist data
431 def _parseArtists(self, items):
432 if 'result' in items:
433 i = 0
434 list = []
435 artists = items['result']['artists']
436 while(i < len(artists)):
437 s = artists[i]
438 list.append([s['ArtistName'].encode('ascii', 'ignore'),\
439 s['ArtistID']])
440 i = i + 1
441 return list
442 else:
443 return []
444
445 # Extract album data
446 def _parseAlbums(self, items):
447 if 'result' in items:
448 i = 0
449 list = []
450 albums = items['result']['albums']
451 while(i < len(albums)):
452 s = albums[i]
453 if 'CoverArtFilename' in s and s['CoverArtFilename'] != None:
454 coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
455 else:
456 coverart = THUMB_URL_DEFAULT
457 list.append([s['ArtistName'].encode('ascii', 'ignore'),\
458 s['ArtistID'],\
459 s['AlbumName'].encode('ascii', 'ignore'),\
460 s['AlbumID'],\
461 coverart])
462 i = i + 1
463 return list
464 else:
465 return []
466
467 def _parsePlaylists(self, items):
468 if 'result' in items:
469 i = 0
470 list = []
471 playlists = items['result']
472 while(i < len(playlists)):
473 s = playlists[i]
474 list.append([s['PlaylistID'],\
475 s['Name'].encode('ascii', 'ignore')])
476 i = i + 1
477 return list
478 else:
479 return []
480
481
482
483 # Test
484 import sys
485 res = []
486 groovesharkApi = GrooveAPI('/tmp')
487 res = groovesharkApi.login(sys.argv[1], sys.argv[2])
488 #res = groovesharkApi.getSongSearchResults('jimmy jazz', 3)
489 #res = groovesharkApi.getPopularSongsToday()
490 #res = groovesharkApi.getSongURLFromSongID('27425375')
491 #res = groovesharkApi.getAlbumSearchResults('london calling', 3)
492 #res = groovesharkApi.getArtistSearchResults('the clash', 3)
493 #res = groovesharkApi.getUserFavoriteSongs()
494 res = groovesharkApi.getUserPlaylists()
495 #res = groovesharkApi.getSongInfo('27425375')
496 #res = groovesharkApi.getPlaylistSongs(40902662)
497 #res = groovesharkApi.addUserFavoriteSong('27425375')
498
499 pprint.pprint(res)
500
501
502