X-Git-Url: http://git.hcoop.net/clinton/xbmc-groove.git/blobdiff_plain/2d388879d3a97d986f0b3fb86b56b8881f3016a9..3b4634dff7aea8ecaba563e8401d15a71a9c7b39:/resources/lib/GroovesharkAPI.py diff --git a/resources/lib/GroovesharkAPI.py b/resources/lib/GroovesharkAPI.py index 0cf9708..2a164df 100644 --- a/resources/lib/GroovesharkAPI.py +++ b/resources/lib/GroovesharkAPI.py @@ -15,7 +15,7 @@ # You should have received a copy of the GNU General Public License # along with xbmc-groove. If not, see . -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,18 +38,22 @@ class GrooveAPI: _sessionID = '' _userID = 0 _lastSessionTime = 0 - _lastStreamKey = '' - _lastStreamServerID = '' _key = md5.new(os.path.basename("GroovesharkAPI.py")).hexdigest() + _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) - print "Made " + self.cacheDir + if self._debugging: + print "Made " + self.cacheDir self._getSavedSession() # session ids last 2 weeks if self._sessionID == '' or time.time()- self._lastSessionTime >= SESSION_EXPIRY: @@ -57,7 +61,8 @@ class GrooveAPI: if self._sessionID == '': raise StandardError('Failed to get session id') else: - print "New GrooveAPI session id: " + self._sessionID + if self._debugging: + print "New GrooveAPI session id: " + self._sessionID self._ip = self._getIP() self._country = self._getCountry() self._setSavedSession() @@ -75,8 +80,9 @@ class GrooveAPI: req = urllib2.Request(url, postData) response = urllib2.urlopen(req) result = response.read() - print "Response..." - pprint.pprint(result) + if self._debugging: + print "Response..." + pprint.pprint(result) response.close() result = simplejson.loads(result) return result @@ -102,8 +108,9 @@ class GrooveAPI: req = urllib2.Request(url) response = urllib2.urlopen(req) result = response.read() - print "Request..." - pprint.pprint(result) + if self._debugging: + print "Request..." + pprint.pprint(result) response.close() try: result = simplejson.loads(result) @@ -153,26 +160,14 @@ class GrooveAPI: except: print "An error occurred during save session" pass - - def _setParams(self, params): - try: - # Create the directory if it doesn't exist. - if not os.path.exists(self.cacheDir): - os.makedirs(self.cacheDir) - path = os.path.join(self.cacheDir, 'params.dmp') - f = open(path, 'wb') - pickle.dump(params, f, protocol=pickle.HIGHEST_PROTOCOL) - f.close() - except: - print "An error occurred during save params" - pass # 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): - print "IP is " + myip + if self._debugging: + print "IP is " + myip return myip except: return '0.0.0.0' @@ -239,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: @@ -272,7 +265,7 @@ class GrooveAPI: # Get artists albums def getArtistAlbums(self, artistID, limit=ALBUM_LIMIT): - result = self._callRemote('getArtistAlbums', {'artistID' : artistID}) + result = self._callRemote('getArtistVerifiedAlbums', {'artistID' : artistID}) if 'result' in result: return self._parseAlbums(result, limit) else: @@ -404,32 +397,53 @@ class GrooveAPI: items = self._callRemote("getSimilarArtists", {"artistID": artistId, "limit": limit}) if 'result' in items: i = 0 - list = [] + itemList = [] artists = items['result']['artists'] while(i < len(artists)): s = artists[i] - list.append([s['artistName'].encode('ascii', 'ignore'),\ + itemList.append([s['artistName'].encode('ascii', 'ignore'),\ s['artistID']]) i = i + 1 - return list + return itemList else: return [] + def getDoesArtistExist(self, artistId): + response = self._callRemote("getDoesArtistExist", {"artistID": artistId}) + if 'result' in response and response['result'] == True: + return True + else: + return False + + def getDoesAlbumExist(self, albumId): + response = self._callRemote("getDoesAlbumExist", {"albumID": albumId}) + if 'result' in response and response['result'] == True: + return True + else: + return False + + def getDoesSongExist(self, songId): + response = self._callRemote("getDoesSongExist", {"songID": songId}) + if 'result' in response and response['result'] == True: + return True + else: + 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) - + # Extract song data def _parseSongs(self, items, limit=0): if 'result' in items: i = 0 - list = [] + itemList = [] index = '' l = -1 try: @@ -470,15 +484,19 @@ 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'] + itemList.append([name.encode('ascii', 'ignore'),\ s['SongID'],\ - s['AlbumName'].encode('ascii', 'ignore'),\ + name,\ s['AlbumID'],\ s['ArtistName'].encode('ascii', 'ignore'),\ s['ArtistID'],\ coverart]) i = i + 1 - return list + return itemList else: return [] @@ -486,14 +504,14 @@ class GrooveAPI: def _parseArtists(self, items): if 'result' in items: i = 0 - list = [] + itemList = [] artists = items['result']['artists'] while(i < len(artists)): s = artists[i] - list.append([s['ArtistName'].encode('ascii', 'ignore'),\ + itemList.append([s['ArtistName'].encode('ascii', 'ignore'),\ s['ArtistID']]) i = i + 1 - return list + return itemList else: return [] @@ -501,7 +519,7 @@ class GrooveAPI: def _parseAlbums(self, items, limit=0): if 'result' in items: i = 0 - list = [] + itemList = [] try: albums = items['result']['albums'] except: @@ -512,23 +530,27 @@ class GrooveAPI: l = limit while(i < l): s = albums[i] + if 'Name' in s: + name = s['Name'].encode('ascii', 'ignore') + else: + name = s['AlbumName'].encode('ascii', 'ignore') if 'CoverArtFilename' in s and s['CoverArtFilename'] != None: coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore') else: coverart = 'None' - list.append([s['ArtistName'].encode('ascii', 'ignore'),\ + itemList.append([s['ArtistName'].encode('ascii', 'ignore'),\ s['ArtistID'],\ - s['AlbumName'].encode('ascii', 'ignore'),\ + name,\ s['AlbumID'],\ coverart]) i = i + 1 - return list + return itemList else: return [] def _parsePlaylists(self, items): i = 0 - list = [] + itemList = [] if 'result' in items: playlists = items['result']['playlists'] elif len(items) > 0: @@ -538,6 +560,6 @@ class GrooveAPI: while (i < len(playlists)): s = playlists[i] - list.append([str(s['PlaylistName']).encode('ascii', 'ignore'), s['PlaylistID']]) + itemList.append([unicode(s['PlaylistName']).encode('utf8', 'ignore'), s['PlaylistID']]) i = i + 1 - return list + return itemList