Bug fixes.
authorstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Mon, 3 Oct 2011 17:50:06 +0000 (17:50 +0000)
committerstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Mon, 3 Oct 2011 17:50:06 +0000 (17:50 +0000)
git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@64 2dec19e3-eb1d-4749-8193-008c8bba0994

default.py
resources/lib/GroovesharkAPI.py

index 4e898ca..a98dc09 100644 (file)
@@ -24,7 +24,7 @@ __cwd__       = __addon__.getAddonInfo('path')
 __author__    = __addon__.getAddonInfo('author')
 __version__   = __addon__.getAddonInfo('version')
 __language__  = __addon__.getLocalizedString
-__debuggui__  = __addon__.getSetting('debug')
+__debugging__  = __addon__.getSetting('debug')
 
 MODE_SEARCH_SONGS = 1
 MODE_SEARCH_ALBUMS = 2
@@ -67,9 +67,6 @@ STREAM_MARKING_TIME = 30
 # Timeout
 STREAM_TIMEOUT = 30
 
-# Retry URL
-STREAM_RETRY = 15
-
 songMarkTime = 0
 player = xbmc.Player()
 playTimer = None
@@ -96,9 +93,13 @@ sys.path.append (libDir)
 from GroovesharkAPI import GrooveAPI
 from threading import Event, Thread
 
+if __debugging__ == 'true':
+    __debugging__ = True
+else:
+    __debugging__ = False
+
 try:
-    groovesharkApi = GrooveAPI()
-    groovesharkApi.setDebug(__debuggui__)
+    groovesharkApi = GrooveAPI(__debugging__)
     if groovesharkApi.pingService() != True:
         raise StandardError(__language__(30007))
 except:
@@ -255,12 +256,12 @@ class Grooveshark:
         self._handle = int(sys.argv[1])
         if os.path.isdir(cacheDir) == False:
             os.makedirs(cacheDir)
-            if __debuggui__ == True:
+            if __debugging__ :
                 xbmc.log(__language__(30012) + " " + cacheDir)
         artDir = xbmc.translatePath(thumbDir)
         if os.path.isdir(artDir) == False:
             os.makedirs(artDir)
-            if __debuggui__ == True:
+            if __debugging__ :
                 xbmc.log(__language__(30012) + " " + artDir)
             
     # Top-level menu
@@ -350,7 +351,7 @@ class Grooveshark:
             if (len(artists) > 0):
                 artist = artists[0]
                 artistID = artist[1]
-                if __debuggui__ == True:
+                if __debugging__ :
                     xbmc.log("Found " + artist[0] + "...")
                 albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit)
                 if (len(albums) > 0):
@@ -407,7 +408,7 @@ class Grooveshark:
     def favorite(self, songid):
         userid = self._get_login()
         if (userid != 0):
-            if __debuggui__ == True:
+            if __debugging__ :
                 xbmc.log("Favorite song: " + str(songid))
             groovesharkApi.addUserFavoriteSong(songID = songid)
             xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30036) + ', 1000, ' + thumbDef + ')')
@@ -419,7 +420,7 @@ class Grooveshark:
     def unfavorite(self, songid, prevMode=0):
         userid = self._get_login()
         if (userid != 0):
-            if __debuggui__ == True:
+            if __debugging__ :
                 xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode))
             groovesharkApi.removeUserFavoriteSongs(songIDs = songid)
             xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30038) + ', 1000, ' + thumbDef + ')')
@@ -459,7 +460,7 @@ class Grooveshark:
             if (len(artists) > 0):
                 artist = artists[0]
                 artistID = artist[1]
-                if __debuggui__ == True:
+                if __debugging__ :
                     xbmc.log("Found " + artist[0] + "...")
                 songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit)
                 if (len(songs) > 0):
@@ -480,16 +481,19 @@ class Grooveshark:
         global playTimer
         global player
         if item != None:
-            songid = item.getProperty('songid')
-            duration = self._getSongDuration(songid)
             url = ''
+            songid = item.getProperty('songid')
+            duration = int(self._getSongDuration(songid))
             stream = groovesharkApi.getSubscriberStreamKey(songid)
             if stream != False:
                 url = stream['url']
+                key = stream['StreamKey']
+                server = stream['StreamServerID']
+                duration = self._setDuration(stream['uSecs'])
             if url != '':
                 item.setPath(url)
                 xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
-                if __debuggui__ == True:
+                if __debugging__ :
                     xbmc.log("Grooveshark playing: " + url)
                 # Wait for play then start timer
                 seconds = 0
@@ -499,7 +503,7 @@ class Grooveshark:
                             if playTimer != None:
                                 playTimer.cancel()
                                 songMarkTime = 0
-                            playTimer = PlayTimer(1, markSong, duration, [songid, duration, stream['streamKey'], stream['streamServerID']])
+                            playTimer = PlayTimer(1, markSong, duration, [songid, duration, key, server])
                             playTimer.start()
                             break
                     except: pass
@@ -618,7 +622,7 @@ class Grooveshark:
                     else:
                         playlist = playlists[i]
                         playlistid = playlist[1]
-                        if __debuggui__ == True:
+                        if __debugging__ :
                             xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid))
                         songIDs=[]
                         songs = groovesharkApi.getPlaylistSongs(playlistid)
@@ -729,7 +733,7 @@ class Grooveshark:
 
         # No pages needed
         if offset == 0 and totalSongs <= self.songspagelimit:
-            if __debuggui__ == True:
+            if __debugging__ :
                 xbmc.log("Found " + str(totalSongs) + " songs...")
         # Pages
         else:
@@ -780,7 +784,7 @@ class Grooveshark:
                 id = id + 1
             else:
                 end = min(end + 1,totalSongs)
