def _make_session(token): sess = _do_lastfm_query("GET", "auth.getSession", token=token) key = sess["session"][0]["key"][0] fp = open(os.path.expanduser("~/.lastfmsess"), "w") fp.write(key) fp.close() print "session successfully created. thank you."
def identify_audio(filename): """ Identify a given song. You can provide a filename of some audio. You must provide start time to end time for scanning. """ cmd = '{} "{}" > song.out'.format(EP_PATH, filename) os.system(cmd) with open('song.out', 'rb') as fp: data = fp.read() fp.close() data = json.loads(data.strip())[0] print json.dumps(data, indent=3) fp_code = data["code"] cmd = 'curl -S http://localhost:8080/query?fp_code={} > song.out'.format(fp_code) os.system(cmd) with open('song.out', 'rb') as fp: data = fp.read() fp.close() data = json.loads(data.strip()) print json.dumps(data, indent=3) if data["match"]: print "SUCCESSFUL MATCH!" title = songMap[str(data["track_id"])] print "Matched to: {}".format(title) return { "success": True, "match": title, "score": data["score"] }; else: return { "success": False };
def read_map(): global songMap try: with open(MAP_FNAME, 'rb') as fp: songMap = pickle.load(fp) fp.close() except IOError: songMap = {} pass
def read_id(): global lastId try: with open(ID_FNAME, 'rb') as fp: lastId = fp.read() fp.close() except IOError: lastId = "songid1" pass
def do_GET(self): print self.path if self.path == '/identify': self.send_response(200) self.send_header('Content-type', 'text/json') self.end_headers() self.wfile.write(json.dumps(identify_audio("file.mp3"))) elif self.path == '/learn': self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() with open("learn.title", "rb") as fp: title = fp.read() fp.close() learn_song("file.mp3", title) self.wfile.write('OK')
def main(file): statusfile = os.path.expanduser("~/.scrobbyl") lines = [] if os.path.exists(statusfile): fp = open(statusfile, "r") lines = fp.readlines() fp.close() lasttime = 0 lastartist = "" lasttrack = "" if len(lines) == 3: lasttime = int(lines[0]) lastartist = lines[1] lasttrack = lines[2] f = fingerprint(file) code = f[0]["code"] song = echonest.fp_lookup(code) echonest.pp(song) if "response" in song and "status" in song["response"] \ and song["response"]["status"]["message"] == "Success" \ and len(song["response"]["songs"]) > 0: track = song["response"]["songs"][0]["title"] artist = song["response"]["songs"][0]["artist_name"] now = time.time() print(now - lasttime) if now - lasttime < 100: # Only scrobble if we've just been playing if lasttrack != "" and lasttrack != track: print "Last track was", lasttrack, "now", track, ", scrobbling" else: print "same song" else: print "too long since we last did it,", now - lasttime fp = open(statusfile, "w") fp.write("%d\n%s\n%s" % (now, artist, track)) fp.close()
def main(file): statusfile = os.path.expanduser("~/.scrobbyl") lines = [] if os.path.exists(statusfile): fp = open(statusfile, "r") lines = fp.readlines() fp.close() lasttime = 0 lastartist = "" lasttrack = "" if len(lines) == 3: lasttime = int(lines[0]) lastartist = lines[1] lasttrack = lines[2] f = fingerprint(file) code = f[0]["code"] song = echonest.fp_lookup(code) echonest.pp(song) if "response" in song and "status" in song["response"] \ and song["response"]["status"]["message"] == "Success" \ and len(song["response"]["songs"]) > 0: track = song["response"]["songs"][0]["title"] artist = song["response"]["songs"][0]["artist_name"] now = time.time() print (now-lasttime) if now - lasttime < 100: # Only scrobble if we've just been playing if lasttrack != "" and lasttrack != track: print "Last track was",lasttrack,"now",track,", scrobbling" else: print "same song" else: print "too long since we last did it,", now-lasttime fp = open(statusfile, "w") fp.write("%d\n%s\n%s" % (now, artist, track)) fp.close()
def learn_song(filename, description): global lastId cmd = '{} "{}" > song.out'.format(EP_PATH, filename) with open('song.out', 'rb') as fp: subprocess.call(cmd, shell=True, stdout=fp) os.system(cmd) data = fp.read() fp.close() data = json.loads(data.strip())[0] #print json.dumps(data, indent=3) try: fp_code = data["code"] except KeyError: print "Could not learn song with title: {}".format(description) return length = data["metadata"]["duration"] cmd = 'curl http://localhost:8080/ingest -d "fp_code={}&track_id={}&length={}&codever=4.12" > /dev/null'.format(fp_code, lastId, length) songMap[lastId] = description lastId = 'songid' + str(int(lastId.split('songid')[1]) + 1) save_id() save_map() os.system(cmd) print "Learned song with title: {}".format(description)
def save_map(): with open(MAP_FNAME, 'wb') as fp: pickle.dump(songMap, fp) fp.close()
def save_id(): with open(ID_FNAME, 'wb') as fp: fp.write(lastId) fp.close()