First attempt at new API.
authorstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Wed, 12 Jan 2011 20:03:16 +0000 (20:03 +0000)
committerstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Wed, 12 Jan 2011 20:03:16 +0000 (20:03 +0000)
git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@26 2dec19e3-eb1d-4749-8193-008c8bba0994

resources/lib/GrooveAPIWP.py

index 739416d..95b85b9 100644 (file)
@@ -91,7 +91,7 @@ class GrooveAPI:
                if 'result' in result:
                        return self._parseArtists(result)
                else:
-                       return ''
+                       return []
 
        # Search for albums
        def getAlbumSearchResults(self, query, limit=ALBUM_LIMIT):
@@ -99,7 +99,7 @@ class GrooveAPI:
                if 'result' in result:
                        return self._parseAlbums(result)
                else:
-                       return ''
+                       return []
                
        # Search for songs
        def getSongSearchResults(self, query, limit=SONG_LIMIT):
@@ -107,7 +107,7 @@ class GrooveAPI:
                if 'result' in result:
                        return self._parseSongs(result)
                else:
-                       return ''
+                       return []
        
        # Gets the popular songs
        def getPopularSongsToday(self, limit=SONG_LIMIT):
@@ -115,7 +115,7 @@ class GrooveAPI:
                if 'result' in result:
                        return self._parseSongs(result)
                else:
-                       return ''
+                       return []
 
        # Gets the favorite songs of the logged-in user
        def getUserFavoriteSongs(self):
@@ -125,7 +125,7 @@ class GrooveAPI:
                if 'result' in result:
                        return self._parseSongs(result)
                else:
-                       return ''
+                       return []
                
        # Get the url to link to a song on Grooveshark
        def getSongURLFromSongID(self, songID):
@@ -136,43 +136,38 @@ class GrooveAPI:
                else:
                        return ''
 
+       # Get the url to link to a song on Grooveshark
+       def getSongInfo(self, songID):
+               result = self._callRemote('getSongInfoEx', {'songID' : songID})
+               if 'result' in result and 'SongID' in result['result']:
+                       info = result['result']
+                       if 'CoverArtFilename' in info and info['CoverArtFilename'] != None:
+                               info['CoverArtFilename'] = THUMB_URL+info['CoverArtFilename'].encode('ascii', 'ignore')
+                       else:
+                               info['CoverArtFilename'] = THUMB_URL_DEFAULT
+                       return info
+               else:
+                       return ''
+
        # Gets the playlists of the logged-in user
        def getUserPlaylists(self):
                if (self.userID == 0):
                        return [];
                result = self._callRemote('getUserPlaylists', {'sessionID' : self.sessionID})
                if 'result' in result:
-                       playlists = result['result']['playlists']
-               else:
-                       return []
-               i = 0
-               list = []
-               while(i < len(playlists)):
-                       p = playlists[i]
-                       list.append([p['playlistName'].encode('ascii', 'ignore'), p['playlistID']])
-                       i = i + 1       
-               return sorted(list, key=itemgetter(0))
-       
-       # Gets playlist information
-       def getPlaylistInfo(self, playlistID):
-               result = self._callRemote('getPlaylistInfo', {'playlistID' : playlistID})
-               if 'result' in result:
-                       return result['result']
+                       return self._parsePlaylists(result)
                else:
                        return []
-               
+
        # 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'] == 1
-                       return result['result']['playlistID']
+               if 'result' in result and result['result']['success'] == 'true'
+                       return result['result']['PlaylistID']
                elif 'errors' in result:
-                       duplicate = False;
                        for error in result['errors']:
                                if (error['code'] == 800):
-                                       duplicate = True;
-                       if (duplicate):
-                               return -1
+                                       return -1
                        else:
                                return 0
                else:
@@ -181,7 +176,7 @@ class GrooveAPI:
        # 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'] == 1
+               if 'result' in result and result['result']['success'] == 'true'
                        return True;
                else:
                        return False;
@@ -190,11 +185,9 @@ class GrooveAPI:
        def getPlaylistSongs(self, playlistID):
                result = self._callRemote('getPlaylistSongs', {'playlistID' : playlistID});
                if 'result' in result:
-                       return result['result'];
-               elif result['errors'] in result :
-                       return result['errors'][0]['code']
+                       return self._parseSongs(result)
                else:
-                       return {'error':-512};
+                       return []
        
        # Extract song data     
        def _parseSongs(self, items):
@@ -208,14 +201,19 @@ class GrooveAPI:
                                l = 1
                                index = 'song'
                        else:
-                               l = 0
+                               l = len(items['result'])
                                index = ''
                        while(i < l):
                                if index == 'songs':
                                        s = items['result'][index][i]
-                               else:
+                               elif index == 'song':
                                        s = items['result'][index]
-                               if s['CoverArtFilename'] != None:
+                               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 = THUMB_URL_DEFAULT
@@ -254,7 +252,7 @@ class GrooveAPI:
                        albums = items['result']['albums']
                        while(i < len(albums)):
                                s = albums[i]
-                               if s['CoverArtFilename'] != None:
+                               if 'CoverArtFilename' in s and s['CoverArtFilename'] != None:
                                        coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
                                else:
                                        coverart = THUMB_URL_DEFAULT
@@ -272,12 +270,11 @@ class GrooveAPI:
                if 'result' in items:
                        i = 0
                        list = []
-                       playlists = items['result']['playlists']
+                       playlists = items['result']
                        while(i < len(playlists)):
                                s = playlists[i]
                                list.append([s['PlaylistID'],\
-                               s['PlaylistName'].encode('ascii', 'ignore'),\
-                               s['Username'].encode('ascii', 'ignore')])
+                               s['Name'].encode('ascii', 'ignore')])
                                i = i + 1
                        return list
                else:
@@ -293,8 +290,11 @@ print "UserID: " + str(groovesharkApi.login('stephendenham', 'lond0n'))
 #res = groovesharkApi.getSongURLFromSongID('27425375')
 #res = groovesharkApi.getAlbumSearchResults('london calling', 3)
 #res = groovesharkApi.getArtistSearchResults('the clash', 3)
-res = groovesharkApi.getUserFavoriteSongs()
+#res = groovesharkApi.getUserFavoriteSongs()
+#res = groovesharkApi.getUserPlaylists()
+#res = groovesharkApi.getSongInfo('27425375')
 
+res = groovesharkApi.getPlaylistSongs(40902662)
 
 pprint.pprint(res)