Fixes and tidy up.
authorstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Sun, 2 Oct 2011 16:35:41 +0000 (16:35 +0000)
committerstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Sun, 2 Oct 2011 16:35:41 +0000 (16:35 +0000)
git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@62 2dec19e3-eb1d-4749-8193-008c8bba0994

default.py
resources/lib/GroovesharkAPI.py

index 8f68fa5..5dd94c2 100644 (file)
@@ -68,7 +68,7 @@ STREAM_MARKING_TIME = 30
 STREAM_TIMEOUT = 30
 
 # Retry URL
-STREAM_RETRY = STREAM_TIMEOUT/2
+STREAM_RETRY = 15
 
 songMarkTime = 0
 player = xbmc.Player()
@@ -107,19 +107,19 @@ except:
     sys.exit(-1)
   
 # Mark song as playing or played
-def markSong(songid, duration):
+def markSong(songid, duration, streamKey, streamServerID):
     global songMarkTime
     global playTimer
     global player
     if player.isPlayingAudio():
         tNow = player.getTime()
         if tNow >= STREAM_MARKING_TIME and songMarkTime == 0:
-            groovesharkApi.markStreamKeyOver30Secs()
+            groovesharkApi.markStreamKeyOver30Secs(streamKey, streamServerID)
             songMarkTime = tNow
         elif duration > tNow and duration - tNow < 2 and songMarkTime >= STREAM_MARKING_TIME:
             playTimer.cancel()
             songMarkTime = 0
-            groovesharkApi.markSongComplete(songid)
+            groovesharkApi.markSongComplete(songid, streamKey, streamServerID)
     else:
         playTimer.cancel()
         songMarkTime = 0
@@ -489,8 +489,9 @@ class Grooveshark:
                         if playTimer != None:
                             playTimer.cancel()
                             songMarkTime = 0
-                        duration = int(item.getProperty('duration'))
-                        playTimer = PlayTimer(1, markSong, duration, [songid, duration])
+                        stream = self._getSongStream(songid)
+                        duration = stream['duration']
+                        playTimer = PlayTimer(1, markSong, duration, [songid, duration, stream['streamKey'], stream['streamServerID']])
                         playTimer.start()
                         break
                 except: pass
@@ -498,10 +499,11 @@ class Grooveshark:
                 seconds = seconds + 1
  
                 # If not playing after a few seconds try to refresh the URL
-                if (seconds == STREAM_RETRY):
+                if (player.isPlayingAudio() == False and seconds == STREAM_RETRY):
+                    if __debug__:
+                        xbmc.log("Refreshing URL")
                     item.setProperty('url', '')
                     self._setItem(item)
-                    self._setSongStream(songid, item.getProperty('duration'), item.getProperty('url'))
         else:
             xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30044) + ', 1000, ' + thumbDef + ')')
         
@@ -723,7 +725,9 @@ class Grooveshark:
         songid = item.getProperty('songid')
         if url == '':
             stream = groovesharkApi.getSubscriberStreamKey(songid)
-            url = stream['url']
+            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__:
@@ -925,6 +929,8 @@ class Grooveshark:
                 if song[0] == id:
                     duration = song[1]
                     url = song[2]
+                    streamKey = song[3]
+                    streamServerID = song[4]
                     break;
             f.close()
         except:
@@ -933,26 +939,29 @@ class Grooveshark:
         # Not in cache but exists
         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, url]
+            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}
+        return {'duration':duration, 'url':url, 'streamKey':streamKey, 'streamServerID':streamServerID}
 
-    def _setSongStream(self, songid, duration, url):            
+    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] = duration
-                    song[2] = url
+                    song[1] = self._setDuration(stream['uSecs'])
+                    song[2] = stream['url']
+                    song[3] = stream['streamKey']
+                    song[4] = stream['streamServerID']
                     self._setSongStreams(streams)
                     break;
             f.close()
@@ -973,6 +982,10 @@ class Grooveshark:
             xbmc.log("An error occurred saving streams")
             pass
 
+    def _setDuration(self, usecs):
+        if usecs < 60000000:
+            usecs = usecs * 10 # Some durations are 10x to small
+        return usecs / 1000000
     
 # Parse URL parameters
 def get_params():
index 64e2504..232f849 100644 (file)
@@ -38,8 +38,6 @@ class GrooveAPI:
        _sessionID = ''
        _userID = 0
        _lastSessionTime = 0
-       _lastStreamKey = ''
-       _lastStreamServerID = ''
        _key = md5.new(os.path.basename("GroovesharkAPI.py")).hexdigest()
        _apiDebug = False
 
@@ -232,8 +230,6 @@ class GrooveAPI:
                params = { "songID": songID, "country": self._country }
                response = self._callRemote("getSubscriberStreamKey", params)
                try: 
-                       self._lastStreamKey = response["result"]["StreamKey"]
-                       self._lastStreamServerID = response["result"]["StreamServerID"]
                        res = response["result"]
                        return res
                except:
@@ -430,13 +426,13 @@ class GrooveAPI:
                        return False
                
        # After 30s play time
-       def markStreamKeyOver30Secs(self):
-               params = { "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID }
+       def markStreamKeyOver30Secs(self, lastStreamKey, lastStreamServerID):
+               params = { "streamKey" : lastStreamKey, "streamServerID" : lastStreamServerID }
                self._callRemote("markStreamKeyOver30Secs", params)
 
        # Song complete
-       def markSongComplete(self, songid):             
-               params = { "songID" : songid, "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID }
+       def markSongComplete(self, songid, lastStreamKey, lastStreamServerID):          
+               params = { "songID" : songid, "streamKey" : lastStreamKey, "streamServerID" : lastStreamServerID }
                self._callRemote("markSongComplete", params)
 
        # Debug on off