def nowplaying(self, artist, track, album, length): if not self.session: return True if self.debug: print "audioscrobbler: now playing " + artist + " - " + track artist = self.urlencode(artist) track = self.urlencode(track) album = self.urlencode(album) req = "s=" + self.session + "&a=" + artist + "&t=" + track + "&b=" + album + "&l=" + str(length) + "&m=" s = httpclient.httpclient(self.nowplayingurl) s.post(req) reslines = string.split(s.response, "\n") if reslines[0] != "OK": print "audioscrobbler: Now playing error:" print repr(s.response) if reslines[0] == "BADSESSION": self.session = None return True return False
def submit(self, artist, track, album, starttime, rating, length, key): if not self.session: return True if self.debug: print "audioscrobbler: submitting " + artist + " - " + track artist = self.urlencode(artist) track = self.urlencode(track) album = self.urlencode(album) req = "s=" + self.session + "&a[0]=" + artist + "&t[0]=" + track + "&b[0]=" + album + "&r[0]=" + rating + "&i[0]=" + str(starttime) + "&l[0]=" + str(length) + "&o[0]=L" + key + "&n[0]=&m[0]=" s = httpclient.httpclient(self.submiturl) s.post(req) reslines = string.split(s.response, "\n") if reslines[0] != "OK": print "audioscrobbler: Submission error:" print repr(s.response) if reslines[0] == "BADSESSION": self.session = None self.queue.append( (artist,track,album,starttime,rating,length,key) ) f = open("scrobblerqueue.txt", "w") f.write(repr(self.queue)) f.close() return True return False
def getplaylist(self): if self.debug: sys.stderr.write("Fetching playlist...\n") s = httpclient.httpclient(self.info["base_url"]) s.req(self.info["base_path"] + "/xspf.php?sk=" + self.info["session"] + "&discovery=0&desktop=" + self.version) self.playlist.parse(s.response) # debug if self.debug: sys.stderr.write("Saving playlist...\n") if len(self.playlist.data.tracks): f = open("playlist.xspf", "w") f.write(s.response) f.close() elif False: print "No playlist?? Using cached version instead..." f = open("playlist.xspf", "r") cache = f.read() f.close() self.playlist.parse(cache) self.playlist.pos = 0 #len(self.playlist.data.tracks) -1 return len(self.playlist.data.tracks)
def handshake(self, username, password, version): self.username = username self.password = password timestamp = str(int(time.time())) auth = self.hexify(md5.md5(password).digest()) auth = self.hexify(md5.md5(auth + timestamp).digest()) req = "/?hs=true&p=1.2&c=" + self.clientid + "&v=" + self.clientversion + "&u=" + username + "&t=" + timestamp + "&a=" + auth s = httpclient.httpclient(self.handshakeurl) s.req(req) reslines = string.split(s.response, "\n") if self.debug: print "audioscrobbler: handshake " + reslines[0] if reslines[0] != "OK": print "audioscrobbler: Handshake error:" print repr(s.response) return True self.session = reslines[1] self.nowplayingurl = reslines[2] self.submiturl = reslines[3] return False
def wsSession(self, token): s = httpclient.httpclient(self.host, self.port) params = "api_key="+self.api_key+"&method=auth.getSession&token="+token p = "api_key"+self.api_key+"methodauth.getSessiontoken"+token if self.debug: sys.stderr.write(params+"\n") print params #no write request -> can use url-style url = "http://ws.audioscrobbler.com/2.0/?"+params+"&api_sig="+self.sig(p) if self.debug: sys.stderr.write(url+"\n") print url s.req(url) resp = s.response if self.debug: sys.stderr.write(resp+"\n") print resp res = self.parselinesXml(resp) sessionKey = res["key"] if self.debug: print "sessionKey is "+sessionKey self.sessionKey = sessionKey self.s = s return
def connect(self, username, password): s = httpclient.httpclient(self.host, self.port) s.req("/radio/handshake.php?version=" + self.version + "&platform=" + self.platform + "&username="******"&passwordmd5=" + password + "&language=en&api_key=" + self.api_key + "&player=" + self.player) self.info = self.parselines(s.response) # if the file does not exist, this is the first usage -> auth for web service needed from user # if os.path.isfile(os.path.join(".", "ws-auth.txt")) == False: print "starting web service auth" s = httpclient.httpclient(self.host, self.port) # s.req("http://www.last.fm/api/auth?api_key="+self.api_key) url = "http://www.last.fm/api/auth?api_key="+self.api_key print "Please point your browser to the following url for authentication: " print " "+url webbrowser.open_new(url)
def changestation(self, url): if self.debug: sys.stderr.write("changestation " + url + "\n") s = httpclient.httpclient(self.info["base_url"], 80) s.req(self.info["base_path"] + "/adjust.php?session=" + self.info["session"] + "&url=" + url) res = self.parselines(s.response) if not res.has_key("response") or res["response"] != "OK": sys.stderr.write("changestation " + url + " returned:" + repr(res) + "\n") return res
def command(self, cmd): # commands = skip, love, ban, rtp, nortp if self.debug: sys.stderr.write("command " + cmd + "\n") s = httpclient.httpclient(self.info["base_url"], 80) s.req(self.info["base_path"] + "/control.php?command=" + cmd + "&session=" + self.info["session"]) res = self.parselines(s.response) if not res.has_key("response") or res["response"] != "OK": sys.stderr.write("command " + cmd + " returned:" + repr(res) + "\n") return res
def connect(self, username, password): s = httpclient.httpclient(self.host, self.port) s.req("/radio/handshake.php?version=" + self.version + "&platform=" + self.platform + "&username="******"&passwordmd5=" + password + "&language=en&api_key=" + self.api_key + "&player=" + self.player) self.info = self.parselines(s.response)
def command(self, cmd): # commands = skip, love, ban, rtp, nortp # track.love or track.ban # track, artist, api_key, api_sig, sessionKey (sk) if cmd == "ban" or cmd == "love": if self.debug: sys.stderr.write("Banning or Loving...\n") print "ban or love special" if (self.sessionKey == None): sys.stderr.write("NO SESSION KEY\n") print "NO SESSION KEY" return {} m = self.playlist.data.tracks[self.playlist.pos] artist = m["creator"] track = m["title"] artist = self.utf8(artist) track = self.utf8(track) if self.debug: sys.stderr.write("artist..:"+artist+"\n") sys.stderr.write("track...:"+track+"\n") print "artist..: "+artist print "track...: "+track # re-use httpclient s = self.s method = "" if cmd == "ban": method = "track.ban" elif cmd == "love": method = "track.love" else: print "unknown method, shouldn't happen." # all params except api_sig ordered alphabetically # for the postdata request params = "api_key="+self.api_key+"&artist="+artist+"&method="+method+"&sk="+self.sessionKey+"&track="+track # for the signature without = and ? p = "api_key"+self.api_key+"artist"+artist+"method"+method+"sk"+self.sessionKey + "track"+track if self.debug: sys.stderr.write("params are: "+params+"\n") print params # this is a write request -> use postdata url = "http://ws.audioscrobbler.com/2.0/" postdata = params+"&api_sig="+self.sig(p) if self.debug: sys.stderr.write("url: "+url+"\n") sys.stderr.write("postdata: "+postdata+"\n") print url print postdata s.req(url, postdata) resp = s.response if self.debug: sys.stderr.write("response: "+resp+"\n") print resp res = self.parselinesXml(resp) return res if self.debug: sys.stderr.write("command " + cmd + "\n") s = httpclient.httpclient(self.info["base_url"], 80) s.req(self.info["base_path"] + "/control.php?command=" + cmd + "&session=" + self.info["session"]) res = self.parselines(s.response) if not res.has_key("response") or res["response"] != "OK": sys.stderr.write("command " + cmd + " returned:" + repr(res) + "\n") return res
#!/usr/bin/python import sys import config import httpclient s = httpclient.httpclient("localhost", config.listenport) s.req("/" + sys.argv[1])