From: stephendenham Date: Sat, 18 Aug 2012 11:56:00 +0000 (+0000) Subject: Cache streams. X-Git-Url: https://git.hcoop.net/clinton/xbmc-groove.git/commitdiff_plain/a9e5e6c494b0901d5b93028bc4ccedb0eebfd4fc Cache streams. git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@77 2dec19e3-eb1d-4749-8193-008c8bba0994 --- diff --git a/default.py b/default.py index 73d6629..9d56556 100644 --- a/default.py +++ b/default.py @@ -483,6 +483,7 @@ class Grooveshark: player.stop() if item != None: # Get stream as it could have expired + item.select(True) url = '' songid = item.getProperty('songid') stream = groovesharkApi.getSubscriberStreamKey(songid) @@ -491,40 +492,43 @@ class Grooveshark: key = stream['StreamKey'] server = stream['StreamServerID'] duration = int(self._setDuration(stream['uSecs'])) - if url != '': - item.setPath(url) - xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) - if __debugging__ : - xbmc.log("Grooveshark playing: " + url) - # Wait for play then start timer - xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) - 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, key, server]) - playTimer.start() - break - except: pass - time.sleep(1) - seconds = seconds + 1 + stream = [songid, duration, url, key, server] + self._setSongStream(stream) + if url != '': + item.setPath(url) + xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) + if __debugging__ : + 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, self._setDuration(duration), [songid, duration, key, server]) + playTimer.start() + break + except: pass + time.sleep(1) + seconds = seconds + 1 + else: + xbmc.log("No song URL") else: - xbmc.log("No song URL") + xbmc.log("No song stream") else: xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30044) + ', 1000, ' + thumbDef + ')') # Make a song directory item def songItem(self, songid, name, album, artist, coverart, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL): - stream = groovesharkApi.getSubscriberStreamKey(songid) + stream = self._getSongStream(songid) if stream != False: - url = stream['url'] - key = stream['StreamKey'] - server = stream['StreamServerID'] - duration = self._setDuration(stream['uSecs']) + duration = stream[1] + url = stream[2] + key = stream[3] + server = stream[4] songImg = self._get_icon(coverart, 'song-' + str(songid) + "-image") if int(trackLabelFormat) == NAME_ALBUM_ARTIST_LABEL: trackLabel = name + " - " + album + " - " + artist @@ -916,7 +920,78 @@ class Grooveshark: def _setDuration(self, usecs): if usecs < 60000000: usecs = usecs * 10 # Some durations are 10x to small - return usecs / 1000000 + return int(usecs / 1000000) + + def _getSongStream(self, songid): + id = int(songid) + stream = None + streams = [] + path = os.path.join(cacheDir, 'streams.dmp') + try: + f = open(path, 'rb') + streams = pickle.load(f) + for song in streams: + if song[0] == id: + duration = song[1] + url = song[2] + key = song[3] + server = song[4] + stream = [id, duration, url, key, server] + if __debugging__ : + xbmc.log("Found " + str(id) + " in cache") + break; + f.close() + except: + pass + + # Not in cache + if stream == None: + stream = groovesharkApi.getSubscriberStreamKey(songid) + if stream != False and stream['url'] != '': + duration = self._setDuration(stream['uSecs']) + url = stream['url'] + key = stream['StreamKey'] + server = stream['StreamServerID'] + stream = [id, duration, url, key, server] + self._addSongStream(stream, streams) + + return stream + + def _addSongStream(self, stream, streams): + try: + streams.append(stream) + # 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') + f = open(path, 'wb') + pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL) + f.close() + except: + xbmc.log("An error occurred adding stream") + pass + + def _setSongStream(self, stream): + id = int(stream[0]) + stream[1] = self._setDuration(stream[1]) + streams = [] + path = os.path.join(cacheDir, 'streams.dmp') + try: + f = open(path, 'wb') + streams = pickle.load(f) + i = 0 + for song in streams: + if song[0] == id: + streams[i] = stream + if __debugging__ : + xbmc.log("Added " + str(id) + " to cache") + pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL) + break; + i = i + 1 + f.close() + except: + xbmc.log("An error occurred setting stream") + # Parse URL parameters def get_params():