2.0.1
[clinton/xbmc-groove.git] / resources / lib / GroovesharkAPI.py
index 64e2504..282c94f 100644 (file)
@@ -15,7 +15,7 @@
 #    You should have received a copy of the GNU General Public License
 #    along with xbmc-groove.  If not, see <http://www.gnu.org/licenses/>.
 
-import urllib2, pprint, md5, os, pickle, tempfile, time, re, simplejson, base64
+import urllib2, pprint, md5, os, pickle, tempfile, time, re, simplejson, base64, sys, socket
 from blowfish import Blowfish
 
 SESSION_EXPIRY = 1209600 # 2 weeks
@@ -38,19 +38,21 @@ class GrooveAPI:
        _sessionID = ''
        _userID = 0
        _lastSessionTime = 0
-       _lastStreamKey = ''
-       _lastStreamServerID = ''
        _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
+               if "linux" in sys.platform.lower():
+                       socket.setdefaulttimeout(30)
+                       
                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
@@ -59,7 +61,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()
@@ -78,7 +80,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()
@@ -106,7 +108,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,9 +164,9 @@ class GrooveAPI:
        # Get IP
        def _getIP(self):
                try:
-                       myip = urllib2.urlopen('http://whatismyip.org').read()
+                       myip = urllib2.urlopen('http://ipecho.net/plain').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:
@@ -232,8 +234,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,21 +430,14 @@ class GrooveAPI:
                        return False
                
        # After 30s play time
-       def markStreamKeyOver30Secs(self):
-               params = { "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID }
+       def markStreamKeyOver30Secs(self, streamKey, streamServerID):
+               params = { "streamKey" : streamKey, "streamServerID" : streamServerID }
                self._callRemote("markStreamKeyOver30Secs", params)
 
        # Song complete
-       def markSongComplete(self, songid):             
-               params = { "songID" : songid, "streamKey" : self._lastStreamKey, "streamServerID" : self._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):
-               self._apiDebug = state
-               if (self._apiDebug):
-                       print "API debug is on"
-
                
        # Extract song data     
        def _parseSongs(self, items, limit=0):
@@ -491,7 +484,11 @@ class GrooveAPI:
                                        coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
                                else:
                                        coverart = 'None'
-                               list.append([s['SongName'].encode('ascii', 'ignore'),\
+                               if 'Name' in s:
+                                       name = s['Name']
+                               else:
+                                       name = s['SongName']
+                               list.append([name.encode('ascii', 'ignore'),\
                                s['SongID'],\
                                s['AlbumName'].encode('ascii', 'ignore'),\
                                s['AlbumID'],\
@@ -559,6 +556,6 @@ class GrooveAPI:
 
                while (i < len(playlists)):
                        s = playlists[i]
-                       list.append([str(s['PlaylistName']).encode('ascii', 'ignore'), s['PlaylistID']])
+                       list.append([unicode(s['PlaylistName']).encode('utf8', 'ignore'), s['PlaylistID']])
                        i = i + 1
                return list