Fix item counts in directory generation.
[clinton/xbmc-groove.git] / default.py
index 2053c2a..30460dd 100644 (file)
@@ -23,7 +23,7 @@ resDir = xbmc.translatePath(os.path.join(baseDir, 'resources'))
 libDir = xbmc.translatePath(os.path.join(resDir,  'lib'))
 imgDir = xbmc.translatePath(os.path.join(resDir,  'img'))
 thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'thumb'))
-favoritesCache = xbmc.translatePath('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'favorites.dmp'))
+favoritesCache = xbmc.translatePath(os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'favorites.dmp')))
 
 sys.path.append (libDir)
 from GrooveAPI import *
@@ -169,8 +169,8 @@ class Groveshark:
     def similarSong(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Frown playSong: " + str(songid))
-            if groovesharkApi.radioSong(songId = songid) and groovesharkApi.radioStartSongs() == True:
+            xbmc.log("Add song: " + str(songid))
+            if groovesharkApi.radioSong(songId = songid) != False and groovesharkApi.radioStartSongs() == True:
                 self.playNext()
             else:
                 dialog = xbmcgui.Dialog()
@@ -182,8 +182,8 @@ class Groveshark:
     def similarArtist(self, artistId):
         userid = self._get_login()
         if (userid != 0):
-            xbmc.log("Add radio artist of playSong: " + str(artistId))
-            if groovesharkApi.radioArtist(artistId = artistId) and groovesharkApi.radioStartArtists() == True:
+            xbmc.log("Add radio artist: " + str(artistId))
+            if groovesharkApi.radioArtist(artistId = artistId) != False and groovesharkApi.radioStartArtists() == True:
                 self.playNext()
             else:
                 dialog = xbmcgui.Dialog()
@@ -220,8 +220,13 @@ class Groveshark:
 
     def songItem(self, id, name, album, artist, duration, thumb, image):
         url = groovesharkApi.getStreamURL(id)
-        songImg = self._get_icon(image, 'song-' + str(id) + "-image")
-        songThm = self._get_icon(thumb, 'song-' + str(id) + "-thumb")
+        # Only try to get the image
+        if image != "":
+            songImg = self._get_icon(image, 'song-' + str(id) + "-image")
+            songThm = songImg
+        else:
+            songThm = self._get_icon(thumb, 'song-' + str(id) + "-thumb")
+            songImg = songThm
         item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, path=url, thumbnailImage=songThm, iconImage=songImg)
         item.setInfo( type="music", infoLabels={ "title": name, "duration": duration, "album": album, "artist": artist} )
         item.setProperty('mimetype', 'audio/mpeg')
@@ -235,7 +240,7 @@ class Groveshark:
     def _get_favorites(self):
         favorites = []
         # if the cache does not exist or is older than x hours then reload
-        if (os.path.isfile(favoritesCache) == False) or (time.gmtime() - os.path.gmtime(favoritesCache) > 3*60*60):
+        if (os.path.isfile(favoritesCache) == False) or (time.time() - os.path.getmtime(favoritesCache) > 3*60*60):
             xbmc.log("Refresh favorites cache")
             userid = self._get_login()
             if (userid != 0):
@@ -290,9 +295,9 @@ class Groveshark:
     # File download            
     def _get_icon(self, url, id):
         localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn'
-        xbmc.log('Downloading ' + url + ' to ' + localThumb)
         try:
             if os.path.isfile(localThumb) == False:
+                xbmc.log('Downloading ' + url + ' to ' + localThumb)
                 loc = urllib.URLopener()
                 loc.retrieve(url, localThumb)
         except:
@@ -316,7 +321,7 @@ class Groveshark:
             songduration = song[2]
             songalbum = song[3]
             songartist = song[6]
-            songartistid = song[7]
+            #songartistid = song[7]
             u=sys.argv[0]+"?url="+urllib.quote_plus(songurl) \
             +"&mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id=" \
             +str(songid) \
@@ -327,59 +332,66 @@ 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)
+            #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+")"))
+            #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)
+            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):
-        xbmc.log("Found " + str(len(albums)) + " albums...")
+        n = len(albums)
+        xbmc.log("Found " + str(n) + " albums...")
         i = 0
-        while i < len(albums):
+        while i < n:
             album = albums[i]
             albumArtistName = album[0]
             albumName = album[2]
             albumID = album[3]
             albumImage = self._get_icon(album[4], 'album-' + str(albumID))
-            self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID)
+            self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n)
             i = i + 1
         xbmcplugin.setContent(self._handle, 'albums')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE)
+        xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
             
     def _add_artists_directory(self, artists):
-        xbmc.log("Found " + str(len(artists)) + " artists...")
+        n = len(artists)
+        xbmc.log("Found " + str(n) + " artists...")
         i = 0
-        while i < len(artists):
+        while i < n:
             artist = artists[i]
             artistName = artist[0]
             artistID = artist[1]
-            self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID)
+            self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n)
             i = i + 1
         xbmcplugin.setContent(self._handle, 'artists')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE)
+        xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
             
     def _add_playlists_directory(self, playlists):
-        xbmc.log("Found " + str(len(playlists)) + " playlists...")
+        n = len(playlists)
+        xbmc.log("Found " + str(n) + " playlists...")
         i = 0
-        while i < len(playlists):
+        while i < n:
             playlist = playlists[i]
             playlistName = playlist[0]
             playlistID = playlist[1]
-            self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID)
+            self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n)
             i = i + 1  
         xbmcplugin.setContent(self._handle, 'files')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER)
+        xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
       
-    def _add_dir(self, name, url, mode, iconimage, id):
+    def _add_dir(self, name, url, mode, iconimage, id, items=1):
         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 } )
@@ -387,7 +399,7 @@ class Groveshark:
         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)
+        return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items)
 
    
 def get_params():