Add similar artists function.
[clinton/xbmc-groove.git] / default.py
index 30460dd..0e0261e 100644 (file)
@@ -9,12 +9,10 @@ MODE_PLAYLISTS = 6
 MODE_ALBUM = 7
 MODE_ARTIST = 8
 MODE_PLAYLIST = 9
-MODE_SONG = 10
-MODE_FAVORITE = 11
-MODE_UNFAVORITE = 12
-MODE_SIMILAR_SONG = 13
-MODE_SIMILAR_ARTIST = 14
-MODE_FROWN = 15
+MODE_SIMILAR_ARTISTS = 10
+MODE_SONG = 11
+MODE_FAVORITE = 12
+MODE_UNFAVORITE = 13
 
 lastID = 0
 
@@ -139,7 +137,7 @@ class Groveshark:
     def favorite(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Favorite playSong: " + str(songid))
+            xbmc.log("Favorite song: " + str(songid))
             groovesharkApi.favoriteSong(songID = songid)
             os.remove(favoritesCache)
         else:
@@ -149,7 +147,7 @@ class Groveshark:
     def unfavorite(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Unfavorite playSong: " + str(songid))
+            xbmc.log("Unfavorite song: " + str(songid))
             groovesharkApi.unfavoriteSong(songID = songid)
             os.remove(favoritesCache)
         else:
@@ -159,38 +157,21 @@ class Groveshark:
     def frown(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Frown playSong: " + str(songid))
+            xbmc.log("Frown song: " + str(songid))
             if groovesharkApi.radioFrown(songId = songid) != True:
                 xbmc.log("Unable to frown song " + str(songid))
         else:
             dialog = xbmcgui.Dialog()
             dialog.ok('Grooveshark', 'You must be logged in', 'to frown a song.')
 
-    def similarSong(self, songid):
-        userid = self._get_login()
-        if (userid != 0):
-            xbmc.log("Add song: " + str(songid))
-            if groovesharkApi.radioSong(songId = songid) != False and groovesharkApi.radioStartSongs() == True:
-                self.playNext()
-            else:
-                dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark', 'Cannot start radio')
+    def similarArtists(self, artistId):
+        similar = groovesharkApi.artistGetSimilar(artistId, limit = self.artistsearchlimit)
+        if (len(similar) > 0):
+            self._add_artists_directory(similar)
         else:
             dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark', 'You must be logged in', 'to update radio song.')
-
-    def similarArtist(self, artistId):
-        userid = self._get_login()
-        if (userid != 0):
-            xbmc.log("Add radio artist: " + str(artistId))
-            if groovesharkApi.radioArtist(artistId = artistId) != False and groovesharkApi.radioStartArtists() == True:
-                self.playNext()
-            else:
-                dialog = xbmcgui.Dialog()
-                dialog.ok('Grooveshark', 'Cannot start radio')
-        else:
-            dialog = xbmcgui.Dialog()
-            dialog.ok('Grooveshark', 'You must be logged in', 'to update radio artists.')
+            dialog.ok('Grooveshark', 'No similar artists.')
+            self.categories()
     
     def album(self,albumid):
         album = groovesharkApi.albumGetSongs(albumId = albumid, limit = self.songsearchlimit)
@@ -198,7 +179,7 @@ class Groveshark:
     
     def artist(self, artistid):
         albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = self.albumsearchlimit)
-        self._add_albums_directory(albums)
+        self._add_albums_directory(albums, artistid)
     
     def playlist(self, playlistid):
         userid = self._get_login()
@@ -214,10 +195,6 @@ class Groveshark:
         xbmc.log("Playing: " + url)
         xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
     
-    def playNext(self):
-        item = self._get_song_item(groovesharkApi.radioNextSong()[0])
-        self.playSong(item)
-
     def songItem(self, id, name, album, artist, duration, thumb, image):
         url = groovesharkApi.getStreamURL(id)
         # Only try to get the image
@@ -321,7 +298,6 @@ class Groveshark:
             songduration = song[2]
             songalbum = song[3]
             songartist = song[6]
-            #songartistid = song[7]
             u=sys.argv[0]+"?url="+urllib.quote_plus(songurl) \
             +"&mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id=" \
             +str(songid) \
@@ -332,22 +308,16 @@ class Groveshark:
             +"&image="+urllib.quote_plus(songimage)
             fav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)
             unfav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)
-            #similarArtist=sys.argv[0]+"?mode="+str(MODE_SIMILAR_ARTIST)+"&id="+str(songartistid)
-            #similarSong=sys.argv[0]+"?mode="+str(MODE_SIMILAR_SONG)+"&id="+str(songid)
-            #frown=sys.argv[0]+"?mode="+str(MODE_FROWN)+"&id="+str(songid)
             menuItems = []
             menuItems.append(("Grooveshark Favorite", "XBMC.RunPlugin("+fav+")"))
             menuItems.append(("Not Grooveshark Favorite", "XBMC.RunPlugin("+unfav+")"))
-            #menuItems.append(("Listen to similar artist", "XBMC.RunPlugin("+similarArtist+")"))
-            #menuItems.append(("Listen to similar song", "XBMC.RunPlugin("+similarSong+")"))
-            #menuItems.append(("No thanks!", "XBMC.RunPlugin("+frown+")"))
             item.addContextMenuItems(menuItems, replaceItems=False)
             xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False,totalItems=n)
             i = i + 1
         xbmcplugin.setContent(self._handle, 'songs')
         xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
             
-    def _add_albums_directory(self, albums):
+    def _add_albums_directory(self, albums, artistid=0):
         n = len(albums)
         xbmc.log("Found " + str(n) + " albums...")
         i = 0
@@ -359,6 +329,8 @@ class Groveshark:
             albumImage = self._get_icon(album[4], 'album-' + str(albumID))
             self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n)
             i = i + 1
+        if artistid > 0:
+            self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
         xbmcplugin.setContent(self._handle, 'albums')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE)
         xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
@@ -492,14 +464,8 @@ elif mode==MODE_FAVORITE:
 elif mode==MODE_UNFAVORITE:
     grooveshark.unfavorite(lastID)
 
-elif mode==MODE_SIMILAR_ARTIST:
-    grooveshark.similarArtist(lastID)
-
-elif mode==MODE_SIMILAR_SONG:
-    grooveshark.similarSong(lastID)
+elif mode==MODE_SIMILAR_ARTISTS:
+    grooveshark.similarArtists(lastID)
 
-elif mode==MODE_FROWN:
-    grooveshark.frown(lastID)
-    
 if (mode < MODE_SONG):    
     xbmcplugin.endOfDirectory(int(sys.argv[1]))