sessionID = ''
userID = 0
host = 'api.grooveshark.com'
+ lastSessionTime = 0
# Constructor
def __init__(self, cacheDir):
self.simplejson = simplejson
socket.setdefaulttimeout(40)
self.cacheDir = cacheDir
- self.sessionID = self._getSavedSessionID()
- if self.sessionID == '':
+ self._getSavedSessionID()
+ # session ids last 1 week
+ if self.sessionID == '' or time.time()- self.lastSessionTime >= 6*86400:
self.sessionID = self._getSessionID()
if self.sessionID == '':
raise StandardError('Failed to get session id')
else:
- self._setSavedSessionID()
+ print "New session id: " + self.sessionID
+ self._setSavedSessionID()
+
# Sort keys
def _keySort(self, d):
return [(k,d[k]) for k in sorted(d.keys())]
def _getSessionID(self):
params = {}
result = self._callRemote('startSession', params)
+ self.lastSessionTime = time.time()
return result['result']['sessionID']
def _getSavedSessionID(self):
- sessionID = ''
- path = os.path.join(self.cacheDir, 'session', 'session.txt')
+ path = os.path.join(self.cacheDir, 'session', 'session.dmp')
try:
f = open(path, 'rb')
- sessionID = pickle.load(f)
+ session = pickle.load(f)
+ self.sessionID = session['sessionID']
+ self.lastSessionTime = session['lastSessionTime']
f.close()
except:
- sessionID = ''
+ self.sessionID = ''
+ self.lastSessionTime = 0
pass
-
- return sessionID
def _setSavedSessionID(self):
try:
# Create the 'data' directory if it doesn't exist.
if not os.path.exists(dir):
os.makedirs(dir)
- path = os.path.join(dir, 'session.txt')
+ path = os.path.join(dir, 'session.dmp')
f = open(path, 'wb')
- pickle.dump(self.sessionID, f, protocol=pickle.HIGHEST_PROTOCOL)
+ session = { 'sessionID' : self.sessionID, 'lastSessionTime' : self.lastSessionTime}
+ pickle.dump(session, f, protocol=pickle.HIGHEST_PROTOCOL)
f.close()
except:
print "An error occured during save session"
else:
return []
+ # Add song to user favorites
+ def addUserFavoriteSong(self, songID):
+ if (self.userID == 0):
+ return False;
+ result = self._callRemote('addUserFavoriteSong', {'sessionID' : self.sessionID, 'songID' : songID})
+ return result['result']['success']
+
# Get the url to link to a song on Grooveshark
def getSongURLFromSongID(self, songID):
song = GrooveSong(self.cacheDir)
# Creates a playlist with songs
def createPlaylist(self, name, songIDs):
result = self._callRemote('createPlaylist', {'name' : name, 'songIDs' : songIDs, 'sessionID' : self.sessionID})
- if 'result' in result and result['result']['success'] == 'true':
+ if 'result' in result and result['result']['success'] == True:
return result['result']['PlaylistID']
elif 'errors' in result:
return 0
# Sets the songs for a playlist
def setPlaylistSongs(self, playlistID, songIDs):
result = self._callRemote('setPlaylistSongs', {'playlistID' : playlistID, 'songIDs' : songIDs, 'sessionID' : self.sessionID})
- if 'result' in result and result['result']['success'] == 'true':
+ if 'result' in result and result['result']['success'] == True:
return True;
else:
return False;
# Test
-
-#res = []
-#groovesharkApi = GrooveAPI('/tmp')
-#res = groovesharkApi.login('stephendenham', '*******')
+import sys
+res = []
+groovesharkApi = GrooveAPI('/tmp')
+res = groovesharkApi.login(sys.argv[1], sys.argv[2])
#res = groovesharkApi.getSongSearchResults('jimmy jazz', 3)
#res = groovesharkApi.getPopularSongsToday()
#res = groovesharkApi.getSongURLFromSongID('27425375')
#res = groovesharkApi.getAlbumSearchResults('london calling', 3)
#res = groovesharkApi.getArtistSearchResults('the clash', 3)
#res = groovesharkApi.getUserFavoriteSongs()
-#res = groovesharkApi.getUserPlaylists()
+res = groovesharkApi.getUserPlaylists()
#res = groovesharkApi.getSongInfo('27425375')
#res = groovesharkApi.getPlaylistSongs(40902662)
+#res = groovesharkApi.addUserFavoriteSong('27425375')
-#pprint.pprint(res)
+pprint.pprint(res)