From 48e0c13869ef6c64f4e4dbf926a6de040b6a4e8a Mon Sep 17 00:00:00 2001 From: stephendenham Date: Sun, 2 Oct 2011 17:27:24 +0000 Subject: [PATCH] Fixes. git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@63 2dec19e3-eb1d-4749-8193-008c8bba0994 --- default.py | 152 ++++++++++++-------------------- resources/lib/GroovesharkAPI.py | 8 +- 2 files changed, 61 insertions(+), 99 deletions(-) diff --git a/default.py b/default.py index 5dd94c2..4e898ca 100644 --- a/default.py +++ b/default.py @@ -255,12 +255,12 @@ class Grooveshark: self._handle = int(sys.argv[1]) if os.path.isdir(cacheDir) == False: os.makedirs(cacheDir) - if __debuggui__: + if __debuggui__ == True: xbmc.log(__language__(30012) + " " + cacheDir) artDir = xbmc.translatePath(thumbDir) if os.path.isdir(artDir) == False: os.makedirs(artDir) - if __debuggui__: + if __debuggui__ == True: xbmc.log(__language__(30012) + " " + artDir) # Top-level menu @@ -350,7 +350,7 @@ class Grooveshark: if (len(artists) > 0): artist = artists[0] artistID = artist[1] - if __debuggui__: + if __debuggui__ == True: xbmc.log("Found " + artist[0] + "...") albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit) if (len(albums) > 0): @@ -407,7 +407,7 @@ class Grooveshark: def favorite(self, songid): userid = self._get_login() if (userid != 0): - if __debuggui__: + if __debuggui__ == True: xbmc.log("Favorite song: " + str(songid)) groovesharkApi.addUserFavoriteSong(songID = songid) xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30036) + ', 1000, ' + thumbDef + ')') @@ -419,7 +419,7 @@ class Grooveshark: def unfavorite(self, songid, prevMode=0): userid = self._get_login() if (userid != 0): - if __debuggui__: + if __debuggui__ == True: 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 + ')') @@ -459,7 +459,7 @@ class Grooveshark: if (len(artists) > 0): artist = artists[0] artistID = artist[1] - if __debuggui__: + if __debuggui__ == True: xbmc.log("Found " + artist[0] + "...") songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit) if (len(songs) > 0): @@ -480,30 +480,33 @@ class Grooveshark: global playTimer global player if item != None: - songid = self._setItem(item) - # Wait for play then start timer - seconds = 0 - while seconds < STREAM_TIMEOUT: - try: - if player.isPlayingAudio() == True: - if playTimer != None: - playTimer.cancel() - songMarkTime = 0 - stream = self._getSongStream(songid) - duration = stream['duration'] - playTimer = PlayTimer(1, markSong, duration, [songid, duration, stream['streamKey'], stream['streamServerID']]) - playTimer.start() - break - except: pass - time.sleep(1) - seconds = seconds + 1 - - # If not playing after a few seconds try to refresh the URL - if (player.isPlayingAudio() == False and seconds == STREAM_RETRY): - if __debug__: - xbmc.log("Refreshing URL") - item.setProperty('url', '') - self._setItem(item) + songid = item.getProperty('songid') + duration = self._getSongDuration(songid) + url = '' + stream = groovesharkApi.getSubscriberStreamKey(songid) + if stream != False: + url = stream['url'] + if url != '': + item.setPath(url) + xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) + if __debuggui__ == True: + xbmc.log("Grooveshark playing: " + url) + # Wait for play then start timer + seconds = 0 + while seconds < STREAM_TIMEOUT: + try: + if player.isPlayingAudio() == True: + if playTimer != None: + playTimer.cancel() + songMarkTime = 0 + playTimer = PlayTimer(1, markSong, duration, [songid, duration, stream['streamKey'], stream['streamServerID']]) + playTimer.start() + break + except: pass + time.sleep(1) + seconds = seconds + 1 + else: + xbmc.log("No song URL") else: xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30044) + ', 1000, ' + thumbDef + ')') @@ -514,9 +517,7 @@ class Grooveshark: trackLabel = name + " - " + album + " - " + artist else: trackLabel = artist + " - " + album + " - " + name - stream = self._getSongStream(songid) - duration = stream['duration'] - url = stream['url'] + duration = self._getSongDuration(songid) 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') @@ -527,7 +528,6 @@ class Grooveshark: item.setProperty('album', album) item.setProperty('artist', artist) item.setProperty('duration', str(duration)) - item.setProperty('url', str(url)) return item @@ -618,7 +618,7 @@ class Grooveshark: else: playlist = playlists[i] playlistid = playlist[1] - if __debuggui__: + if __debuggui__ == True: xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid)) songIDs=[] songs = groovesharkApi.getPlaylistSongs(playlistid) @@ -719,21 +719,6 @@ class Grooveshark: else: return thumbDef - # Set URL - def _setItem(self, item): - url = item.getProperty('url') - songid = item.getProperty('songid') - if url == '': - stream = groovesharkApi.getSubscriberStreamKey(songid) - if stream != False: - url = stream['url'] - self._setSongStream(songid, stream) - item.setPath(url) - xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) - if __debuggui__: - xbmc.log("Grooveshark playing: " + songid) - return songid - # Add songs to directory def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, offset=0, playlistid=0, playlistname='', isFavorites=False): @@ -744,7 +729,7 @@ class Grooveshark: # No pages needed if offset == 0 and totalSongs <= self.songspagelimit: - if __debuggui__: + if __debuggui__ == True: xbmc.log("Found " + str(totalSongs) + " songs...") # Pages else: @@ -765,8 +750,8 @@ class Grooveshark: while n < end: song = songs[n] songid = song[1] - stream = self._getSongStream(songid) - if stream['url'] != '': + duration = self._getSongDuration(songid) + if duration != -1: item = self._get_song_item(song, trackLabelFormat) coverart = item.getProperty('coverart') songname = song[0] @@ -795,7 +780,7 @@ class Grooveshark: id = id + 1 else: end = min(end + 1,totalSongs) - if __debuggui__: + if __debuggui__ == True: xbmc.log(song[0] + " does not exist.") n = n + 1 @@ -810,7 +795,7 @@ class Grooveshark: def _add_albums_directory(self, albums, artistid=0): n = len(albums) itemsExisting = n - if __debuggui__: + if __debuggui__ == True: xbmc.log("Found " + str(n) + " albums...") i = 0 while i < n: @@ -835,7 +820,7 @@ class Grooveshark: def _add_artists_directory(self, artists): n = len(artists) itemsExisting = n - if __debuggui__: + if __debuggui__ == True: xbmc.log("Found " + str(n) + " artists...") i = 0 while i < n: @@ -854,7 +839,7 @@ class Grooveshark: # Add playlists to directory def _add_playlists_directory(self, playlists): n = len(playlists) - if __debuggui__: + if __debuggui__ == True: xbmc.log("Found " + str(n) + " playlists...") i = 0 while i < n: @@ -916,21 +901,17 @@ class Grooveshark: xbmc.log("An error occurred saving songs") pass - def _getSongStream(self, songid): + def _getSongDuration(self, songid): id = int(songid) duration = -1 - streams = [] - url = '' - path = os.path.join(cacheDir, 'streams.dmp') + durations = [] + path = os.path.join(cacheDir, 'durations.dmp') try: f = open(path, 'rb') - streams = pickle.load(f) - for song in streams: + durations = pickle.load(f) + for song in durations: if song[0] == id: duration = song[1] - url = song[2] - streamKey = song[3] - streamServerID = song[4] break; f.close() except: @@ -940,46 +921,25 @@ class Grooveshark: if duration < 0 and groovesharkApi.getDoesSongExist(songid): stream = groovesharkApi.getSubscriberStreamKey(songid) usecs = stream['uSecs'] - url = stream['url'] - streamKey = stream['StreamKey'] - streamServerID = stream['StreamServerID'] duration = self._setDuration(usecs) - song = [id, duration, url, streamKey, streamServerID] - streams.append(song) - self._setSongStreams(streams) - - return {'duration':duration, 'url':url, 'streamKey':streamKey, 'streamServerID':streamServerID} + song = [id, duration] + durations.append(song) + self._setSongDuration(durations) - def _setSongStream(self, songid, stream): - path = os.path.join(cacheDir, 'streams.dmp') - try: - f = open(path, 'rb') - streams = pickle.load(f) - for song in streams: - if song[0] == songid: - song[1] = self._setDuration(stream['uSecs']) - song[2] = stream['url'] - song[3] = stream['streamKey'] - song[4] = stream['streamServerID'] - self._setSongStreams(streams) - break; - f.close() - except: - xbmc.log("An error occurred saving stream") - pass + return duration - def _setSongStreams(self, streams): + def _setSongDuration(self, durations): try: # Create the cache directory if it doesn't exist. if not os.path.exists(cacheDir): os.makedirs(cacheDir) - path = os.path.join(cacheDir, 'streams.dmp') + path = os.path.join(cacheDir, 'durations.dmp') f = open(path, 'wb') - pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL) + pickle.dump(durations, f, protocol=pickle.HIGHEST_PROTOCOL) f.close() except: - xbmc.log("An error occurred saving streams") + xbmc.log("An error occurred saving duration") pass def _setDuration(self, usecs): @@ -991,7 +951,7 @@ class Grooveshark: def get_params(): param=[] paramstring=sys.argv[2] - if __debuggui__: + if __debuggui__ == True: xbmc.log(paramstring) if len(paramstring)>=2: params=sys.argv[2] diff --git a/resources/lib/GroovesharkAPI.py b/resources/lib/GroovesharkAPI.py index 232f849..7790f17 100644 --- a/resources/lib/GroovesharkAPI.py +++ b/resources/lib/GroovesharkAPI.py @@ -437,10 +437,12 @@ class GrooveAPI: # Debug on off def setDebug(self, state): - self._apiDebug = state - if (self._apiDebug): + if state == True: + self._apiDebug = True + else: + self._apiDebug = False + if self._apiDebug == True: print "API debug is on" - # Extract song data def _parseSongs(self, items, limit=0): -- 2.20.1