Cache streams.
authorstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Sun, 19 Aug 2012 09:10:42 +0000 (09:10 +0000)
committerstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Sun, 19 Aug 2012 09:10:42 +0000 (09:10 +0000)
git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@78 2dec19e3-eb1d-4749-8193-008c8bba0994

default.py

index 9d56556..099f21a 100644 (file)
@@ -938,7 +938,7 @@ class Grooveshark:
                     server = song[4]
                     stream = [id, duration, url, key, server]
                     if __debugging__ :
-                        xbmc.log("Found " + str(id) + " in cache")
+                        xbmc.log("Found " + str(id) + " in stream cache")
                     break;
             f.close()
         except:
@@ -953,45 +953,55 @@ class Grooveshark:
                 key = stream['StreamKey']
                 server = stream['StreamServerID']
                 stream = [id, duration, url, key, server]
-                self._addSongStream(stream, streams)
+                self._addSongStream(stream)
 
         return stream
         
-    def _addSongStream(self, stream, streams):            
+    def _addSongStream(self, stream):
+        streams = self._getStreams()           
+        streams.append(stream)                
+        path = os.path.join(cacheDir, 'streams.dmp')
         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()
+            if __debugging__ :
+                xbmc.log("Added " + str(stream[0]) + " to stream cache")
         except:
-            xbmc.log("An error occurred adding stream")
-            pass
+            xbmc.log("An error occurred adding to stream")
     
     def _setSongStream(self, stream):
         id = int(stream[0])
         stream[1] = self._setDuration(stream[1])
-        streams = []
+        streams = self._getStreams()
         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")
+        i = 0
+
+        for song in streams:
+            if song[0] == id:
+                streams[i] = stream
+                try:
+                    f = open(path, 'wb')
                     pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
+                    f.close()
+                    if __debugging__ :
+                        xbmc.log("Updated " + str(id) + " in stream cache")
                     break;
-                i = i + 1
+                except:
+                    xbmc.log("An error occurred setting stream")                    
+            i = i + 1
+    
+    def _getStreams(self):
+        path = os.path.join(cacheDir, 'streams.dmp')
+        try:
+            f = open(path, 'rb')
+            streams = pickle.load(f)
             f.close()
         except:
-            xbmc.log("An error occurred setting stream")
-    
+            streams = []
+            pass
+        return streams
+
     
 # Parse URL parameters
 def get_params():