-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
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 playlistGetSongs(self, playlistId):
+ items = self.callRemote("playlist.getSongs", {"playlistID": playlistId})
+ print items
+ if 'result' in items:
+ i = 0
+ list = []
+ 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
+
+ while(i < l):
+ if index == 'songs[]':
+ s = items['result'][0]['songs'][i]
+ elif index == 'songs':
+ s = items['result'][index][i]
+ elif index == 'song':
+ s = items['result'][index]
+ else:
+ s = items['result'][i]
+ if 'CoverArtFilename' not in s:
+ info = self.getSongInfo(s['SongID'])
+ coverart = info['CoverArtFilename']
+ elif s['CoverArtFilename'] != None:
+ coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
+ else:
+ coverart = 'None'
+ list.append([s['SongName'].encode('ascii', 'ignore'),\
+ s['SongID'],\
+ s['AlbumName'].encode('ascii', 'ignore'),\
+ s['AlbumID'],\
+ s['ArtistName'].encode('ascii', 'ignore'),\
+ s['ArtistID'],\
+ coverart])
+ i = i + 1
+ return list
+ else:
+ return []
+
+
+ return list
+
+ 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: