From 5e72534b4750dd1670b8e2940d408eef46a5fe4e Mon Sep 17 00:00:00 2001 From: stephendenham Date: Sun, 2 Oct 2011 13:37:07 +0000 Subject: [PATCH] Verify items before adding to directories. git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@59 2dec19e3-eb1d-4749-8193-008c8bba0994 --- addon.xml | 4 +- changelog.txt | 4 + default.py | 223 +++++++++++++++---------- description.xml | 4 +- resources/language/English/strings.xml | 2 +- resources/lib/GroovesharkAPI.py | 50 +++++- resources/settings.xml | 3 + 7 files changed, 191 insertions(+), 99 deletions(-) diff --git a/addon.xml b/addon.xml index 8b111ef..7827afb 100644 --- a/addon.xml +++ b/addon.xml @@ -1,6 +1,6 @@ + version="0.6.2" provider-name="Stephen Denham"> @@ -12,7 +12,7 @@ all Grooveshark addon for XBMC. - Grooveshark addon for XBMC. You should enter your Grooveshark username and password in the addon settings. + Grooveshark addon for XBMC. You should enter your Grooveshark Anywhere username and password in the addon settings. diff --git a/changelog.txt b/changelog.txt index b373285..5608c3f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,7 @@ +0.6.2 + +Check item existence before adding to directory. + 0.6.1 Some more changes for XBMC addon compliancy. diff --git a/default.py b/default.py index cb8f31f..8ebbdd4 100644 --- a/default.py +++ b/default.py @@ -25,6 +25,7 @@ __author__ = __addon__.getAddonInfo('author') __version__ = __addon__.getAddonInfo('version') __language__ = __addon__.getLocalizedString + MODE_SEARCH_SONGS = 1 MODE_SEARCH_ALBUMS = 2 MODE_SEARCH_ARTISTS = 3 @@ -219,7 +220,7 @@ class PlayTimer(Thread): return self.iterations * self.interval -class Groveshark: +class Grooveshark: albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) @@ -240,17 +241,21 @@ class Groveshark: songspagelimit = int(settings.getSetting('songspagelimit')) username = settings.getSetting('username') password = settings.getSetting('password') + userid = 0 def __init__( self ): self._handle = int(sys.argv[1]) + self.setDebug() if os.path.isdir(cacheDir) == False: os.makedirs(cacheDir) - xbmc.log(__language__(30012) + " " + cacheDir) + if self.debug: + xbmc.log(__language__(30012) + " " + cacheDir) artDir = xbmc.translatePath(thumbDir) if os.path.isdir(artDir) == False: os.makedirs(artDir) - xbmc.log(__language__(30012) + " " + artDir) + if self.debug: + xbmc.log(__language__(30012) + " " + artDir) # Top-level menu def categories(self): @@ -339,7 +344,8 @@ class Groveshark: if (len(artists) > 0): artist = artists[0] artistID = artist[1] - xbmc.log("Found " + artist[0] + "...") + if self.debug: + xbmc.log("Found " + artist[0] + "...") albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit) if (len(albums) > 0): self._add_albums_directory(albums, artistID) @@ -395,7 +401,8 @@ class Groveshark: def favorite(self, songid): userid = self._get_login() if (userid != 0): - xbmc.log("Favorite song: " + str(songid)) + if self.debug: + xbmc.log("Favorite song: " + str(songid)) groovesharkApi.addUserFavoriteSong(songID = songid) xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30036) + ', 1000, ' + thumbDef + ')') else: @@ -406,7 +413,8 @@ class Groveshark: def unfavorite(self, songid, prevMode=0): userid = self._get_login() if (userid != 0): - xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) + if self.debug: + xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) groovesharkApi.removeUserFavoriteSongs(songIDs = songid) xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30038) + ', 1000, ' + thumbDef + ')') # Refresh to remove item from directory @@ -445,7 +453,8 @@ class Groveshark: if (len(artists) > 0): artist = artists[0] artistID = artist[1] - xbmc.log("Found " + artist[0] + "...") + if self.debug: + xbmc.log("Found " + artist[0] + "...") songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit) if (len(songs) > 0): self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) @@ -466,10 +475,13 @@ class Groveshark: global player if item != None: songid = item.getProperty('songid') - stream = groovesharkApi.getSubscriberStreamKey(songid) - url = stream['url'] + url = item.getProperty('url') + if url == '': + stream = groovesharkApi.getSubscriberStreamKey(songid) + url = stream['url'] item.setPath(url) - xbmc.log("Grooveshark playing: " + url) + if self.debug: + xbmc.log("Grooveshark playing: " + url) xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) # Wait for play then start time seconds = 0 @@ -496,7 +508,9 @@ class Groveshark: trackLabel = name + " - " + album + " - " + artist else: trackLabel = artist + " - " + album + " - " + name - duration = self._getSongDuration(songid) + stream = self._getSongStream(songid) + duration = stream['duration'] + url = stream['url'] item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg) item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist, "duration": duration} ) item.setProperty('mimetype', 'audio/mpeg') @@ -507,12 +521,13 @@ class Groveshark: item.setProperty('album', album) item.setProperty('artist', artist) item.setProperty('duration', str(duration)) + item.setProperty('url', str(url)) return item # Next page of songs - def songPage(self, page, trackLabelFormat, playlistid = 0, playlistname = ''): - self._add_songs_directory([], trackLabelFormat, page, playlistid = playlistid, playlistname = playlistname) + def songPage(self, offset, trackLabelFormat, playlistid = 0, playlistname = ''): + self._add_songs_directory([], trackLabelFormat, offset, playlistid = playlistid, playlistname = playlistname) # Make a playlist from an album def makePlaylist(self, albumid, name): @@ -597,7 +612,8 @@ class Groveshark: else: playlist = playlists[i] playlistid = playlist[1] - xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid)) + if self.debug: + xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid)) songIDs=[] songs = groovesharkApi.getPlaylistSongs(playlistid) for song in songs: @@ -649,6 +665,16 @@ class Groveshark: dialog = xbmcgui.Dialog() dialog.ok(__language__(30008), __language__(30068)) self.categories() + + # Debug + def setDebug(self): + self.debug = self.settings.getSetting('debug') + if self.debug: + xbmc.log("Debug is on") + + # Debug + def getDebug(self): + return self.debug # Get keyboard input def _get_keyboard(self, default="", heading="", hidden=False): @@ -698,64 +724,73 @@ class Groveshark: return thumbDef # Add songs to directory - def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, page=0, playlistid=0, playlistname='', isFavorites=False): + def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, offset=0, playlistid=0, playlistname='', isFavorites=False): totalSongs = len(songs) - page = int(page) + offset = int(offset) + start = 0 + end = totalSongs # No pages needed - if page == 0 and totalSongs <= self.songspagelimit: - xbmc.log("Found " + str(totalSongs) + " songs...") + if offset == 0 and totalSongs <= self.songspagelimit: + if self.debug: + xbmc.log("Found " + str(totalSongs) + " songs...") # Pages else: - # Cache all songs - if page == 0: + # Cache all next pages songs + if offset == 0: self._setSavedSongs(songs) else: songs = self._getSavedSongs() totalSongs = len(songs) if totalSongs > 0: - start = page * self.songspagelimit - end = start + self.songspagelimit - songs = songs[start:end] + start = offset + end = min(start + self.songspagelimit,totalSongs) id = 0 - for song in songs: - item = self._get_song_item(song, trackLabelFormat) - coverart = item.getProperty('coverart') - songname = song[0] + n = start + items = end - start + while n < end: + song = songs[n] songid = song[1] - songalbum = song[2] - songartist = song[4] - - - u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \ - +"&album="+urllib.quote_plus(songalbum) \ - +"&artist="+urllib.quote_plus(songartist) \ - +"&coverart="+urllib.quote_plus(coverart) - fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) - unfav=sys.argv[0]+"?mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode=" - menuItems = [] - if isFavorites == True: - unfav = unfav +str(MODE_FAVORITES) - else: - menuItems.append((__language__(30071), "XBMC.RunPlugin("+fav+")")) - menuItems.append((__language__(30072), "XBMC.RunPlugin("+unfav+")")) - if playlistid > 0: - rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(songid)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname - menuItems.append((__language__(30073), "XBMC.RunPlugin("+rmplaylstsong+")")) + stream = self._getSongStream(songid) + if stream['url'] != '': + item = self._get_song_item(song, trackLabelFormat) + coverart = item.getProperty('coverart') + songname = song[0] + songalbum = song[2] + songartist = song[4] + u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \ + +"&album="+urllib.quote_plus(songalbum) \ + +"&artist="+urllib.quote_plus(songartist) \ + +"&coverart="+urllib.quote_plus(coverart) + fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) + unfav=sys.argv[0]+"?mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode=" + menuItems = [] + if isFavorites == True: + unfav = unfav +str(MODE_FAVORITES) + else: + menuItems.append((__language__(30071), "XBMC.RunPlugin("+fav+")")) + menuItems.append((__language__(30072), "XBMC.RunPlugin("+unfav+")")) + if playlistid > 0: + rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(songid)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname + menuItems.append((__language__(30073), "XBMC.RunPlugin("+rmplaylstsong+")")) + else: + addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG) + menuItems.append((__language__(30074), "XBMC.RunPlugin("+addplaylstsong+")")) + item.addContextMenuItems(menuItems, replaceItems=False) + xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=items) + id = id + 1 else: - addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG) - menuItems.append((__language__(30074), "XBMC.RunPlugin("+addplaylstsong+")")) - item.addContextMenuItems(menuItems, replaceItems=False) - xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=len(songs)) - id = id + 1 + end = min(end + 1,totalSongs) + if self.debug: + xbmc.log(song[0] + " does not exist.") + n = n + 1 - page = page + 1 - if totalSongs > page * self.songspagelimit: - u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=playlistid"+"&page="+str(page)+"&label="+str(trackLabelFormat)+"&name="+playlistname - self._add_dir(__language__(30075) + '...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - (page * self.songspagelimit)) + if totalSongs > end: + u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=playlistid"+"&offset="+str(end)+"&label="+str(trackLabelFormat)+"&name="+playlistname + self._add_dir(__language__(30075) + '...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - end) xbmcplugin.setContent(self._handle, 'songs') xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) @@ -763,15 +798,20 @@ class Groveshark: # Add albums to directory def _add_albums_directory(self, albums, artistid=0): n = len(albums) - xbmc.log("Found " + str(n) + " albums...") + itemsExisting = n + if self.debug: + xbmc.log("Found " + str(n) + " albums...") i = 0 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, n) + if groovesharkApi.getDoesAlbumExist(albumID): + albumArtistName = album[0] + albumName = album[2] + albumImage = self._get_icon(album[4], 'album-' + str(albumID)) + self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, itemsExisting) + else: + itemsExisting = itemsExisting - 1 i = i + 1 # Not supported by key #if artistid > 0: @@ -783,13 +823,18 @@ class Groveshark: # Add artists to directory def _add_artists_directory(self, artists): n = len(artists) - xbmc.log("Found " + str(n) + " artists...") + itemsExisting = n + if self.debug: + xbmc.log("Found " + str(n) + " artists...") i = 0 while i < n: artist = artists[i] - artistName = artist[0] artistID = artist[1] - self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n) + if groovesharkApi.getDoesArtistExist(artistID): + artistName = artist[0] + self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, itemsExisting) + else: + itemsExisting = itemsExisting - 1 i = i + 1 xbmcplugin.setContent(self._handle, 'artists') xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) @@ -798,7 +843,8 @@ class Groveshark: # Add playlists to directory def _add_playlists_directory(self, playlists): n = len(playlists) - xbmc.log("Found " + str(n) + " playlists...") + if self.debug: + xbmc.log("Found " + str(n) + " playlists...") i = 0 while i < n: playlist = playlists[i] @@ -825,12 +871,12 @@ class Groveshark: if mode == MODE_ALBUM: mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id) menuItems.append((__language__(30076), "XBMC.RunPlugin("+mkplaylst+")")) - # Broken rename/delete are broken in API if mode == MODE_PLAYLIST: rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) menuItems.append((__language__(30077), "XBMC.RunPlugin("+rmplaylst+")")) mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) menuItems.append((__language__(30078), "XBMC.RunPlugin("+mvplaylst+")")) + dir.addContextMenuItems(menuItems, replaceItems=False) return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) @@ -859,46 +905,48 @@ class Groveshark: xbmc.log("An error occurred saving songs") pass - def _getSongDuration(self, songid): - path = os.path.join(cacheDir, 'duration.dmp') + def _getSongStream(self, songid): id = int(songid) - durations = [] duration = -1 - - # Try cache first + streams = [] + url = '' + path = os.path.join(cacheDir, 'streams.dmp') try: f = open(path, 'rb') - durations = pickle.load(f) - for song in durations: + streams = pickle.load(f) + for song in streams: if song[0] == id: duration = song[1] + url = song[2] + break; f.close() except: pass - if duration < 0: + if duration < 0 and groovesharkApi.getDoesSongExist(songid): stream = groovesharkApi.getSubscriberStreamKey(songid) + url = stream['url'] usecs = stream['uSecs'] if usecs < 60000000: usecs = usecs * 10 # Some durations are 10x to small duration = usecs / 1000000 - song = [id, duration] - durations.append(song) - self._setSongDuration(durations) - - return duration + song = [id, duration, url] + streams.append(song) + self._setSongStream(streams) - def _setSongDuration(self, durations): + return {'duration':duration, 'url':url} + + def _setSongStream(self, streams): try: # Create the 'data' directory if it doesn't exist. if not os.path.exists(cacheDir): os.makedirs(cacheDir) - path = os.path.join(cacheDir, 'duration.dmp') + path = os.path.join(cacheDir, 'streams.dmp') f = open(path, 'wb') - pickle.dump(durations, f, protocol=pickle.HIGHEST_PROTOCOL) + pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL) f.close() except: - xbmc.log("An error occurred saving durations") + xbmc.log("An error occurred saving streams") pass @@ -906,7 +954,8 @@ class Groveshark: def get_params(): param=[] paramstring=sys.argv[2] - xbmc.log(paramstring) + if grooveshark.getDebug(): + xbmc.log(paramstring) if len(paramstring)>=2: params=sys.argv[2] cleanedparams=params.replace('?','') @@ -922,7 +971,9 @@ def get_params(): return param # Main -grooveshark = Groveshark(); +grooveshark = Grooveshark(); +grooveshark.setDebug() +groovesharkApi.setDebug(grooveshark.getDebug()) params=get_params() mode=None @@ -967,11 +1018,11 @@ elif mode==MODE_PLAYLISTS: grooveshark.playlists() elif mode==MODE_SONG_PAGE: - try: page=urllib.unquote_plus(params["page"]) + try: offset=urllib.unquote_plus(params["offset"]) except: pass try: label=urllib.unquote_plus(params["label"]) except: pass - grooveshark.songPage(page, label, id, name) + grooveshark.songPage(offset, label, id, name) elif mode==MODE_SONG: try: album=urllib.unquote_plus(params["album"]) diff --git a/description.xml b/description.xml index 8fb5ef5..420a647 100644 --- a/description.xml +++ b/description.xml @@ -18,7 +18,7 @@ Grooveshark XBMC - 0.6.1 + 0.6.2 - windows,linux + windows,linux, osx