Add search of artist's albums.
[clinton/xbmc-groove.git] / resources / lib / GroovesharkAPI.py
index e36ffc2..d571670 100644 (file)
@@ -200,19 +200,14 @@ class GrooveSong:
                        "songID": songID}
                self._callRemote("markSongDownloaded", params)
 
-       # Download a song to a temporary file
+       # Get the song URL
        def getSongURL(self, songID):
-               filename = os.path.join(self.cacheDir, songID + SONG_SUFFIX)
-               print "Caching song " + songID + " to " + filename
-               if os.path.isfile(filename) == False:
-                       if self._getStreamDetails(songID) == True:
-                               postData = {"streamKey": self._lastStreamKey}
-                               postData = urllib.urlencode(postData)
-                               urllib.FancyURLopener().retrieve( "http://" + self._lastStreamServer + "/stream.php", filename, data=postData)
-                               self._markSongDownloaded(songID)
-                       else:
-                               return ''
-               return filename
+               if self._getStreamDetails(songID) == True:
+                       postData = {"streamKey": self._lastStreamKey}
+                       postData = urllib.urlencode(postData)
+                       return "http://" + self._lastStreamServer + "/stream.php?" + str(postData)
+               else:
+                       return ''
 
 # Main API
 class GrooveAPI:
@@ -412,7 +407,9 @@ class GrooveAPI:
        # Get the url to link to a song on Grooveshark
        def getSongURLFromSongID(self, songID):
                song = GrooveSong()
-               return song.getSongURL(songID)
+               url = song.getSongURL(songID)
+               print "Got song URL " + url
+               return url
 
        # Get the url to link to a song on Grooveshark
        def getSongInfo(self, songID):
@@ -436,6 +433,25 @@ class GrooveAPI:
                        return self._parsePlaylists(result)
                else:
                        return []
+               
+       # Get userid from name
+       def _getUserIDFromUsername(self, username):
+               result = self._callRemote('getUserIDFromUsername', {'username' : username})
+               if 'result' in result and result['result']['UserID'] > 0:
+                       return result['result']['UserID']
+               else:
+                       return 0
+
+       # Gets the playlists of the logged-in user
+       def getUserPlaylistsEx(self, username):
+               userID = self._getUserIDFromUsername(username)
+               if (userID > 0):
+                       result = self._callRemote('getUserPlaylistsEx', {'userID' : userID})
+                       if 'result' in result and result['result']['playlists'] != None:
+                               playlists = result['result']['playlists']
+                               return self._parsePlaylists(playlists)
+               else:
+                       return []
 
        # Creates a playlist with songs
        def createPlaylist(self, name, songIDs):
@@ -556,17 +572,20 @@ class GrooveAPI:
                        return []
 
        def _parsePlaylists(self, items):
+               i = 0
+               list = []
                if 'result' in items:
-                       i = 0
-                       list = []
                        playlists = items['result']
-                       while(i < len(playlists)):
-                               s = playlists[i]
-                               list.append([s['Name'].encode('ascii', 'ignore'), s['PlaylistID']])
-                               i = i + 1
-                       return list
+               elif len(items) > 0:
+                       playlists = items
                else:
                        return []
+               
+               while (i < len(playlists)):
+                       s = playlists[i]
+                       list.append([s['Name'].encode('ascii', 'ignore'), s['PlaylistID']])
+                       i = i + 1
+               return list
 
 # Test
 #import sys
@@ -590,5 +609,6 @@ class GrooveAPI:
 #res = groovesharkApi.getPlaylistSongs(40902662)
 #res = groovesharkApi.addUserFavoriteSong('27425375')
 #res = groovesharkApi.logout()
+#res = groovesharkApi.getUserPlaylistsEx('stephendenham')
 #
 #pprint.pprint(res)