X-Git-Url: https://git.hcoop.net/clinton/xbmc-groove.git/blobdiff_plain/86f629eafbd6d6f83b3a1642dc3d2a2b6ff7a861..e6f8730bfc96ae4055c0e2c8f49ff61bf8f8981e:/resources/lib/GroovesharkAPI.py diff --git a/resources/lib/GroovesharkAPI.py b/resources/lib/GroovesharkAPI.py index d571670..a205924 100644 --- a/resources/lib/GroovesharkAPI.py +++ b/resources/lib/GroovesharkAPI.py @@ -1,9 +1,9 @@ -import socket, hmac, urllib, urllib2, pprint, md5, re, sha, time, random, os, pickle, uuid, tempfile +import socket, hmac, urllib, urllib2, pprint, md5, re, sha, time, random, os, pickle, uuid, tempfile, pprint SESSION_EXPIRY = 518400 # 6 days in seconds # GrooveAPI constants -THUMB_URL = 'http://beta.grooveshark.com/static/amazonart/' +THUMB_URL = 'http://beta.grooveshark.com/static/amazonart/m' SONG_LIMIT = 25 ALBUM_LIMIT = 15 ARTIST_LIMIT = 15 @@ -209,6 +209,143 @@ class GrooveSong: else: return '' +class GrooveAPIv1: + + def __init__(self, sessionID = 0): + import simplejson + self.simplejson = simplejson + timeout = 40 + socket.setdefaulttimeout(timeout) + self.loggedIn = 0 + self.userId = 0 + self.sessionID = sessionID + + def callRemote(self, method, params={}): + data = {'header': {'sessionID': self.sessionID}, 'method': method, 'parameters': params} + data = self.simplejson.dumps(data) + req = urllib2.Request("http://api.grooveshark.com/ws/1.0/?json") + req.add_header('Host', 'api.grooveshark.com') + req.add_header('Content-type', 'text/json') + req.add_header('Content-length', str(len(data))) + req.add_data(data) + response = urllib2.urlopen(req) + result = response.read() + response.close() + try: + result = self.simplejson.loads(result) + if 'fault' in result: + if result['fault']['code'] == 8: #Session ID has expired. Get a new and try again if possible. + print 'GrooveShark: SessionID expired' + return [] + return result + except: + return [] + + def login(self, username, password): + if self.loggedIn == 1: + return self.userId + token = md5.new(username.lower() + md5.new(password).hexdigest()).hexdigest() + result = self.callRemote("session.loginExt", {"username": username, "token": token}) + if 'result' in result: + if 'userID' in result['result']: + self.loggedIn = 1 + self.userId = result['result']['userID'] + return result['result']['userID'] + else: + return 0 + + def unfavorite(self, songID): + return self.callRemote("song.unfavorite", {"songID": songID}) + + def playlistCreate(self, name, about): + if self.loggedIn == 1: + result = self.callRemote("playlist.create", {"name": name, "about": about}) + if 'result' in result: + return result['result']['playlistID'] + else: + return 0 + else: + return 0 + + def playlistCreateUnique(self, name, songIds): + if self.loggedIn == 1: + result = self.callRemote("playlist.createunique", {"name": name, "songIDs": songIds}) + if 'result' in result: + return result['result']['playlistID'] + else: + return 0 + else: + return 0 + + def playlistDelete(self, playlistId): + if self.loggedIn == 1: + result = self.callRemote("playlist.delete", {"playlistID": playlistId}) + if 'fault' in result: + return 0 + else: + return 1 + else: + return 0 + + def playlistRename(self, playlistId, name): + if self.loggedIn == 1: + result = self.callRemote("playlist.rename", {"playlistID": playlistId, "name": name}) + if 'fault' in result: + return 0 + else: + return 1 + else: + return 0 + + def playlistClearSongs(self, playlistId): + if self.loggedIn == 1: + return self.callRemote("playlist.clearSongs", {"playlistID": playlistId}) + + def playlistAddSong(self, playlistId, songId, position): + if self.loggedIn == 1: + result = self.callRemote("playlist.addSong", {"playlistID": playlistId, "songID": songId, "position": position}) + if 'fault' in result: + return 0 + else: + return 1 + else: + return 0 + + def playlistDeleteSong(self, playlistId, position): + if self.loggedIn == 1: + result = self.callRemote("playlist.removeSong", {"playlistID": playlistId, "position": position}) + if 'fault' in result: + return 0 + else: + return 1 + else: + return 0 + + def playlistReplace(self, playlistId, songIds): + if self.loggedIn == 1: + result = self.callRemote("playlist.replace", {"playlistID": playlistId, "songIDs": songIds}) + if 'fault' in result: + return 0 + else: + return 1 + else: + return 0 + + def artistGetSimilar(self, artistId, limit): + items = self.callRemote("artist.getSimilar", {"artistID": artistId, "limit": limit}) + if 'result' in items: + i = 0 + list = [] + artists = items['result']['artists'] + while(i < len(artists)): + s = artists[i] + list.append([s['artistName'].encode('ascii', 'ignore'),\ + s['artistID']]) + i = i + 1 + return list + else: + return [] + # Main API class GrooveAPI: @@ -377,7 +514,15 @@ class GrooveAPI: return self._parseSongs(result) else: return [] - + + # Get artist's popular songs + def getArtistPopularSongs(self, artistID, limit = SONG_LIMIT): + result = self._callRemote('getArtistPopularSongs', {'artistID' : artistID}) + if 'result' in result: + return self._parseSongs(result, limit) + else: + return [] + # Gets the popular songs def getPopularSongsToday(self, limit=SONG_LIMIT): result = self._callRemote('getPopularSongsToday', {'limit' : limit}) @@ -490,19 +635,34 @@ class GrooveAPI: if 'result' in items: i = 0 list = [] - if 'songs' in items['result']: - l = len(items['result']['songs']) - index = 'songs' - elif 'song' in items['result']: - l = 1 - index = 'song' - else: - l = len(items['result']) - index = '' + index = '' + l = -1 + try: + if 'songs' in items['result'][0]: + l = len(items['result'][0]['songs']) + index = 'songs[]' + except: pass + try: + if l < 0 and 'songs' in items['result']: + l = len(items['result']['songs']) + index = 'songs' + except: pass + try: + if l < 0 and 'song' in items['result']: + l = 1 + index = 'song' + except: pass + try: + if l < 0: + l = len(items['result']) + except: pass + if limit > 0 and l > limit: l = limit while(i < l): - if index == 'songs': + if index == 'songs[]': + s = items['result'][0]['songs'][i] + elif index == 'songs': s = items['result'][index][i] elif index == 'song': s = items['result'][index] @@ -593,10 +753,11 @@ class GrooveAPI: #groovesharkApi = GrooveAPI() #res = groovesharkApi.pingService() #res = groovesharkApi.login(sys.argv[1], sys.argv[2]) -#songIDs = ['23404546','23401810','23401157'] -#res = groovesharkApi.createPlaylist("Test") -#res = groovesharkApi.setPlaylistSongs(res, songIDs) -#res = groovesharkApi.getPlaylistSongs(42251632) +#songIDs = "[23404546,23401810,23401157]" +#res = groovesharkApi.createPlaylist("Test", songIDs) +#res = groovesharkApi.setPlaylistSongs('42873478', songIDs) +#pprint.pprint(res) +#res = groovesharkApi.getPlaylistSongs('42873478') #res = groovesharkApi.getSongSearchResults('jimmy jazz', 3) #res = groovesharkApi.getPopularSongsToday(3) #res = groovesharkApi.getSongURLFromSongID('26579347') @@ -610,5 +771,6 @@ class GrooveAPI: #res = groovesharkApi.addUserFavoriteSong('27425375') #res = groovesharkApi.logout() #res = groovesharkApi.getUserPlaylistsEx('stephendenham') +#res = groovesharkApi.getArtistPopularSongs('3707') # #pprint.pprint(res)