X-Git-Url: http://git.hcoop.net/clinton/xbmc-groove.git/blobdiff_plain/5b941088cc1006fa8aa2765300d9dd0e228d663b..5e72534b4750dd1670b8e2940d408eef46a5fe4e:/resources/lib/GroovesharkAPI.py diff --git a/resources/lib/GroovesharkAPI.py b/resources/lib/GroovesharkAPI.py index 43db1ba..767c0fb 100644 --- a/resources/lib/GroovesharkAPI.py +++ b/resources/lib/GroovesharkAPI.py @@ -1,4 +1,21 @@ -import socket, urllib, urllib2, pprint, md5, os, pickle, tempfile, time, re, simplejson, base64 +# Copyright 2011 Stephen Denham + +# This file is part of xbmc-groove. +# +# xbmc-groove is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# xbmc-groove is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# 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 from blowfish import Blowfish SESSION_EXPIRY = 1209600 # 2 weeks @@ -24,16 +41,17 @@ class GrooveAPI: _lastStreamKey = '' _lastStreamServerID = '' _key = md5.new(os.path.basename("GroovesharkAPI.py")).hexdigest() + _apiDebug = False # Constructor def __init__(self): self.simplejson = simplejson - socket.setdefaulttimeout(40) 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._apiDebug: + print "Made " + self.cacheDir self._getSavedSession() # session ids last 2 weeks if self._sessionID == '' or time.time()- self._lastSessionTime >= SESSION_EXPIRY: @@ -41,7 +59,8 @@ class GrooveAPI: if self._sessionID == '': raise StandardError('Failed to get session id') else: - print "New GrooveAPI session id: " + self._sessionID + if self._apiDebug: + print "New GrooveAPI session id: " + self._sessionID self._ip = self._getIP() self._country = self._getCountry() self._setSavedSession() @@ -59,8 +78,9 @@ class GrooveAPI: req = urllib2.Request(url, postData) response = urllib2.urlopen(req) result = response.read() - print "Response..." - pprint.pprint(result) + if self._apiDebug: + print "Response..." + pprint.pprint(result) response.close() result = simplejson.loads(result) return result @@ -86,8 +106,9 @@ class GrooveAPI: req = urllib2.Request(url) response = urllib2.urlopen(req) result = response.read() - print "Request..." - pprint.pprint(result) + if self._apiDebug: + print "Request..." + pprint.pprint(result) response.close() try: result = simplejson.loads(result) @@ -156,7 +177,8 @@ 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): - print "IP is " + myip + if self._apiDebug: + print "IP is " + myip return myip except: return '0.0.0.0' @@ -256,7 +278,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: @@ -399,6 +421,27 @@ class GrooveAPI: 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 } @@ -409,6 +452,13 @@ class GrooveAPI: params = { "songID" : songid, "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID } 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): if 'result' in items: