+
+ # Duration to seconds
+ def _setDuration(self, usecs):
+ if usecs < 60000000:
+ usecs = usecs * 10 # Some durations are 10x to small
+ return int(usecs / 1000000)
+
+ def _getSongStream(self, songid):
+ idSong = int(songid)
+ stream = None
+ streams = {}
+ path = os.path.join(cacheDir, 'streams.dmp')
+ try:
+ f = open(path, 'rb')
+ streams = pickle.load(f)
+ song = streams[songid]
+
+ duration = song[1]
+ url = song[2]
+ key = song[3]
+ server = song[4]
+ stream = [idSong, duration, url, key, server]
+
+ if __debugging__ :
+ xbmc.log("Found " + str(idSong) + " in stream cache")
+
+ 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 = [idSong, duration, url, key, server]
+ self._addSongStream(stream)
+
+ return stream
+
+ def _addSongStream(self, stream):
+ streams = self._getStreams()
+ streams[int(stream[0])] = stream
+ path = os.path.join(cacheDir, 'streams.dmp')
+ try:
+ f = open(path, 'wb')
+ pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
+ f.close()
+ if __debugging__ :
+ xbmc.log("Added " + str(stream[0]) + " to stream cache")
+ except:
+ xbmc.log("An error occurred adding to stream")
+
+ def _setSongStream(self, stream):
+ idStream = int(stream[0])
+ stream[1] = self._setDuration(stream[1])
+ streams = self._getStreams()
+ path = os.path.join(cacheDir, 'streams.dmp')
+
+ try:
+ streams[idStream] = stream
+ f = open(path, 'wb')
+ pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
+ f.close()
+ if __debugging__ :
+ xbmc.log("Updated " + str(idStream) + " in stream cache")
+
+ except:
+ xbmc.log("An error occurred setting stream")
+
+ def _getStreams(self):
+ path = os.path.join(cacheDir, 'streams.dmp')
+ try:
+ f = open(path, 'rb')
+ streams = pickle.load(f)
+ f.close()
+ except:
+ streams = {}
+ pass
+ return streams
+