Fixes and tidy up.
[clinton/xbmc-groove.git] / default.py
index 8f68fa5..5dd94c2 100644 (file)
@@ -68,7 +68,7 @@ STREAM_MARKING_TIME = 30
 STREAM_TIMEOUT = 30
 
 # Retry URL
 STREAM_TIMEOUT = 30
 
 # Retry URL
-STREAM_RETRY = STREAM_TIMEOUT/2
+STREAM_RETRY = 15
 
 songMarkTime = 0
 player = xbmc.Player()
 
 songMarkTime = 0
 player = xbmc.Player()
@@ -107,19 +107,19 @@ except:
     sys.exit(-1)
   
 # Mark song as playing or played
     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:
     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
             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
     else:
         playTimer.cancel()
         songMarkTime = 0
@@ -489,8 +489,9 @@ class Grooveshark:
                         if playTimer != None:
                             playTimer.cancel()
                             songMarkTime = 0
                         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
                         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
                 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)
                     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 + ')')
         
         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)
         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__:
         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]
                 if song[0] == id:
                     duration = song[1]
                     url = song[2]
+                    streamKey = song[3]
+                    streamServerID = song[4]
                     break;
             f.close()
         except:
                     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)
         # Not in cache but exists
         if duration < 0 and groovesharkApi.getDoesSongExist(songid):
             stream = groovesharkApi.getSubscriberStreamKey(songid)
-            url = stream['url']
             usecs = stream['uSecs']
             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)
 
             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:
         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()
                     self._setSongStreams(streams)
                     break;
             f.close()
@@ -973,6 +982,10 @@ class Grooveshark:
             xbmc.log("An error occurred saving streams")
             pass
 
             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():
     
 # Parse URL parameters
 def get_params():