def search_quality(artist_name,track_name): result,titles = pygn.search(clientID=clientID, userID=userID, artist=artist_name, track=track_name) newjson = json.dumps(result, sort_keys=True, indent=4) ddata = eval(newjson) first_layer = ddata["tempo"]["1"]["TEXT"] second_layer = ddata["tempo"]["2"]["TEXT"] return first_layer,second_layer
def checkGN(art, track): tags = [] rj = pygn.search(clientID=gracenote_client_id, userID=gracenote_user_id, artist=art, track=track) if rj is None: print('Dead song found - {0}: {1}'.format(art.encode('utf-8'), track.encode('utf-8'))) return ['non'] #Gracenote return too much info, he can found song with absolutly random name, so i need to make this shitcode. #Maybe normal solution can exsist, but idk how to make it without f*****g my brain if rj['tracks'][0]['track_title'] != track: print('Strange song found - {0}: {1}. Catched: {2}'.format( art.encode('utf-8'), track.encode('utf-8'), rj['tracks'][0]['track_title'].encode('utf-8'))) #a = raw_input('More info?(Y/Any):') #if a == 'Y': # print(rj) #a = raw_input('Kill that?(Y/Any):') #if a == 'Y': #return ['nan'] #print("{0}: {1} (?) {2}".format(art.encode('utf-8'), track.encode('utf-8'), rj['tracks'][0]['track_title'].encode('utf-8'))) for k in rj['genre']: tags.append(rj['genre'][k]['TEXT']) return tags
def get_mood(artist, track): ''' Gets artist and track information from Gracenote ''' #print artist, track clientid = 'your-gracenote-client-id' userid = pygn.register(clientid) gn_dict = defaultdict(list) gn_info = pygn.search(clientid, userid, artist=artist, track=track) try: gn_dict['gnid'] = gn_info['track_gnid'] except: return None # artist specific info for a in ['artist_origin', 'artist_type', 'artist_era']: get_gn_multiple(gn_info, gn_dict, a) # track specific info for s in ['genre', 'mood', 'tempo']: # can potentially drop 'tempo' since Spotify has already captured this get_gn_multiple(gn_info, gn_dict, s) #return dict(gn_dict) return gn_dict['mood_1']
def fetchGraceNote(qartist,qalbum,detail,fid): metadata = pygn.search(clientID=clientID, userID=userID, artist=qartist,album=qalbum) try: filename = "./wiki_gn/"+fid title = metadata["album_title"] artist = metadata["album_artist_name"] genre = [] for item in metadata["genre"]: genre.append(metadata["genre"][item]["TEXT"]) album_art_url = metadata["album_art_url"] print("url = " + album_art_url) if(len(album_art_url)>0): f=open(filename+".gnpkl","wb") pickle.dump(metadata,f) f.close() fout = open(filename+".gnmeta","w") fout.write("artist = {0}\n".format(artist)) fout.write("title = {0}\n".format(title)) fout.write("genre = {0}\n".format(genre)) fout.write("img_gn = {0}\n".format(album_art_url)) fout.close() except: print("********** {0} failed **********".format(filename)) fout=open("_faillist.txt","a") fout.write("*******************\n") fout.write(filename+"\n") fout.write(traceback.format_exc()+"\n") fout.close()
def query_mood(userID, artist, track): result = pygn.search(clientID=config["GRACENOTE_WEB_ID"], userID=userID, artist=artist, track=track) moods = [] moods.append(result['mood']['1']['TEXT']) moods.append(result['mood']['2']['TEXT']) print("Found the following moods: "+ moods[0] +", " + moods[1]) return moods
def fetch_meta(artist, track): """ Fetch metadata for artist and track: track, album, artist, track_no, genre, mood, year, art, duration """ clientID = '2104584822-2DA6486B91DF135895A29CC1A6A3A6E7' userID = '49075496506642242-F63F92D359342233D28026EFFADCE3A0' meta = False try: result = pygn.search(clientID=clientID, userID=userID, artist=artist, track=track) except: result = False if result: meta = { "track": non_unicode(result["track_title"]), "album": non_unicode(result["album_title"]), "artist": non_unicode(result["album_artist_name"]), "track_no": result["track_number"], "genre": [y["TEXT"] for x, y in result["genre"].iteritems()], "mood": [y["TEXT"] for x, y in result["mood"].iteritems()], "year": result["album_year"], "album_art": fix_art(result["album_art_url"]), "artist_art": fix_art(result["artist_image_url"]), "length": track_length(), "price": track_price() } return meta
def search_for_song(temp_query, temp_artist, gn_user_ID): ''' This function takes all the Spotify data retrieved and sends it through GraceNote in order to get the proper genres for each song. ''' # It is in a large try/except block because of an error that would pop # up when GraceNote couldnt find a song. try: metadata = pygn.search(clientID=gn_client_ID, userID=gn_user_ID, track=temp_query, artist=temp_artist) # GraceNote has 3 seperate genres for every song, more and more specific # when running, option 1(Broad), will only use the first line, # where as the 'Narrow' option will use the second line and the first line. temp_genre1 = metadata['genre']['1']['TEXT'] temp_genre2 = metadata['genre']['2']['TEXT'] print("Genre Found!") if temp_genre1 is None or temp_genre2 is None: temp_genre1 == "N/A" temp_genre2 == "N/A" return temp_genre1, temp_genre2 # these type errors are when the song is not found except TypeError as y: temp_genre1 = 'N/A' temp_genre2 = 'N/A' # these key errors are when the song is found but has no genre except KeyError as e: temp_genre1 = 'N/A' temp_genre2 = 'N/A' # I kept getting this, I didnt know what to do, so I just pass it... except UnboundLocalError as idk: pass
def search_results(request): if request.method == "POST": data = json.loads(request.body) print "HELLO!" print data pprint.pprint(data) query = "{} motion picture".format(data['album_query']) print query sp = spotipy.Spotify() current_movie, created = Movie.objects.get_or_create(title=data['album_query']) current_movie.save() current_movie.listener.add(request.user) # current_movie.add(request.user) movie_id = sp.search(query, limit=1, type="album")['albums']['items'][0]['id'] # current_movie.add(Listener.objects.get(username=request.user)) print request.user current_movie.save() songs = sp.album_tracks(movie_id) for song in songs['items']: this_song, created = Song.objects.get_or_create(title=song['name'], artist=song['artists'][0]['name']) this_song.save() print song['name'] print song['artists'][0]['name'] this_song.song_movie.add(current_movie) this_song.listener.add(request.user) # above line to link the many to many relationship print "HELLOOOOOO" print this_song.artist result = pygn.search(clientID= g_client_id, userID= g_user_id, artist=this_song.artist, track=this_song.title) mood_dict = result["mood"] mood_present = "1" in mood_dict.keys() #Checks if there is actually a mood forthe song mood_2_present = "2" in mood_dict.keys() if mood_present: print mood_dict["1"]['TEXT'] #This is the Mood you want!! new_mood, created = Mood.objects.get_or_create(feel= mood_dict["1"]['TEXT']) new_mood.save() new_mood.listener.add(request.user) new_mood.mood_song.add(this_song) # if mood_2_present: # new_2_mood = Mood.objects.create(feel= mood_dict["2"]['TEXT']) # new_2_mood.save() # new_2_mood.listener.add(request.user) # new_2_mood.mood_song.add(this_song) else: print "No Moods" # result2 = result["mood"]["1"] # print result # print result2 print "============================================================" json.dumps(result, sort_keys=True, indent=4) # json.dumps(result2, sort_keys=True, indent=4) print "++++++++++++++++++++++++++++++++++++++++++++==" # artist = songs['items'][0]['artists'][0]['name'] return HttpResponse(json.dumps({'msg': 'Movie Added!'}), content_type='application/json')
def getGraceNoteMetadata(artist, song): clientID = '2122781398-E1CD26B308E4A8C61E4DDED49E9D1D60' # Enter your Client ID here userID = pygn.register(clientID) metadata = pygn.search(clientID=clientID, userID=userID, artist=artist, track=song) '''for elem in metadata: if str(metadata[elem]) != '': print 'Field = ' + elem print '\t' + str(metadata[elem]) print "\n" ''' return metadata
def main(client_id, user_id, license, filename): """ Takes media files, converts it to 44100 wav file, tries to recognize it and returns metadata. """ if user_id is None: user_id = pygn.register(client_id) click.echo('user_id: {}'.format(user_id)) return with tempfile.NamedTemporaryFile(suffix='.wav') as wav: subprocess.check_call(['sox', filename, '-r', '44100', wav.name]) c_id, tag_id = client_id.split('-') res = subprocess.Popen([ 'gracetune_musicid_stream', c_id, tag_id, license, 'online', wav.name ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = res.communicate() try: res = json.loads(stdout) except Exception: print(json.dumps({'error': stderr}, indent=4)) return if 'error' in res: print(json.dumps(res, indent=4)) return for _ in xrange(5): try: metadata = pygn.search( clientID=client_id, userID=user_id, artist=res['artist'], album=res['album'], track=res['track'], ) if metadata: print(json.dumps(metadata, indent=4)) return except Exception: time.sleep(0.5) print(json.dumps({'error': 'failed to fetch song details via pygn'}, indent=4))
def _fetch_cover_art(self): clientID = GRACENOTE_KEY userID = pygn.register(clientID) metadata = pygn.search(clientID=clientID, userID=userID, artist=self.artist, track=self.title.split('/')[0]) try: artist_pic = os.path.join(base_path(), 'tmp', md5.md5(metadata['album_art_url']).hexdigest()) if not os.path.exists(artist_pic): r = requests.get(metadata['album_art_url']) with open(artist_pic, 'wb') as wh: wh.write(r.content) self.album_art = '/audio_files/'+os.path.split(artist_pic)[1] except (KeyError, requests.exceptions.MissingSchema): self.album_art = None
def new_from_file(cls, user, fobj): temp_name_base = str(uuid.uuid4()) temp_name = os.path.join(MUSIC_DIR, temp_name_base + ".mp3") fobj.save(temp_name) # First lets grab the metadata cur = os.getcwd() os.chdir(MUSIC_DIR) meta = eyed3.load(os.path.basename(temp_name)) os.chdir(cur) # Grab checksum checksum = os.popen("%s %s" % (MD5SUM, temp_name)).read().split(" ", 1)[0] # Normalize Audio new_temp_name = os.path.join(MUSIC_DIR, temp_name_base + "_normal.mp3") os.popen("sox --norm %s %s" % (temp_name, new_temp_name)) temp_name = new_temp_name # We need some basic stuff if not meta.tag.artist or not meta.tag.title: raise Exception("Not enough metadata") count = cls.select(cls.id).where( ((cls.artist == meta.tag.artist) & (cls.title == meta.tag.title)) | (cls.checksum == checksum) ).count() # Already exists if count: return -1 # Attempt to get album art pygn_meta = pygn.search(clientID=GN_CLI, userID=GN_USR, artist=meta.tag.artist, album=meta.tag.album, track=meta.tag.title) song = cls() song.owner = user song.title = meta.tag.title song.artist = meta.tag.artist song.album = meta.tag.album song.cover = pygn_meta.get("album_art_url") song.location = song.create_song_path() song.checksum = checksum os.rename(temp_name, song.location) return song.save()
def get_genre(filename): clientID = '11545856-DEC3E5FFDC52222C23C090C9C280B9EC' audiofile = eyed3.load(filename) artist = audiofile.tag.artist album = audiofile.tag.album title = audiofile.tag.title userID = pygn.register(clientID) metadata = pygn.search(clientID=clientID, userID=userID, artist=artist, album=album, track=title) try: print metadata['genre']['1']['TEXT'] return metadata['genre']['1']['TEXT'] except: return None
def get_genre_list(song_title, album_name): metadata = pygn.search(clientID=pgyn_clientID, userID=pgyn_userID, track=song_title, album=album_name) if not metadata: # print 'Unknown Error' return None elif 'genre' in metadata: genre_list = [] genre_data = metadata['genre'] for gi in genre_data: genre_text = genre_data[gi]['TEXT'] genre_list += [genre_text] return genre_list else: # print 'No Genre Error' # print metadata return None
def generating(clientID='', userID='', artist='', title='', album='', save=''): if title is bool: return 0 #convert the three fields into usable link tokens artist = artist.lower().replace(" ", "-") title = title.lower().replace(" ", "-") album = album.lower().replace(" ", "-") #if the artis is blank we will try to find a new artist usying pygn if artist == '': print('no artist name') temp = pygn.search(clientID=clientID, userID=userID, track=title) artist = temp['track_artist_name'] #print(artist) #return 0 #lets try to lookup the lyrics sit try: generate_url = 'http://lyrics.az/' + artist + '/' + album + '/' + title + '.html' #urllib.request.urlopen(generate_url) return processing(generate_url, artist, title, save) #in this case we can try the url with the '-' album except HTTPError: if album != '-': return generating(clientID=clientID,\ userID=userID,\ artist=artist,\ title= title,\ album='-',\ save = True ) else: print('failed!') generate_url = 'http://lyrics.az/' + artist + '/' + album + '/' + title + '.html' print(generate_url) print('-===-') return 0
def application(environ, start_response): # Get Request(GET) from app query = cgi.parse_qsl(environ.get('QUERY_STRING')) param_dict, ret_dict = {'artist': '', 'track': ''}, {} for params in query: param_dict[params[0]] = params[1] start_response("200 OK", [('Content-Type','application/json'), ('charset', 'utf-8')]) try: metadata = pygn.search(clientID=clientID, userID=userID, artist=param_dict['artist'].decode('utf-8'), track=param_dict['track'].decode('utf-8')) ret_dict['title'] = metadata['track_title'] ret_dict['jacket'] = metadata['album_art_url'] ret_dict['artist'] = metadata['album_artist_name'] ret_dict['artist_art'] = metadata['artist_image_url'] ret_dict['tempo'] = metadata['tempo']['3']['TEXT'][:-1] ret_dict['mood'] = metadata['mood']['1']['TEXT'][:-1] except: ret_dict['error'] = 'results not found' return json.dumps(ret_dict)
def search_artist_info(artist_name): result,titles = pygn.search(clientID=clientID, userID=userID, artist=artist_name) newjson = json.dumps(result,sort_keys=True,indent=4) ddata = eval(newjson) artist_era = [] artist_img = ddata["artist_image_url"] artist_origin = [] artist_type = [] artist_genre = [] artist_mood = [] for x,item in ddata["artist_era"].items(): artist_era.append(item["TEXT"]) for x, item in ddata["artist_origin"].items(): artist_origin.append(item["TEXT"]) for x, item in ddata["artist_type"].items(): artist_type.append(item["TEXT"]) for x, item in ddata["genre"].items(): artist_genre.append(item["TEXT"]) for x, item in ddata["mood"].items(): artist_mood.append(item["TEXT"]) Artist.objects.create(name=artist_name, era=artist_era, origin=artist_origin, genre=artist_genre, mood=artist_mood, img=artist_img)
def fetchGraceNote(qartist,qalbum,detail,fid): metadata = pygn.search(clientID=clientID, userID=userID, artist=qartist,album=qalbum) try: filename = "./anigen_gn/"+fid title = metadata["album_title"] artist = metadata["album_artist_name"] artist_origin = [] for item in metadata["artist_origin"]: artist_origin.append(metadata["artist_origin"][item]["TEXT"]) genre = [] for item in metadata["genre"]: genre.append(metadata["genre"][item]["TEXT"]) flag=False album_art_url = metadata["album_art_url"] print("url = " + album_art_url) if((len(album_art_url)>0) and ("Japan" in artist_origin)): f=open(filename+".gnpkl","wb") pickle.dump(metadata,f) f.close() fout = codecs.open(filename+".gnmeta","w",encoding="utf-8") fout.write("artist = {0}\n".format(artist)) fout.write("title = {0}\n".format(title)) fout.write("genre = {0}\n".format(genre)) fout.write("img_gn = {0}\n".format(album_art_url)) fout.write("origin = {0}\n".format(artist_origin)) fout.write("query_title = {0}\n".format(qalbum)) fout.write("query_artist = {0}\n".format(qartist)) fout.close() except: print("********** {0} failed **********".format(filename)) fout=codecs.open("_faillist.txt","a",encoding="utf-8") fout.write("*******************\n") fout.write(filename+"\n") fout.write(traceback.format_exc()+"\n") fout.close()
def query_and_embed(mp3path, track_name, artist, album): print('inside query_and_embed!!') artist = '' if artist == 'unknown' else artist album = '' if artist == 'unknown' else album try: metadata = pygn.search(clientID=clientID, userID=userID, artist=artist, album=album, track=track_name) except: return artist = metadata['album_artist_name'] album = metadata['album_title'] album_year = metadata['album_year'] album_artwork = metadata['album_art_url'] print('metadata fetched!',artist,album,album_year) '''deleting previous metadata''' ##mp3 = MP3(mp3path) ##mp3.delete() ##mp3.save() try: audio = ID3(mp3path) audio.delete() except: return if album_artwork == '': album_artwork = get_art(artist,track_name,album) art_embedder(mp3path,album_artwork) print('art embedding done!!!!!!!!!!!!!!!!') audio = ID3(mp3path) audio.add(TIT2(encoding=3, text=track_name)) audio.add(TPE1(encoding=3, text=artist)) audio.add(TALB(encoding=3, text=album)) audio.add(TDRC(encoding=3, text=album_year)) audio.save()
def search_artist_info(artist_name): result,titles = pygn.search(clientID=clientID, userID=userID, artist=artist_name) newjson = json.dumps(result,sort_keys=True,indent=4) ddata = eval(newjson) artist_era = [] # [ddata["artist_era"]["1"]["TEXT"],ddata["artist_era"]["2"]["TEXT"]] artist_img = ddata["artist_image_url"] artist_origin = [] artist_type = [] artist_genre = [] artist_mood = [] for item in ddata["artist_era"]: artist_era.append(item["TEXT"]) for item in ddata["artist_origin"]: artist_origin.append(item["TEXT"]) for item in ddata["artist_type"]: artist_type.append(item["TEXT"]) for item in ddata["artist_genre"]: artist_genre.append(item["TEXT"]) for item in ddata["artist_mood"]: artist_mood.append(item["TEXT"]) return [artist_img,artist_era,artist_name,artist_mood,artist_genre,artist_type]
def get_genres(): # read the newly created feed feed = feedparser.parse("datafiles/f-measure.xml") # want the set of unique artists uniqueArtists = set() # get the artists from the feed for e in feed.entries: m = artistsExtractor.match(e.title) if m is not None: # my regex grabs the trailing space dash in some cases # so remove it uniqueArtists.add(m.group(1).rstrip(" -")) # set up for getting the unique artist genre userID = pygn.register(gnmUID) artistsInfo = {} # store all associated genre information per artist artistsToGenre = {} # store the direct mapping of the chosen genre # for each artist for ua in uniqueArtists: # get their metadata metaData = pygn.search(clientID=gnmUID, userID=userID, artist=ua) uaG = [] print(ua) # if its not none then Gracenote Music has information on them if metaData is not None: # extract the genre for the artists for genre in map(lambda ge: ge["TEXT"], metaData["genre"].values()): uaG.append(genre) artistsInfo[ua] = uaG artistsToGenre[ua] = determine_genre(ua, uaG) # write data to file with open("datafiles/artistsInfo.json", "w+") as out: out.write(json.dumps(artistsInfo, indent=1)) with open("datafiles/artistsToGenre.json", "w+") as out: out.write(json.dumps(artistsToGenre, indent=1))
def get_song_metadata(song_name, artist_name): # Query database and get song metadata as a dict return pygn.search(clientID=clientID, userID=userID, artist=artist_name, track=song_name)
def get_gracenote_track_data(artist, album, track): gnmetadata = pygn.search(CLIENT_ID, USER_ID, artist, album, track) return gnmetadata
if not songs[i][0]: continue # Determines if a song's info has been edited updated = False # Song info from Columns 1 and 2 artist = songs[i][0] track = songs[i][1] # Update genre if not songs[i][3]: updated = True try: metadata = pygn.search(clientID=clientID, userID=userID, artist=str(artist), track=str(track)) genres = metadata['genre'] output = "" # Add all three genres for j in range(len(genres)): value = str(genres[str(j + 1)]['TEXT']) output += (value if value != 'Alternative & Punk' else 'Alternative') + \ (', ' if j < len(genres)-1 else "") sheet.update_cell(i + 1, 4, output) except TypeError: sheet.update_cell(i + 1, 4, "unknown") # Only check if status is not listed if not songs[i][4]: updated = True
# NOTE: # userID is obtained by executing the following command # # userID = pygn.register(clientID) # # IMPORTANT: Run this command only *once*, and store the userID for future use. for root, _, files in os.walk(sys.argv[1]): artist = os.path.split(root)[1] for f in files: if f.endswith('.mp3') or f.endswith('.MP3'): track = os.path.splitext(f)[0].replace('_', ' ') audioFile = eyed3.load(os.path.join(root, f)) metadata = pygn.search(clientID=clientID, userID=userID, artist=artist, track=track) print("Title :", metadata['track_title']) print("Artist :", metadata['album_artist_name']) print("Album :", metadata['album_title']) print("Genre :", ', '.join([x['TEXT'] for _, x in metadata['genre'].items()])) print("Year :", metadata['album_year']) print("Album art :", metadata['album_art_url']) audioFile.tag.title = str(metadata['track_title']) audioFile.tag.artist = str(metadata['album_artist_name']) audioFile.tag.genre = str(', '.join( [x['TEXT'] for _, x in metadata['genre'].items()])) audioFile.tag.year = str(metadata['album_year'])
def populate_sql(artist_id, spotify, gracenote_clientID, gracenote_userID): '''Populate the psql database with song data for specified artist, pulling data from Spotify and Gracenote APIs''' # For each artist, get all of their albums (including singles) artist_query = pd.read_sql_query( "SELECT DISTINCT artist FROM artist_songs", con=engine) if artist_id not in list(artist_query.artist): offset = 0 album_compilation = [] search_albums = [ i['id'] for i in spotify.artist_albums( artist_id, limit=50, offset=offset)['items'] ] album_compilation.extend(search_albums) while len(search_albums) > 0: offset += 50 search_albums = [ i['id'] for i in spotify.artist_albums( artist_id, limit=50, offset=offset)['items'] ] album_compilation.extend(search_albums) # Get all tracks for each album search_tracks = {} for albums in chunks(album_compilation, 20): for album in spotify.albums(albums)['albums']: for track in album['tracks']['items']: for artist in track['artists']: if artist['id'] == artist_id: search_tracks[track['name'].lower()] = track['id'] # Get features for each track (and discard tracks without feature information) playlist_features = [] for tracks in chunks(search_tracks.values(), 100): playlist_features.extend(spotify.audio_features(tracks)) playlist_nonempty_idx = [ idx for idx, features in enumerate(playlist_features) if features != None ] final_tracks = [search_tracks.keys()[i] for i in playlist_nonempty_idx] playlist_features = [ playlist_features[i] for i in playlist_nonempty_idx ] # Filter songs by popularity to reduce covers, remixes, and other song versions that may # add too much noise (*ba dum tss*) playlist_songs = pd.DataFrame(playlist_features).drop( ['analysis_url', 'track_href', 'type', 'uri'], axis=1) playlist_songs['name'] = final_tracks playlist_songs = playlist_songs.groupby(['id', 'name']).mean().fillna(0) popularity_info = [] for tracks in chunks(playlist_songs.index.levels[0], 25): for track in spotify.tracks(tracks)['tracks']: popularity_info.append({ 'id': track['id'], 'popularity': track['popularity'], 'artist': artist_id, 'explicit': track['explicit'] }) popularity_info = pd.DataFrame(popularity_info) playlist_songs = pd.merge(playlist_songs.reset_index(), popularity_info, how='left') # Use median song popularity by artist as a cutoff popularity_cutoff = playlist_songs.groupby( ['artist'], as_index=False).popularity.median() popularity_cutoff.columns = ['artist', 'cutoff'] playlist_songs = pd.merge(playlist_songs, popularity_cutoff, how='left') playlist_songs['artist_name'] = spotify.artist(artist_id)['name'] playlist_songs = playlist_songs[ playlist_songs.popularity > playlist_songs.cutoff].set_index( ['id', 'name', 'artist', 'artist_name']) # Now, for this artist we have a dataframe containing Spotify track data for songs above median # popularity. We add Gracenote data in the following lines. gn_data = [] for track in list(playlist_songs.index): artist_name = track[-1] song_name = track[1] spotify_id = track[0] # Use fuzzy matching to align Gracenote search results with Spotify tracks. # Collect artist_type, genre, and mood (primary and secondary) from Gracenote, # where available pygn_results = pygn.search(clientID=gracenote_clientID, userID=gracenote_userID, artist=artist_name, track=song_name) if fuzz.ratio(pygn_results['track_title'], song_name) > 50: info_dict = {} features = ['artist_type', 'genre', 'mood'] for feature in features: if len(pygn_results[feature]) > 0: info_dict[feature] = pygn_results[feature]['1']['TEXT'] if feature == 'genre': info_dict[ feature + '_2'] = pygn_results[feature]['2']['TEXT'] else: info_dict[feature] = np.nan if feature == 'genre': info_dict[feature + '_2'] = np.nan gn_data.append({ 'id': spotify_id, 'artist_type_1': info_dict['artist_type'], 'genre_1': info_dict['genre'], 'genre_2': info_dict['genre_2'], 'mood_1': info_dict['mood'], 'album_year': pygn_results['album_year'], 'album_title': pygn_results['album_title'], 'gn_track_id': pygn_results['track_gnid'] }) # Reorganize Spotify and Gracenote data into three tables for psql database. # Append to existing tables (which were set up previously) gn_data = pd.DataFrame(gn_data) compiled_data = pd.merge(playlist_songs.reset_index(), gn_data, how='left') artist_songs = compiled_data[['id', 'name', 'artist', 'artist_name']] spotify_song_data = compiled_data[[ 'id', 'artist', 'acousticness', 'danceability', 'duration_ms', 'energy', 'instrumentalness', 'key', 'liveness', 'loudness', 'mode', 'speechiness', 'tempo', 'time_signature', 'valence', 'explicit', 'popularity', 'cutoff' ]] gracenote_song_data = compiled_data[[ 'id', 'artist', 'album_title', 'album_year', 'artist_type_1', 'genre_1', 'genre_2', 'gn_track_id', 'mood_1' ]] artist_songs.to_sql(name='artist_songs', con=engine, if_exists='append') spotify_song_data.to_sql(name='spotify_song_data', con=engine, if_exists='append') gracenote_song_data.to_sql(name='gracenote_song_data', con=engine, if_exists='append') sleep(1) # Refresh Spotify and Gracenote tokens, which may have expired during this process token = util.prompt_for_user_token('', client_id='', client_secret='', redirect_uri='') spotify = spotipy.Spotify(auth=token) gracenote_userID = pygn.register(gracenote_clientID)
song_info = song_info.astype('object') if token: sp = spotipy.Spotify(auth=token) offset = 0 t0 = time() tracks = sp.current_user_saved_tracks( offset=offset, limit=20) # get 20 songs from user's saved songs while (len(tracks['items']) != 0): # haven't reached the end of saved tracks for song in tracks['items']: features = sp.audio_features(song['track']['id'])[0] metadata = pygn.search(clientID=clientID, userID=userID, artist=song['track']['artists'][0]['name'], album=song['track']['album']['name'], track=song['track']['name']) genres = [] for key, genre in metadata['genre'].items(): genres.append(metadata['genre'][str(key)] ['TEXT']) # add all genres to list # print(metadata['genre'][str(key)]['TEXT']) if (len(genres) > 0): print('{} - {}'.format(song['track']['name'], metadata['genre']['1']['TEXT'])) if features is not None: # if features exist (sometimes they don't), add row to df song_info = song_info.append({'Song Name': song['track']['name'], 'Artist': song['track']['artists'][0]['name'], 'Album': song['track']['album']['name'], \ 'Duration': features['duration_ms'], 'Danceability': features['danceability'], 'Instrumentalness': features['instrumentalness'], \ 'Speechiness': features['speechiness'], 'Energy': features['energy'], 'Mode': features['mode'], 'Tempo': features['tempo'], 'Liveness': features['liveness'], \ 'Loudness': features['loudness'], 'Key': features['key'], 'Acousticness': features['acousticness'], 'Genre': genres}, ignore_index=True)
import pygn import json c = '14742784-696183B35476B91443B0E2BC457FDF29' u = pygn.register(c) def beautiful(gn_obj): print(gn_obj) data = json.dumps(gn_obj, sort_keys=True, indent=4, ensure_ascii=False) print(data) return data res = beautiful(pygn.search(clientID=c, userID=u, artist='Beatles')) print(res)
def get_gracenote_genre(artist='', track=''): result = pygn.search(clientID=clientID, userID=userID, artist=artist, track=track) #print json.dumps(result, sort_keys=True, indent=4) return result['genre']['1']['TEXT']
def findArtistAndAddToDB(artistname): # if session.query(Artist).filter_by(Artist) artistData = pygn.search(clientID=clientID, userID=userID, artist=artistname) # pprint(artistData) era = artistData.get("artist_era")['1']['TEXT'] a = Artist(name=artistData.get("album_artist_name"), era=era) artistname = artistData.get("album_artist_name") artist_object = session.query(Artist).filter_by(name=artistname).first() if not artist_object: session.add(a) session.commit() artist_object = session.query(Artist).filter_by(name=artistname).first() artistID = artist_object.artistid tracks = artistData.get("tracks") tracks_with_info = [] # for track in tracks: for track in tracks: track_info = pygn.search(clientID=clientID, userID=userID, artist=artistname, track=track['track_title']) tracks_with_info.append(track_info) album_title = track_info['album_title'] # if it doesnt work try getting rid of artist id in query album = session.query(Album).filter_by(albumtitle=album_title, artistid=artistID).first() if not album: al = Album(albumtitle=album_title, artistid=artistID) session.add(al) session.commit() albumid = session.query(Album).filter_by(albumtitle=album_title, artistid=artistID).first().albumid track_info['albumID'] = albumid # for track in tracks: for track in tracks_with_info: findTrack = session.query(Song).filter_by(songtitle=track['track_title']).first() if findTrack: continue else: s = Song(songtitle=track['track_title'], artistid=artistID, albumid=track['albumID']) session.add(s) session.commit() song_object = session.query(Song).filter_by(songtitle=track['track_title']).first() for k, mood in track['mood'].iteritems(): newmood = mood['TEXT'] mood_object = session.query(Mood).filter_by(name=newmood).first() if not mood_object: m = Mood(name=newmood) session.add(m) session.commit mood_object = session.query(Mood).filter_by(name=newmood).first() sm = SongMood(songid=song_object.songid, moodid=mood_object.moodid) session.add(sm) session.commit() for k, genre in track['genre'].iteritems(): newgenre = genre['TEXT'] genre_object = session.query(Genre).filter_by(name=newgenre).first() if not genre_object: g = Genre(name=newgenre) session.add(g) session.commit genre_object = session.query(Genre).filter_by(name=newgenre).first() sg = SongGenre(songid=song_object.songid, genreid=genre_object.genreid) session.add(sg) session.commit() for k, tempo in track['tempo'].iteritems(): newtempo = tempo['TEXT'] tempo_object = session.query(Tempo).filter_by(name=newtempo).first() if not tempo_object: t = Tempo(name=newtempo) session.add(t) session.commit tempo_object = session.query(Tempo).filter_by(name=newtempo).first() st = SongTempo(songid=song_object.songid, tempoid=tempo_object.tempoid) session.add(st) session.commit()
from __future__ import print_function import sys, pygn, json clientID = '' # Enter your Client ID from developer.gracenote.com here userID = '' # Get a User ID from pygn.register() - Only register once per end-user print('\nSearch for artist "Kings of Convenience"\n') result = pygn.search(clientID=clientID, userID=userID, artist='Kings of Convenience') print(json.dumps(result, sort_keys=True, indent=4)) print('\nSearch for album "Prism" by "Katy Perry"\n') result = pygn.search(clientID=clientID, userID=userID, artist='Katy Perry', album='Prism') print(json.dumps(result, sort_keys=True, indent=4)) print('\nSearch for track "Drop" by "Cornelius"\n') result = pygn.search(clientID=clientID, userID=userID, artist='Cornelius', track='Drop') print(json.dumps(result, sort_keys=True, indent=4)) print('\nSearching by album TOC\n') result = pygn.search(clientID=clientID, userID=userID, toc='150 20512 30837 50912 64107 78357 90537 110742 126817 144657 153490 160700 175270 186830 201800 218010 237282 244062 262600 272929') print(json.dumps(result, sort_keys=True, indent=4)) print('\nGetting artist discography for "Daft Punk"\n') result = pygn.get_discography(clientID=clientID, userID=userID, artist='Daft Punk') print(json.dumps(result, sort_keys=True, indent=4)) # Example how to create a radio playlist by mood peaceful result = pygn.createRadio(clientID, userID, mood='65322', popularity ='1000', similarity = '1000') print(json.dumps(result, sort_keys=True, indent=4))
def gracenoteSearch(user, album, artist, song): # the function used for calling all the metadata about the music in the user's playlist # this function is using the gracenote API to retrieve the information return pygn.search(clientID=gracenoteClientID, userID = user, artist=artist, album=album, track=song)
tracks = {} for chart in weekcharts: for song in chart: item = song.item tracks[song_to_key(item)] = item metadata = open_cache(settings["metadata_cache"]) for track in tracks.values(): k = song_to_key(track) if not metadata.has_key(k): print "Fetching '%s'..." % (k) m = pygn.search( clientID=GN_CLIENT_ID, userID=GN_USER_ID, artist=track.artist.name, #album=track.a, track=track.title) metadata[k] = m dump_cache(metadata, settings["metadata_cache"]) if settings['only_main_mood']: for k in metadata: if '1' in metadata[k]['mood'].keys(): metadata[k]['mood'] = {'1': metadata[k]['mood']['1']} song_moods_dict = {} for song, mdata in metadata.items(): song_moods_dict[song] = mdata['mood'] history = []
def search_songs(artist_name,albums): result,titles = pygn.search(clientID=clientID, userID=userID, artist=artist_name, album=albums) return titles
def gracenoteSearch(user, album, artist, song): return pygn.search(clientID=gracenoteClientID, userID=user, artist=artist, album=album, track=song)
except id3.ID3NoHeaderError: tags = id3.ID3() try: if artist in relate: real_artist = relate[artist] else: real_artist = artist if music[-4:] == '.mp3': music = music[:-4] else: music_path += '.mp3' result = search(clientID=clientID, userID=userID, artist=real_artist, track=music) except UnboundLocalError: print("Failed query with artist '{0}' and music '{1}'".format( artist, music)) continue try: album_art = request.urlopen(result['album_art_url']).read() except (error.URLError, ValueError, TypeError): album_art = None for each in result['genre'].keys(): if result['genre'][each]['TEXT'] in genres: result['genre'] = result['genre'][each]['TEXT'] break
from __future__ import print_function import sys, pygn, json clientID = '13118464-4E8A3151390D6E9F3979F8715F51F7F3' # Enter your Client ID from developer.gracenote.com here userID = '27678420343239243-6E83356B7E66B2E6A2BF0B9B54C551AD' # Get a User ID from pygn.register() - Only register once per end-user lines = open('krishnan_raw_reco.csv','r').read().split("\n") for line in lines: vals = line.split(",") artist = vals[3] track = vals[4] print("Processing : ", (track+ " | " + artist ), file=sys.stderr) #print(track, " | " , vals) result = pygn.search(clientID=clientID, userID=userID, artist=artist, track=track) print(json.dumps(result, sort_keys=True, indent=4))
import sys, pygn, json print 'loading' f = open('../all.tags.json', 'r') s = f.read() j = json.loads(s) f.close() print 'client...' clientID = '582912-203D3664F314C618AF20562C0EF17B5E' # Enter your Client ID from developer.gracenote.com here #userID = '259611467494464959-6A2DAD2AB9C1A6189167742A9F3105BE' # Get a User ID from pygn.register() - Only register once per end-user #userID = '260455892557887637-F2F5BEDBA4FA97FDCBD92A3EA72F0F3B' userID = pygn.register(clientID) print userID print '\nSearch"\n' print j[0]['artist'][0] s = 'Lyle Lovett' #sys.exit(0) result = pygn.search(clientID=clientID, userID=userID, artist=s) print json.dumps(result, sort_keys=True, indent=4)
from __future__ import print_function import sys, pygn, json clientID = '13118464-4E8A3151390D6E9F3979F8715F51F7F3' # Enter your Client ID from developer.gracenote.com here userID = '27678420343239243-6E83356B7E66B2E6A2BF0B9B54C551AD' # Get a User ID from pygn.register() - Only register once per end-user print('\nSearch for artist "Kings of Convenience"\n') result = pygn.search(clientID=clientID, userID=userID, artist='Kings of Convenience') print(json.dumps(result, sort_keys=True, indent=4)) print('\nSearch for album "Prism" by "Katy Perry"\n') result = pygn.search(clientID=clientID, userID=userID, artist='Katy Perry', album='Prism') print(json.dumps(result, sort_keys=True, indent=4)) print('\nSearch for track "Drop" by "Cornelius"\n') result = pygn.search(clientID=clientID, userID=userID, artist='Cornelius', track='Drop') print(json.dumps(result, sort_keys=True, indent=4)) print('\nSearching by album TOC\n') result = pygn.search( clientID=clientID, userID=userID, toc=
for chart in weekcharts: for song in chart: item = song.item tracks[song_to_key(item)] = item metadata = open_cache(settings["metadata_cache"]) for track in tracks.values(): k = song_to_key(track) if not metadata.has_key(k): print "Fetching '%s'..." % (k) m = pygn.search(clientID=GN_CLIENT_ID, userID=GN_USER_ID, artist=track.artist.name, #album=track.a, track=track.title) metadata[k] = m dump_cache(metadata, settings["metadata_cache"]) if settings['only_main_mood']: for k in metadata: if '1' in metadata[k]['mood'].keys(): metadata[k]['mood'] = {'1': metadata[k]['mood']['1']} song_moods_dict = {} for song, mdata in metadata.items(): song_moods_dict[song] = mdata['mood']
import sys, pygn, json clientID = '582912-203D3664F314C618AF20562C0EF17B5E' # Enter your Client ID from developer.gracenote.com here #userID = '259611467494464959-6A2DAD2AB9C1A6189167742A9F3105BE' # Get a User ID from pygn.register() - Only register once per end-user userID = pygn.register(clientID) print userID print '\nSearch for artist "Kings of Convenience"\n' result = pygn.search(clientID=clientID, userID=userID, artist='Kings of Convenience') print json.dumps(result, sort_keys=True, indent=4) print '\nSearch for album "Prism" by "Katy Perry"\n' result = pygn.search(clientID=clientID, userID=userID, artist='Katy Perry', album='Prism') print json.dumps(result, sort_keys=True, indent=4) print '\nSearch for track "Drop" by "Cornelius"\n' result = pygn.search(clientID=clientID, userID=userID, artist='Cornelius', track='Drop') print json.dumps(result, sort_keys=True, indent=4) #print '\nSearching by album TOC\n' #result = pygn.search(clientID=clientID, userID=userID, toc='150 20512 30837 50912 64107 78357 90537 110742 126817 144657 153490 160700 175270 186830 201800 218010 237282 244062 262600 272929') #print json.dumps(result, sort_keys=True, indent=4) print '\nGetting artist discography for "Daft Punk"\n'
print('\nSearch for track "Drop" by "Cornelius"\n') result = pygn.search(clientID=clientID, userID=userID, artist='Cornelius', track='Drop') print(json.dumps(result, sort_keys=True, indent=4)) print('\nSearching by album TOC\n') result = pygn.search(clientID=clientID, userID=userID, toc='150 20512 30837 50912 64107 78357 90537 110742 126817 144657 153490 160700 175270 186830 201800 218010 237282 244062 262600 272929') print(json.dumps(result, sort_keys=True, indent=4)) print('\nGetting artist discography for "Daft Punk"\n') result = pygn.get_discography(clientID=clientID, userID=userID, artist='Daft Punk') print(json.dumps(result, sort_keys=True, indent=4)) ''' # Example how to create a radio playlist by mood peaceful result = pygn.search(clientID=clientID, userID=userID, track='Roar') print(json.dumps(result, sort_keys=True, indent=4)) for data in result: print('{}\n{}'.format(data['track_title'],data['track_artist_name'],data['album'])) print('GOGO AZ LYRICS') for data in result: title = data['track_title'] artist = data['track_artist_name'] if artist and title.replace(' ','').isalpha() and artist.replace(' ','').isalpha(): print(azapi.generating(artist,title,True)) ''' # Example how to create a radio playlist by genre classical music result = pygn.createRadio(clientID=clientID, userID=userID, genre='36061', popularity ='1000', similarity = '1000')
def large_scale_test(self, _title='', _artist='', _mood='', _genre='', _era=''): #query the grace note database and grab a list of songs and artists intensities = dict() # get ourselves a radio with a bunch of songs result = pygn.createRadio(clientID=clientID, userID=userID, \ artist=_artist,\ track=_title,\ genre=_genre,\ mood=_mood,\ era=_era, ) #print(json.dumps(result, sort_keys=True, indent=4)) #for data in result: # print('{}\n{}'.format(data['track_title'], data['track_artist_name'])) resultAlt = pygn.search(clientID=clientID, userID=userID, track=_title, artist= _artist) #print (resultAlt) if resultAlt: #print(resultAlt) # grab the title title = resultAlt['track_title'] # grab artist name artist = resultAlt['album_artist_name'] # grab the album name album = resultAlt['album_title'] # print(album) # cleaned_artists = pn.analyze().remove_accents(artist) cleaned_artists = pn.analyze().cleanse(artist) cleaned_artists = pn.analyze().remove_feat(cleaned_artists) cleaned_artists = pn.analyze().cleanse(cleaned_artists) # cleaned_title = pn.analyze().remove_accents(title) cleaned_title = pn.analyze().cleanse(title) cleaned_title = pn.analyze().remove_feat(cleaned_title) cleaned_title = pn.analyze().cleanse(cleaned_title) cleaned_album = pn.analyze().cleanse(album) cleaned_album = pn.analyze().remove_feat(cleaned_album) cleaned_album = pn.analyze().cleanse(cleaned_album) # make a tuple temp = [title, artist] # get the intensity intensity, fname = pn.analyze().setup_analysis(clientID=clientID, userID=userID, _artist_name=cleaned_artists, _song_name=cleaned_title, _album_title=cleaned_album) if intensity != 0: if fname != 0: pn.analyze().teardown(fname) # add our entry to the map intensities[temp[0]] = [intensity, temp[1]] print("added") print('GOGO AZ LYRICS') for data in result: #(result) if not data: print('No results') continue #grab the title title = data['track_title'] #grab artist name artist = data['album_artist_name'] #grab the album name album = data['album_title'] #print(album) #cleaned_artists = pn.analyze().remove_accents(artist) cleaned_artists = pn.analyze().cleanse(artist) cleaned_artists = pn.analyze().remove_feat(cleaned_artists) cleaned_artists = pn.analyze().cleanse(cleaned_artists) #cleaned_title = pn.analyze().remove_accents(title) cleaned_title = pn.analyze().cleanse(title) cleaned_title = pn.analyze().remove_feat(cleaned_title) cleaned_title = pn.analyze().cleanse(cleaned_title) cleaned_album = pn.analyze().cleanse(album) cleaned_album = pn.analyze().remove_feat(cleaned_album) cleaned_album = pn.analyze().cleanse(cleaned_album) #make a tuple temp = [title, artist] #get the intensity intensity, fname = pn.analyze().setup_analysis(clientID=clientID, userID=userID, _artist_name=cleaned_artists, _song_name=cleaned_title, _album_title=cleaned_album) if intensity == 0: continue #teardown the file if fname != 0: pn.analyze().teardown(fname) #add our entry to the map intensities[temp[0]] = [intensity, temp[1]] print("added") for item in intensities: #prin the tuple if item is None: continue if intensities[item][1] is None: continue link = self.link(item=item, item2=intensities[item][1]) if link is None: link = ' ' print('{:30.30}'.format(item) +' : Compound Score ' + '{:05.2f}'.format( 100 * intensities[item][0][0]) + \ ' : Negative Score ' + '{:05.2f}'.format((100 * intensities[item][0][1])) + \ ' : Neutral Score ' + '{:05.2f}'.format(100 * intensities[item][0][2]) + \ ' : Positive Score ' + '{:05.2f}'.format(100 * intensities[item][0][3]) + \ ' : Artist ' + '{:30.30}'.format( intensities[item][1]) + \ ' : Sample ' + link, ) return intensities