Fixes.
[clinton/xbmc-groove.git] / default.py
index 49c0e85..3165eed 100644 (file)
@@ -22,17 +22,16 @@ MODE_SONG = 10
 MODE_FAVORITE = 11
 MODE_UNFAVORITE = 12
 
-songsearchlimit = 25
-albumsearchlimit = 15
-artistsearchlimit = 15
+songsearchlimit = 0
+albumsearchlimit = 0
+artistsearchlimit = 0
 
 lastID = 0
 
-rootDir = os.getcwd()
-
-resDir = xbmc.translatePath(os.path.join(rootDir, 'resources'))
+resDir = xbmc.translatePath(os.path.join(os.getcwd(), 'resources'))
 libDir = xbmc.translatePath(os.path.join(resDir,  'lib'))
 imgDir = xbmc.translatePath(os.path.join(resDir,  'img'))
+thumbDir = os.path.join('special://masterprofile/plugin_data/music', os.path.join(os.path.basename(os.getcwd()), 'thumb'))
 
 sys.path.append (libDir)
 from GrooveAPI import *
@@ -54,12 +53,20 @@ class Groveshark:
     songsearchlimit = xbmcplugin.getSetting('songsearchlimit')
     albumsearchlimit = xbmcplugin.getSetting('albumsearchlimit')
     artistsearchlimit = xbmcplugin.getSetting('artistsearchlimit')
-   
+
+    
     def __init__( self ):
         self._handle = int(sys.argv[1])
-    
+
     def categories(self):
+        
         userid = self._get_login()
+        
+        # Setup
+        groovesharkApi.setRemoveDuplicates(True)
+        if not os.path.exists(xbmc.translatePath(thumbDir)):
+            os.makedirs(xbmc.translatePath(thumbDir))
+
         self._addDir('Search songs', '', MODE_SEARCH_SONGS, self.songImg, 0)
         self._addDir('Search albums', '', MODE_SEARCH_ALBUMS, self.albumImg, 0)
         self._addDir('Search artists', '', MODE_SEARCH_ARTISTS, self.artistImg, 0)
@@ -71,7 +78,7 @@ class Groveshark:
     def searchSongs(self):
         query = self._get_keyboard(default="", heading="Search songs")
         if (query): 
-            songs = groovesharkApi.searchSongs(query, limit = songsearchlimit)
+            songs = groovesharkApi.searchSongs(query, limit = xbmcplugin.getSetting('songsearchlimit'))
             if (len(songs) > 0):
                 self._get_songs(songs)
             else:
@@ -82,7 +89,7 @@ class Groveshark:
     def searchAlbums(self):
         query = self._get_keyboard(default="", heading="Search albums")
         if (query): 
-            albums = groovesharkApi.searchAlbums(query, limit = albumsearchlimit)
+            albums = groovesharkApi.searchAlbums(query, limit = xbmcplugin.getSetting('albumsearchlimit'))
             if (len(albums) > 0):
                 self._get_albums(albums)
             else:
@@ -93,7 +100,7 @@ class Groveshark:
     def searchArtists(self):
         query = self._get_keyboard(default="", heading="Search artists")
         if (query): 
-            artists = groovesharkApi.searchArtists(query, limit = artistsearchlimit)
+            artists = groovesharkApi.searchArtists(query, limit = xbmcplugin.getSetting('artistsearchlimit'))
             if (len(artists) > 0):
                 self._get_artists(artists)
             else:
@@ -113,7 +120,7 @@ class Groveshark:
                 self.categories()
     
     def popular(self):
-        popular = groovesharkApi.popularGetSongs(limit = songsearchlimit)
+        popular = groovesharkApi.popularGetSongs(limit = xbmcplugin.getSetting('songsearchlimit'))
         if (len(popular) > 0):
             self._get_songs(popular)
         else:
@@ -135,7 +142,7 @@ class Groveshark:
     def favorite(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Favorite song: " + str(songid))
+            xbmc.log("Favorite playSong: " + str(songid))
             groovesharkApi.favoriteSong(songID = songid)
         else:
             dialog = xbmcgui.Dialog()
@@ -144,33 +151,34 @@ class Groveshark:
     def unfavorite(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Unfavorite song: " + str(songid))
+            xbmc.log("Unfavorite playSong: " + str(songid))
             groovesharkApi.unfavoriteSong(songID = songid)
         else:
             dialog = xbmcgui.Dialog()
             dialog.ok('Grooveshark', 'You must be logged in', 'to remove favorites.')
     
     def album(self,albumid):
-        album = groovesharkApi.albumGetSongs(albumId = albumid, limit = songsearchlimit)
+        album = groovesharkApi.albumGetSongs(albumId = albumid, limit = xbmcplugin.getSetting('songsearchlimit'))
         self._get_songs(album)
     
     def artist(self, artistid):
-        albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = albumsearchlimit)
+        albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = xbmcplugin.getSetting('albumsearchlimit'))
         self._get_albums(albums)
     
     def playlist(self, playlistid):
         userid = self._get_login()
         if (userid != 0):
-            songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = songsearchlimit)
+            songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = xbmcplugin.getSetting('songsearchlimit'))
             self._get_songs(songs)
         else:
             dialog = xbmcgui.Dialog()
             dialog.ok('Grooveshark', 'You must be logged in', 'to get playlists.')
             