-                if __debuggui__ == True:
+                if __debugging__ :
                     xbmc.log(song[0] + " does not exist.")
             n = n + 1
 
@@ -795,7 +799,7 @@ class Grooveshark:
     def _add_albums_directory(self, albums, artistid=0):
         n = len(albums)
         itemsExisting = n
-        if __debuggui__ == True:
+        if __debugging__ :
             xbmc.log("Found " + str(n) + " albums...")
         i = 0
         while i < n:
@@ -820,7 +824,7 @@ class Grooveshark:
     def _add_artists_directory(self, artists):
         n = len(artists)
         itemsExisting = n
-        if __debuggui__ == True:
+        if __debugging__ :
             xbmc.log("Found " + str(n) + " artists...")
         i = 0
         while i < n:
@@ -839,7 +843,7 @@ class Grooveshark:
     # Add playlists to directory          
     def _add_playlists_directory(self, playlists):
         n = len(playlists)
-        if __debuggui__ == True:
+        if __debugging__ :
             xbmc.log("Found " + str(n) + " playlists...")
         i = 0
         while i < n:
@@ -920,17 +924,15 @@ class Grooveshark:
         # Not in cache but exists
         if duration < 0 and groovesharkApi.getDoesSongExist(songid):
             stream = groovesharkApi.getSubscriberStreamKey(songid)
-            usecs = stream['uSecs']
-            
-            duration = self._setDuration(usecs)
+            duration = self._setDuration(stream['uSecs'])
             song = [id, duration]
-            durations.append(song)                
-            self._setSongDuration(durations)
+            self._setSongDuration(song, durations)
 
         return duration
         
-    def _setSongDuration(self, durations):            
+    def _setSongDuration(self, song, durations):            
         try:
+            durations.append(song)                
             # Create the cache directory if it doesn't exist.
             if not os.path.exists(cacheDir):
                 os.makedirs(cacheDir)
@@ -942,6 +944,7 @@ class Grooveshark:
             xbmc.log("An error occurred saving duration")
             pass
 
+    # Duration to seconds
     def _setDuration(self, usecs):
         if usecs < 60000000:
             usecs = usecs * 10 # Some durations are 10x to small
@@ -951,7 +954,7 @@ class Grooveshark:
 def get_params():
     param=[]
     paramstring=sys.argv[2]
-    if __debuggui__ == True:
+    if __debugging__ :
         xbmc.log(paramstring)
     if len(paramstring)>=2:
         params=sys.argv[2]
index 7790f17..a011276 100644 (file)
@@ -39,16 +39,17 @@ class GrooveAPI:
        _userID = 0
        _lastSessionTime = 0
        _key = md5.new(os.path.basename("GroovesharkAPI.py")).hexdigest()
-       _apiDebug = False
+       _debugging = False
 
        # Constructor
-       def __init__(self):
+       def __init__(self, debug):
                
+               self._debugging = debug
                self.simplejson = simplejson
                self.cacheDir = os.path.join(tempfile.gettempdir(), 'groovesharkapi')
                if os.path.isdir(self.cacheDir) == False:
                        os.makedirs(self.cacheDir)
-                       if self._apiDebug:
+                       if self._debugging:
                                print "Made " + self.cacheDir
                self._getSavedSession()
                # session ids last 2 weeks
@@ -57,7 +58,7 @@ class GrooveAPI:
                        if self._sessionID == '':
                                raise StandardError('Failed to get session id')
                        else:
-                               if self._apiDebug:
+                               if self._debugging:
                                        print "New GrooveAPI session id: " + self._sessionID
                                self._ip = self._getIP()
                                self._country = self._getCountry()
@@ -76,7 +77,7 @@ class GrooveAPI:
                        req = urllib2.Request(url, postData)
                        response = urllib2.urlopen(req)
                        result = response.read()
-                       if self._apiDebug:
+                       if self._debugging:
                                print "Response..."
                                pprint.pprint(result)
                        response.close()
@@ -104,7 +105,7 @@ class GrooveAPI:
                req = urllib2.Request(url)
                response = urllib2.urlopen(req)
                result = response.read()
-               if self._apiDebug:
+               if self._debugging:
                        print "Request..."
                        pprint.pprint(result)
                response.close()
@@ -162,7 +163,7 @@ class GrooveAPI:
                try:
                        myip = urllib2.urlopen('http://whatismyip.org').read()
                        if re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", myip):
-                               if self._apiDebug:
+                               if self._debugging:
                                        print "IP is " + myip
                                return myip
                except:
@@ -426,23 +427,14 @@ class GrooveAPI:
                        return False
                
        # After 30s play time
-       def markStreamKeyOver30Secs(self, lastStreamKey, lastStreamServerID):
-               params = { "streamKey" : lastStreamKey, "streamServerID" : lastStreamServerID }
+       def markStreamKeyOver30Secs(self, streamKey, streamServerID):
+               params = { "streamKey" : streamKey, "streamServerID" : streamServerID }
                self._callRemote("markStreamKeyOver30Secs", params)
 
        # Song complete
-       def markSongComplete(self, songid, lastStreamKey, lastStreamServerID):          
-               params = { "songID" : songid, "streamKey" : lastStreamKey, "streamServerID" : lastStreamServerID }
+       def markSongComplete(self, songid, streamKey, streamServerID):          
+               params = { "songID" : songid, "streamKey" : streamKey, "streamServerID" : streamServerID }
                self._callRemote("markSongComplete", params)
-
-       # Debug on off
-       def setDebug(self, state):
-               if state == True:
-                       self._apiDebug = True
-               else:
-                       self._apiDebug = False
-               if self._apiDebug == True:
-                       print "API debug is on"
                
        # Extract song data     
        def _parseSongs(self, items, limit=0):