9a91245223cc669533b9237bd1eb10f6e5f12981
[clinton/xbmc-groove.git] / resources / lib / GroovesharkAPI.py
1 import socket, urllib, urllib2, pprint, md5, os, pickle, tempfile, time, re, simplejson, base64
2 from blowfish import Blowfish
3
4 SESSION_EXPIRY = 1209600 # 2 weeks
5
6 # Web app
7 WEB_APP_URL = "http://xbmc-groove.appspot.com/"
8
9 # GrooveAPI constants
10 THUMB_URL = 'http://beta.grooveshark.com/static/amazonart/m'
11 SONG_LIMIT = 25
12 ALBUM_LIMIT = 15
13 ARTIST_LIMIT = 15
14 SONG_SUFFIX = '.mp3'
15
16 # Main API
17 class GrooveAPI:
18
19 _ip = '0.0.0.0'
20 _country = ''
21 _sessionID = ''
22 _userID = 0
23 _lastSessionTime = 0
24 _lastStreamKey = ''
25 _lastStreamServerID = ''
26 _key = md5.new(os.path.basename("GroovesharkAPI.py")).hexdigest()
27
28 # Constructor
29 def __init__(self):
30
31 self.simplejson = simplejson
32 self.cacheDir = os.path.join(tempfile.gettempdir(), 'groovesharkapi')
33 if os.path.isdir(self.cacheDir) == False:
34 os.makedirs(self.cacheDir)
35 print "Made " + self.cacheDir
36 self._getSavedSession()
37 # session ids last 2 weeks
38 if self._sessionID == '' or time.time()- self._lastSessionTime >= SESSION_EXPIRY:
39 self._sessionID = self._getSessionID()
40 if self._sessionID == '':
41 raise StandardError('Failed to get session id')
42 else:
43 print "New GrooveAPI session id: " + self._sessionID
44 self._ip = self._getIP()
45 self._country = self._getCountry()
46 self._setSavedSession()
47
48 # Call to API
49 def _callRemote(self, method, params):
50 try:
51 res = self._getRemote(method, params)
52 url = res['url']
53 postData = res['postData']
54 except:
55 print "Failed to get request URL and post data"
56 return []
57 try:
58 req = urllib2.Request(url, postData)
59 response = urllib2.urlopen(req)
60 result = response.read()
61 print "Response..."
62 pprint.pprint(result)
63 response.close()
64 result = simplejson.loads(result)
65 return result
66 except urllib2.HTTPError, e:
67 print "HTTP error " + e.code
68 except urllib2.URLError, e:
69 print "URL error " + e.reason
70 except:
71 print "Request to Grooveshark API failed"
72 return []
73
74
75 # Get the API call
76 def _getRemote(self, method, params = {}):
77 postData = { "method": method, "sessionid": self._sessionID, "parameters": params }
78 postData = simplejson.dumps(postData)
79
80 cipher = Blowfish(self._key)
81 cipher.initCTR()
82 encryptedPostData = cipher.encryptCTR(postData)
83 encryptedPostData = base64.urlsafe_b64encode(encryptedPostData)
84 url = WEB_APP_URL + "?postData=" + encryptedPostData
85 req = urllib2.Request(url)
86 response = urllib2.urlopen(req)
87 result = response.read()
88 print "Request..."
89 pprint.pprint(result)
90 response.close()
91 try:
92 result = simplejson.loads(result)
93 return result
94 except:
95 return []
96
97 # Get a session id
98 def _getSessionID(self):
99 params = {}
100 result = self._callRemote('startSession', params)
101 if 'result' in result:
102 self._lastSessionTime = time.time()
103 return result['result']['sessionID']
104 else:
105 return ''
106
107 def _getSavedSession(self):
108 path = os.path.join(self.cacheDir, 'session.dmp')
109 try:
110 f = open(path, 'rb')
111 session = pickle.load(f)
112 self._sessionID = session['sessionID']
113 self._lastSessionTime = session['lastSessionTime']
114 self._userID = session['userID']
115 self._ip = session['ip']
116 self._country = session['country']
117 f.close()
118 except:
119 self._sessionID = ''
120 self._lastSessionTime = 0
121 self._userID = 0
122 self._ip = '0.0.0.0'
123 self._country = ''
124 pass
125
126 def _setSavedSession(self):
127 try:
128 # Create the directory if it doesn't exist.
129 if not os.path.exists(self.cacheDir):
130 os.makedirs(self.cacheDir)
131 path = os.path.join(self.cacheDir, 'session.dmp')
132 f = open(path, 'wb')
133 session = { 'sessionID' : self._sessionID, 'lastSessionTime' : self._lastSessionTime, 'userID': self._userID, 'ip' : self._ip, 'country' : self._country }
134 pickle.dump(session, f, protocol=pickle.HIGHEST_PROTOCOL)
135 f.close()
136 except:
137 print "An error occurred during save session"
138 pass
139
140 def _setParams(self, params):
141 try:
142 # Create the directory if it doesn't exist.
143 if not os.path.exists(self.cacheDir):
144 os.makedirs(self.cacheDir)
145 path = os.path.join(self.cacheDir, 'params.dmp')
146 f = open(path, 'wb')
147 pickle.dump(params, f, protocol=pickle.HIGHEST_PROTOCOL)
148 f.close()
149 except:
150 print "An error occurred during save params"
151 pass
152
153 # Get IP
154 def _getIP(self):
155 try:
156 myip = urllib2.urlopen('http://whatismyip.org').read()
157 if re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", myip):
158 print "IP is " + myip
159 return myip
160 except:
161 return '0.0.0.0'
162
163 # Get country
164 def _getCountry(self):
165 params = { 'ip' : self._ip }
166 response = self._callRemote("getCountry", params)
167 return response['result']
168
169 # Get userid from name
170 def _getUserIDFromUsername(self, username):
171 result = self._callRemote('getUserIDFromUsername', {'username' : username})
172 if 'result' in result and result['result']['UserID'] > 0:
173 return result['result']['UserID']
174 else:
175 return 0
176
177 # Authenticates the user for current API session
178 def _authenticate(self, login, password):
179 md5pwd = md5.new(password).hexdigest()
180 params = {'login': login, 'password': md5pwd}
181
182 result = self._callRemote('authenticate', params)
183 try:
184 uid = result['result']['UserID']
185 except:
186 uid = 0
187 if (uid > 0):
188 return uid
189 else:
190 return 0
191
192 # Check the service
193 def pingService(self,):
194 result = self._callRemote('pingService', {});
195 if 'result' in result and result['result'] != '':
196 return True
197 else:
198 return False
199
200 # Login
201 def login(self, username, password):
202 if self._userID <= 0:
203 # Check cache
204 self._getSavedSession()
205 if self._userID <= 0:
206 self._userID = self._authenticate(username, password)
207 if self._userID > 0:
208 self._setSavedSession()
209 return self._userID
210
211 # Logs the user out
212 def logout(self):
213 result = self._callRemote('logout', {'sessionID' : self._sessionID})
214 if 'result' in result and result['result']['success'] == True:
215 self._userID = 0
216 self._setSavedSession()
217 return True
218 return False
219
220 # Gets a stream key and host to get song content
221 def getSubscriberStreamKey(self, songID):
222 params = { "songID": songID, "country": self._country }
223 response = self._callRemote("getSubscriberStreamKey", params)
224 try:
225 self._lastStreamKey = response["result"]["StreamKey"]
226 self._lastStreamServerID = response["result"]["StreamServerID"]
227 res = response["result"]
228 return res
229 except:
230 return False
231
232 # Search for albums
233 def getArtistSearchResults(self, query, limit=ARTIST_LIMIT):
234 result = self._callRemote('getArtistSearchResults', {'query' : query,'limit' : limit})
235 if 'result' in result:
236 return self._parseArtists(result)
237 else:
238 return []
239
240 # Search for albums
241 def getAlbumSearchResults(self, query, limit=ALBUM_LIMIT):
242 result = self._callRemote('getAlbumSearchResults', {'query' : query,'limit' : limit})
243 if 'result' in result:
244 return self._parseAlbums(result)
245 else:
246 return []
247
248 # Search for songs
249 def getSongSearchResults(self, query, limit=SONG_LIMIT):
250 result = self._callRemote('getSongSearchResults', {'query' : query, 'country' : self._country, 'limit' : limit})
251 if 'result' in result:
252 return self._parseSongs(result)
253 else:
254 return []
255
256 # Get artists albums
257 def getArtistAlbums(self, artistID, limit=ALBUM_LIMIT):
258 result = self._callRemote('getArtistAlbums', {'artistID' : artistID})
259 if 'result' in result:
260 return self._parseAlbums(result, limit)
261 else:
262 return []
263
264 # Get album songs
265 def getAlbumSongs(self, albumID, limit=SONG_LIMIT):
266 result = self._callRemote('getAlbumSongs', {'albumID' : albumID, 'limit' : limit})
267 if 'result' in result:
268 return self._parseSongs(result)
269 else:
270 return []
271
272 # Get artist's popular songs
273 def getArtistPopularSongs(self, artistID, limit = SONG_LIMIT):
274 result = self._callRemote('getArtistPopularSongs', {'artistID' : artistID})
275 if 'result' in result:
276 return self._parseSongs(result, limit)
277 else:
278 return []
279
280 # Gets the popular songs
281 def getPopularSongsToday(self, limit=SONG_LIMIT):
282 result = self._callRemote('getPopularSongsToday', {'limit' : limit})
283 if 'result' in result:
284 # Note limit is broken in the Grooveshark getPopularSongsToday method
285 return self._parseSongs(result, limit)
286 else:
287 return []
288
289 # Gets the favorite songs of the logged-in user
290 def getUserFavoriteSongs(self):
291 if (self._userID == 0):
292 return [];
293 result = self._callRemote('getUserFavoriteSongs', {})
294 if 'result' in result:
295 return self._parseSongs(result)
296 else:
297 return []
298
299 # Get song info
300 def getSongsInfo(self, songIDs):
301 result = self._callRemote('getSongsInfo', {'songIDs' : songIDs})
302 if 'result' in result and 'SongID' in result['result']:
303 info = result['result']
304 if 'CoverArtFilename' in info and info['CoverArtFilename'] != None:
305 info['CoverArtFilename'] = THUMB_URL+info['CoverArtFilename'].encode('ascii', 'ignore')
306 else:
307 info['CoverArtFilename'] = 'None'
308 return info
309 else:
310 return 'None'
311
312 # Add song to user favorites
313 def addUserFavoriteSong(self, songID):
314 if (self._userID == 0):
315 return False;
316 result = self._callRemote('addUserFavoriteSong', {'songID' : songID})
317 return result['result']['success']
318
319 # Remove songs from user favorites
320 def removeUserFavoriteSongs(self, songIDs):
321 if (self._userID == 0):
322 return False;
323 result = self._callRemote('removeUserFavoriteSongs', {'songIDs' : songIDs})
324 return result['result']['success']
325
326 # Gets the playlists of the logged-in user
327 def getUserPlaylists(self):
328 if (self._userID == 0):
329 return [];
330 result = self._callRemote('getUserPlaylists', {})
331 if 'result' in result:
332 return self._parsePlaylists(result)
333 else:
334 return []
335
336 # Gets the playlists of the logged-in user
337 def getUserPlaylistsByUsername(self, username):
338 userID = self._getUserIDFromUsername(username)
339 if (userID > 0):
340 result = self._callRemote('getUserPlaylistsByUserID', {'userID' : userID})
341 if 'result' in result and result['result']['playlists'] != None:
342 playlists = result['result']['playlists']
343 return self._parsePlaylists(playlists)
344 else:
345 return []
346
347 # Creates a playlist with songs
348 def createPlaylist(self, name, songIDs):
349 result = self._callRemote('createPlaylist', {'name' : name, 'songIDs' : songIDs})
350 if 'result' in result and result['result']['success'] == True:
351 return result['result']['playlistID']
352 elif 'errors' in result:
353 return 0
354
355 # Sets the songs for a playlist
356 def setPlaylistSongs(self, playlistID, songIDs):
357 result = self._callRemote('setPlaylistSongs', {'playlistID' : playlistID, 'songIDs' : songIDs})
358 if 'result' in result and result['result']['success'] == True:
359 return True
360 else:
361 return False
362
363 # Gets the songs of a playlist
364 def getPlaylistSongs(self, playlistID):
365 result = self._callRemote('getPlaylistSongs', {'playlistID' : playlistID});
366 if 'result' in result:
367 return self._parseSongs(result)
368 else:
369 return []
370
371
372 def playlistDelete(self, playlistId):
373 result = self._callRemote("deletePlaylist", {"playlistID": playlistId})
374 if 'fault' in result:
375 return 0
376 else:
377 return 1
378
379 def playlistRename(self, playlistId, name):
380 result = self._callRemote("renamePlaylist", {"playlistID": playlistId, "name": name})
381 if 'fault' in result:
382 return 0
383 else:
384 return 1
385
386 def getSimilarArtists(self, artistId, limit):
387 items = self._callRemote("getSimilarArtists", {"artistID": artistId, "limit": limit})
388 if 'result' in items:
389 i = 0
390 list = []
391 artists = items['result']['artists']
392 while(i < len(artists)):
393 s = artists[i]
394 list.append([s['artistName'].encode('ascii', 'ignore'),\
395 s['artistID']])
396 i = i + 1
397 return list
398 else:
399 return []
400
401 # After 30s play time
402 def markStreamKeyOver30Secs(self):
403 params = { "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID }
404 self._callRemote("markStreamKeyOver30Secs", params)
405
406 # Song complete
407 def markSongComplete(self, songid):
408 params = { "songID" : songid, "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID }
409 self._callRemote("markSongComplete", params)
410
411 # Extract song data
412 def _parseSongs(self, items, limit=0):
413 if 'result' in items:
414 i = 0
415 list = []
416 index = ''
417 l = -1
418 try:
419 if 'songs' in items['result'][0]:
420 l = len(items['result'][0]['songs'])
421 index = 'songs[]'
422 except: pass
423 try:
424 if l < 0 and 'songs' in items['result']:
425 l = len(items['result']['songs'])
426 index = 'songs'
427 except: pass
428 try:
429 if l < 0 and 'song' in items['result']:
430 l = 1
431 index = 'song'
432 except: pass
433 try:
434 if l < 0:
435 l = len(items['result'])
436 except: pass
437
438 if limit > 0 and l > limit:
439 l = limit
440 while(i < l):
441 if index == 'songs[]':
442 s = items['result'][0]['songs'][i]
443 elif index == 'songs':
444 s = items['result'][index][i]
445 elif index == 'song':
446 s = items['result'][index]
447 else:
448 s = items['result'][i]
449 if 'CoverArtFilename' not in s:
450 info = self.getSongsInfo(s['SongID'])
451 coverart = info['CoverArtFilename']
452 elif s['CoverArtFilename'] != None:
453 coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
454 else:
455 coverart = 'None'
456 list.append([s['SongName'].encode('ascii', 'ignore'),\
457 s['SongID'],\
458 s['AlbumName'].encode('ascii', 'ignore'),\
459 s['AlbumID'],\
460 s['ArtistName'].encode('ascii', 'ignore'),\
461 s['ArtistID'],\
462 coverart])
463 i = i + 1
464 return list
465 else:
466 return []
467
468 # Extract artist data
469 def _parseArtists(self, items):
470 if 'result' in items:
471 i = 0
472 list = []
473 artists = items['result']['artists']
474 while(i < len(artists)):
475 s = artists[i]
476 list.append([s['ArtistName'].encode('ascii', 'ignore'),\
477 s['ArtistID']])
478 i = i + 1
479 return list
480 else:
481 return []
482
483 # Extract album data
484 def _parseAlbums(self, items, limit=0):
485 if 'result' in items:
486 i = 0
487 list = []
488 try:
489 albums = items['result']['albums']
490 except:
491 res = items['result'][0]
492 albums = res['albums']
493 l = len(albums)
494 if limit > 0 and l > limit:
495 l = limit
496 while(i < l):
497 s = albums[i]
498 if 'CoverArtFilename' in s and s['CoverArtFilename'] != None:
499 coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
500 else:
501 coverart = 'None'
502 list.append([s['ArtistName'].encode('ascii', 'ignore'),\
503 s['ArtistID'],\
504 s['AlbumName'].encode('ascii', 'ignore'),\
505 s['AlbumID'],\
506 coverart])
507 i = i + 1
508 return list
509 else:
510 return []
511
512 def _parsePlaylists(self, items):
513 i = 0
514 list = []
515 if 'result' in items:
516 playlists = items['result']['playlists']
517 elif len(items) > 0:
518 playlists = items
519 else:
520 return []
521
522 while (i < len(playlists)):
523 s = playlists[i]
524 list.append([str(s['PlaylistName']).encode('ascii', 'ignore'), s['PlaylistID']])
525 i = i + 1
526 return list