-    def song(self, url, name, album, artist, duration):
-        xbmc.log("Playing: " + url)
-        songItem = xbmcgui.ListItem(name + " - " + artist, path=url)
-        songItem.setInfo( type="Music", infoLabels={ "Title": name, "Duration": duration, "Album": album, "Artist": artist} )
+    def playSong(self, url, name, album, artist, duration, thumb, image):
+        xbmc.log("Playing: " + url + ", image " + image)
+        songItem = xbmcgui.ListItem(label = name, path=url, thumbnailImage=thumb, iconImage=image)
+        songItem.setInfo( type="Music", infoLabels={ "title": name, "duration": duration, "album": album, "artist": artist} )
+        songItem.setProperty('mimetype', 'audio/mpeg')
         xbmc.Player().stop()
         xbmc.Player(xbmc.PLAYER_CORE_PAPLAYER).play(url, songItem, False)
         
@@ -191,10 +199,7 @@ class Groveshark:
         else:
             if groovesharkApi.loggedInStatus() == 1:
                 groovesharkApi.logout()
-            try:    
-                userid = groovesharkApi.loginExt(username, password)
-            except (LoginUnknownError):
-                userid = 0
+            userid = groovesharkApi.loginExt(username, password)
             if (userid != 0):
                 xbmc.log("Logged in")
                 return userid
@@ -213,12 +218,13 @@ class Groveshark:
             songDuration = song[2]
             songAlbum = song[3]
             songArtist = song[6]
+            songThumb = song[8]
             songImage = song[9]
-            xbmc.log(songName)
-            self._addSong(songID, songName, groovesharkApi.getStreamURL(songID), songDuration, songAlbum, songArtist, songImage)
+            songUrl = groovesharkApi.getStreamURL(songID)
+            xbmc.log(songName + ", " + songArtist + ", " + songAlbum)
+            self._addSong(songID, songName, songUrl, songDuration, songAlbum, songArtist, songThumb, songImage)
             i = i + 1
         xbmcplugin.setContent(self._handle, 'songs')
-        xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_TITLE_IGNORE_THE)
             
     def _get_albums(self, albums):
         xbmc.log("Found " + str(len(albums)) + " albums...")
@@ -261,30 +267,56 @@ class Groveshark:
         xbmcplugin.setContent(self._handle, 'files')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER)
         
-    def _addSong(self, songid, songname, songurl, songduration, songalbum, songartist, songimage):
+    def _addSong(self, songid, songname, songurl, songduration, songalbum, songartist, songthumb, songimage):
+        songImg = self._getThumb(songimage, str(songid) + "-image")
+        if songImg == "":
+            songImg = songimage
+        songThm = self._getThumb(songthumb, str(songid) + "-thumb")
+        if songThm == "":
+            songThm = songthumb
         u=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \
         +"&album="+urllib.quote_plus(songalbum) \
         +"&artist="+urllib.quote_plus(songartist) \
-        +"&duration="+str(songduration)
-        songItem = xbmcgui.ListItem(songname + " - " + songartist, iconImage=songimage, thumbnailImage=songimage, path=songurl)
-        songItem.setInfo( type="Music", infoLabels={ "Title": songname, "Duration": songduration, "Album": songalbum, "Artist": songartist} )
+        +"&duration="+str(songduration) \
+        +"&thumb="+urllib.quote_plus(songThm) \
+        +"&image="+urllib.quote_plus(songImg)
+        xbmc.log("Artist is " + songartist + ", " + songThm + ", " + songThm)
+        songItem = xbmcgui.ListItem(label = songartist + " - " + songalbum + " - " + songname, iconImage=songImg, thumbnailImage=songThm, path=songurl)
+        songItem.setInfo( type="Music", infoLabels={ "title": songname, "duration": songduration, "album": songalbum, "artist": songartist} )
         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)
         menuItems = []
         menuItems.append(("Grooveshark Favorite",      "XBMC.RunPlugin("+fav+")"))
         menuItems.append(("Not Grooveshark Favorite", "XBMC.RunPlugin("+unfav+")"))
         songItem.addContextMenuItems(menuItems, replaceItems=False)
-        return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=songItem,isFolder=False)
+        xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=songItem,isFolder=False)
+        return songItem
       
     def _addDir(self, name, url, mode, iconimage, id):
         u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
         dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
-        dir.setInfo( type="Music", infoLabels={ "Title": name } )
+        dir.setInfo( type="Music", infoLabels={ "title": name } )
         # Codes from http://xbmc-scripting.googlecode.com/svn/trunk/Script%20Templates/common/gui/codes.py
         menuItems = []
         menuItems.append(("Select", "XBMC.executebuiltin(Action(7))"))
         dir.addContextMenuItems(menuItems, replaceItems=True)
         return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True)
+    
+    # File download            
+    def _getThumb(self, url, id):
+        # Get the channel icon
+        localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn'
+        try:
+            if os.path.isfile(localThumb) == False:
+                loc = urllib.URLopener()
+                loc.retrieve(url, localThumb)
+        except:
+            xbmc.log('URL download failed of ' + url + ' to ' + localThumb)
+            return ""
+
+        return os.path.join(os.path.join(thumbDir, str(id))) + '.tbn'
+     
+        
    
 def get_params():
     param=[]
@@ -354,7 +386,11 @@ elif mode==MODE_SONG:
     except: pass
     try: duration=int(params["duration"])
     except: pass
-    grooveshark.song(url, name, album, artist, duration)
+    try: thumb=urllib.unquote_plus(params["thumb"])
+    except: pass
+    try: image=urllib.unquote_plus(params["image"])
+    except: pass
+    grooveshark.playSong(url, name, album, artist, duration, thumb, image)
 
 elif mode==MODE_ARTIST:
     grooveshark.artist(lastID)