-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
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:
#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')