def isCommercial(artist, track): # search works differently with -, but it can appear in track, maybe also # in artist terms = (u'"%s" "%s"' % (artist, track)) #terms = terms.replace(u'-', u' ') #terms = terms.replace(u':', u' ') # TODO: use unaccent lib #terms = terms.replace(u' – ', ' ') done = False print terms while not done: try: search = spotimeta.search_track(terms) done = True except spotimeta.ServiceUnavailable, e: print "Service unavailable, muting and trying again in 30 seconds" raise LookupFailure(str(e)) # TODO: fix. check current state #mute() time.sleep(30) #unmute() except spotimeta.ServerError, e: print "Server error, muting and trying again in 30 seconds" raise LookupFailure(str(e)) #mute() time.sleep(30)
def get_tracks_by_artist(self, source_artist): #raise Exception(search["result"]) if source_artist not in self.lastFm.track_cache: combined_results = [] # from all pages page = 1 results = 1 while(results > 0): search = spotimeta.search_track('artist:"%s"' % source_artist,page=page) results = len(search["result"]) page = page+1 if(results == 0): break if(page>2 and search["result"][0]["artist"]["name"].lower() != source_artist.lower()): print_debug("Search has gone too deep - %s is not %s" % (search["result"][0]["artist"]["name"], source_artist)) break combined_results.extend(search["result"]) #break - comment to force 1-page support print_debug("Getting tracks by %s - got %s results over %s requests" % (source_artist,len(combined_results),(page-2))) self.lastFm.track_cache[source_artist] = combined_results return combined_results else: return self.lastFm.track_cache[source_artist]
def POST(self): formObj = myform() result = None if not formObj.validates(): return render.index(GetCurrentTrack() , web.songqueue) else: result = spotimeta.search_track(formObj['Track'].value) return render.search(formObj, result['result'])
def spotify(inp): "spotify <song> -- Search Spotify for <song>" data = spotimeta.search_track(inp.strip()) try: type, id = data["result"][0]["href"].split(":")[1:] except IndexError: return "Could not find track." url = gateway.format(type, id) return u"{} by {} - {}".format(data["result"][0]["name"], data["result"][0]["artist"]["name"], url)
def spotify_search(name): retries = 5 while retries > 0: try: results = spotimeta.search_track(name) return results except spotimeta.ServerError: print >> sys.stderr, " ... retrying spotify for ", name time.sleep(5) retries -= 1 return None
def get_spotify_info_for_song(song): """ given a single `pyechonest_song.Song`, search the spotify metadata api for it and return a dictionary containing the songs likely `urn` and `artist_urn` """ search_string = "artist:%s title:%s" % (song.artist_name, song.title) r = spotimeta.search_track(search_string, page=0) if r and "result" in r and len(r["result"]) > 0 and "href" in r["result"][0]: rval = {"urn": r["result"][0]["href"], "artist_urn": r["result"][0]["artist"]["href"]} else: rval = None logger.debug("%s=>%s", search_string, rval) return rval
def spotifytracks(tracklist): for track in tracklist: print "searching spotify for", track['artist'], track['track'] search = spotimeta.search_track(track['artist'] + " " + track['track']) if search["total_results"] > 0: topresult = search["result"][0] s_artist = ", ".join([a['name'] for a in topresult['artists']]) s_title = topresult['name'] s_href = topresult['href'] track['spotify'] = dict(s_artist=s_artist, s_title=s_title, s_href=s_href) yield track else: print "no results, skipping" continue
def verify_spotify_has_song(self, artist, title): search = spotimeta.search_track('artist:"%s" track:"%s"' % (artist, title)) if not search["result"]: return None track = search["result"][0] # validate this artist is in the track's artist collection for data_artist in track['artists']: print_debug("verifying %s is %s" % (data_artist['name'], artist)) if data_artist['name'].lower() == artist.lower(): return track print_debug("%s failed final validation. Attempt to find artist %s in %s failed" % (title, artist, track['artists'])) return None #artist not found data = self.metadata.lookup(track['href']) # TODO: check country code is in available territories return track
metacache = {} metadata = spotimeta.Metadata(cache=metacache) class track(object): pass searches = ["juslo fly away original mix", "estroe late night thinking winter depression remix", "Luomo the present lover", "Junior Boys Hazel Ewan Pearson House Remix", "don't see the point alex smoke", "fatand50 Estroe phat remix rocco caine"] tracks = [] print "------------------------- analyzing tracks -------------------------" for ts in searches: t = track() t.search = ts sp = spotimeta.search_track(ts) t.s_artist = sp["result"][0]["artist"]["name"] t.s_name = sp["result"][0]["name"] t.s_length = sp["result"][0]["length"] t.s_href = sp["result"][0]["href"] #pprint(sp) print "Spotify top hit: %s -- %s (%5.2fs) : href %s" % (t.s_artist, t.s_name, t.s_length, t.s_href) #echonestresult = song.search(artist=s_artist, title=s_name, buckets=['id:7digital', 'tracks'], limit=True) echonestresult = song.search(artist=t.s_artist, title=t.s_name, buckets=['audio_summary']) ares = echonestresult[0] asum = ares.audio_summary print " %s -- %s (%5.2fs) @ %3.3f bpm / %d %% energy " % (ares.artist_name, ares.title, asum["duration"], asum["tempo"], asum["energy"] * 100) t.tempo = asum["tempo"] t.length = asum["duration"]
def build_tree(root, isl_list): if(len(isl_list)>0): # "check_beginning_block" # For each word in isl_list, sequentially append words to an input_string and search for tracks using spotimeta. # Each search result will be pruned keep only songs that have input_string at the *beginning* of the track title # Originally, this portion of the code only performed the search using the first word of isl_list (e.g. isl_list[0]); # however, the results from the search do not necessarily give ALL songs with the first word corresponding to isl_list[0]. # This resulted in lots of tracks missing on test cases. tmp = list() for i in range(len(isl_list)): if(i == 0): input_string = isl_list[0] else: input_string = input_string + " " + isl_list[i] print(input_string) # time is used to avoid hitting API limit of 10 request per second time.sleep(.15) # couldn't figure out quite how to cache things, but i believe it should go here prior to calling search aux = spotimeta.search_track(input_string) aux2 = aux['result'] # if we can't find anything with input_string, assume that input_string + something_else will also return no results if(len(aux2)>0): tmp.append(isl_in_start_of_aux(input_string, aux2)) else: break if(len(tmp)>0): aux_within = reduce(lambda x,y: x+y, tmp) else: aux_within = tmp current_songs = list() current_inputs = list() input_string_temp = isl_list[0] # "check_song_block" # For each word in isl_list, sequentially append words to input_string_temp and check for tracks that match exactly # to input_string_temp. If there is a match, we will create a child node out of it using the song track and # all words after the last word of the track. for s in range(len(isl_list)): # here, we may alternatively set the range to be range(min(5, len(isl_list))) to speed up if(s > 0): input_string_temp = input_string_temp + " " + isl_list[s] aux_song = is_song(input_string_temp, aux_within) print(input_string_temp) if(aux_song != None): current_songs.append(aux_song) if(s<(len(isl_list)-1)): # these inputs will be used to create a child node using all words subsequent to isl_list[s] current_inputs.append(isl_list[(s+1):(len(isl_list))]) else: current_inputs.append(list()) # "children block" if(len(current_songs) > 0): for k in range(len(current_songs)): new_node = Tree() new_node.data = current_songs[k] new_node.parent = root new_node.children.append(build_tree(new_node, current_inputs[k])) root.children.append(new_node) else: root.children = None
### 3. MATCH REMAINING TRACKS ON SPOTIFY AND UPDATE WITH SPOTIFY_URI ### cur = db_conn.cursor() cur.execute("select track_id, name, artist from stalkify_tracks where spotify_uri is null and processed_p is false order by track_id limit 1000") totaltracks = 0 hittracks = 0 missedtracks = 0 errortracks = 0 for row in cur.fetchall(): totaltracks = totaltracks + 1 try: track_id = row[0] name = row[1].decode('utf-8') artist = row[2].decode('utf-8') search = spotimeta.search_track("%s %s" % (name, artist)) if search["total_results"]>0: # Save the spotify_uri; then the sync script will carry on the good word spotify_uri = search["result"][0]["href"] cur.execute("update stalkify_tracks set spotify_uri=%s where track_id = %s", (spotify_uri, track_id)) spotimeta_artist = search["result"][0]["artist"]["name"] spotimeta_name = search["result"][0]["name"] print ("%s: Queued %s - %s (%s): %s - %s" % (totaltracks, artist, name, spotify_uri, spotimeta_artist, spotimeta_name)) sys.stdout.flush() hittracks = hittracks + 1 else: # We won't be able to do more for these tracks cur.execute("update stalkify_tracks set processed_p=true, processed_date=now() where track_id = %s", (track_id, )) missedtracks = missedtracks + 1 except: cur.execute("update stalkify_tracks set processed_p=true, processed_date=now() where track_id = %s", (track_id, ))
def spotify(query): search = spotimeta.search_track(query) if search['total_results'] > 0: return search['result'][0]['href'] else: return None
def spotify(query): search = spotimeta.search_track(query) if search['total_results']>0: return search['result'][0]['href'] else: return None