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 main(): clientID = '1984033224-5834A5143CE87D68376F48BF28A6BEE4' user_id = pygn.register(clientID) print( "Here is your Grace Note user ID: {}, \nIt is saved to \'grace_note_user_id.txt\'." .format(user_id)) f = open('grace_note_user_id.txt', 'w+') f.write(user_id)
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(): output = get_current_output() multi_out = get_multi_device(output) FNULL = open(os.devnull, "w") GRACENOTE_USER_ID = pygn.register(config["GRACENOTE_WEB_ID"]) if subprocess.call(["SwitchAudioSource", "-s", multi_out], stdout=FNULL, stderr=FNULL) == 0: while True: length = RECORD_SECONDS match = False attempts = 0 while not match and attempts <= 2: input_audio = record_audio(get_soundflower_index(), FORMAT, CHANNELS, RATE, CHUNK, length) try: write_file(input_audio, COMPLETE_NAME, FORMAT, CHANNELS, RATE) except IOError: log("Error writing the sound file.") resp = query_gracenote(COMPLETE_NAME) if resp["result"] is None: log("The track was not identified.") length += 3 attempts += 1 if attempts <= 2: log("Retrying...") else: match = True if match: match_response = json.dumps(resp["result"], indent=4, separators=("", " - "), ensure_ascii=False).encode("utf8") artist = resp["result"]["artist"] track = resp["result"]["track"] # remove all gifs in directory empty_gif_folder() # fetch gifs fetch_gif(artist, GIF_LIMIT) fetch_gif(track, GIF_LIMIT) moods = query_mood(GRACENOTE_USER_ID, artist, track) fetch_gif(moods[0], GIF_LIMIT) fetch_gif(moods[1], GIF_LIMIT) print("Waiting another "+str(RECORD_INTERVAL_MIN)+" minutes before guessing the song...") time.sleep(RECORD_INTERVAL_MIN * 60) else: raise RuntimeError("Couldn't switch to multi-output device.") p.terminate() os.remove(COMPLETE_NAME) if subprocess.call(["SwitchAudioSource", "-s", output], stdout=FNULL, stderr=FNULL) == 0: return else: raise RuntimeError("Couldn't switch back to output.")
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 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 GracenoteCrawler(path, queue): threadCrawlerLock.acquire(0) filename = os.path.basename(path) filename = os.path.splitext(filename)[0].lower() keywords = filename.split(' - ')[0] keywords = keywords.split('pres')[0] keywords = keywords.split('feat')[0] keywords = keywords.split('with')[0] keywords = keywords.split('and')[0] artist = keywords clientID = '104448-DA1673C7F933E828A270DD9EE58C4B8B' userID = pygn.register(clientID) metadata = pygn.searchArtist(clientID, userID, artist) uri = metadata['artist_image_url'] artwork = urllib.urlopen(uri).read() metadata['artist_image'] = artwork uri = metadata['album_art_url'] artwork = urllib.urlopen(uri).read() metadata['album_art'] = artwork queue.put(metadata) # collected = gc.collect() gc.collect()
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))
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 register(): gracenoteClientID = "58624-530E5E2D845DB16CB7D3A258CCCD5E07" gracenoteUserID = pygn.register(gracenoteClientID) return gracenoteUserID
def register(self): """ Registers with Gracenote api to get userID stores registeredID in instance var """ self.registered_id = pygn.register(self.clientID)
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)
from robobrowser import RoboBrowser import requests, pickle, json, urllib, urllib2, HTMLParser, re, os, sys, urlparse import pygn, show import config current_client_id = config.vk_app_client_id current_client_secret = config.vk_app_client_secret # file, where auth data is saved AUTH_FILE = '.auth_data' # chars to exclude from filename. Not used. FORBIDDEN_CHARS = '/\\\?%*:|"<>!' theAudioDBUrl = 'http://www.theaudiodb.com/api/v1/json/' + config.audioDBKey + '/searchtrack.php?{end}' lastFMUrl = 'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=' + config.lastFMKey + '&format=json&{end}' gracenote_client_id = config.gracenoteAPIKey gracenote_user_id = pygn.register(gracenote_client_id) class VKUrlOpener(urllib.FancyURLopener): version = config.vk_app_useragent urllib._urlopener = VKUrlOpener() def utf8_urlencode(params): # problem: u.urlencode(params.items()) is not unicode-safe. Must encode all params strings as utf8 first. # UTF-8 encodes all the keys and values in params dictionary for k, v in params.items(): # TRY urllib.unquote_plus(artist.encode('utf-8')).decode('utf-8') if type(v) in (int, long, float):
import os, sys import bcrypt, eyed3, uuid, pygn from datetime import datetime from peewee import * from playhouse.sqlite_ext import SqliteExtDatabase, FTSModel from gravatar import Gravatar db = SqliteExtDatabase("juicebox.db", threadlocals=True) MUSIC_DIR = "data/music" MD5SUM = "md5 -q" if sys.platform == "darwin" else "md5sum" GN_CLI = "3392512-77AC0BD72360CA0653409F31B97412CF" GN_USR = pygn.register(GN_CLI) class BModel(Model): SEARCHABLE = False class Meta: database = db def get_search_model(self): return None def save(self, *args, **kwargs): id = Model.save(self, *args, **kwargs) if self.SEARCHABLE: search_model = self.get_search_model() if bool(self._get_pk_value()):
import pygn import spotipy import spotipy.util as util import config import pandas as pd import sys from time import time #This program takes all of a user's saved songs and writes them to a CSV. It also catalogs genre from Gracenote and various features from Spotify. scope = 'user-library-read' clientID = config.pygn_client_id #pygn api (for Gracenote) userID = pygn.register(clientID) if len(sys.argv) > 1: #check for username input username = sys.argv[1] else: print("Usage: {} username".format(sys.argv[1])) sys.exit() client_id = config.spotipy_client_id #spotipy api client_secret = config.spotipy_secret_id redirect_uri = 'http://localhost/' token = util.prompt_for_user_token(username, scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri) #get token song_info = pd.DataFrame(columns=[
import datetime import spotipy import spotipy.util as util import pygn from time import sleep from fuzzywuzzy import fuzz token = util.prompt_for_user_token('', client_id='', client_secret='', redirect_uri='') spotify = spotipy.Spotify(auth=token) gracenote_clientID = '' gracenote_userID = pygn.register(gracenote_clientID) dt_fmt = '%Y-%m-%d' # The following code creates tables to store song data for use in the playlist application. # A PostreSQL database must exist with the specified 'database_name'. Tables are initialized # based on pickled sample tables, and data inserted through queries to the Spotify # and Gracenote APIs. database_name = '' engine = create_engine('postgresql://*****:*****@...' + database_name) artist_songs_table_sample = pickle.load( open('artist_songs_table_sample.p', 'r')) gracenote_song_data_table_sample = pickle.load( open('gracenote_song_data_table_sample.p', 'r')) spotify_song_data_table_sample = pickle.load(
host = 'http://127.0.0.1:5000/' os.environ["SPOTIPY_CLIENT_ID"] = sp_client_ID os.environ["SPOTIPY_CLIENT_SECRET"] = sp_client_secret os.environ["SPOTIPY_REDIRECT_URI"] = host username = "******" scope = 'playlist-modify-public' util.prompt_for_user_token(username=username, scope=scope, client_id=sp_client_ID, client_secret=sp_client_secret, redirect_uri=host) token = util.prompt_for_user_token(username, scope) client_ID = '170259781-06F33892AE9B17F95EE25A24C4593C70' user_ID = pygn.register(client_ID) track_titles = [] artist_list = [] result = pygn.createRadio(client_ID, user_ID, mood='42947', popularity='1000', similarity='1000', count='10') jsonData = json.dumps(result, sort_keys=True, indent=4) jsonObject = json.loads(jsonData) i = 0 while (i < 9):
import pygn 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 exit(0) userID = pygn.register(clientID) print userID
#!/usr/bin/env python3 from argparse import ArgumentParser, RawDescriptionHelpFormatter from mutagen import id3 from os import listdir, mkdir, path, rename, rmdir from pygn import register, search # https://raw.githubusercontent.com/cweichen/pygn/master/pygn.py from sys import argv from textwrap import dedent from urllib import error, request clientID = '702337754-900975F20663BD0B36A073B2E463DE14' userID = register(clientID) relate = {'ACDC': 'AC/DC'} forbidden = ['Live'] genres = [ 'Indie Rock', 'Acid Rock', 'Trance', 'Reggae', 'Grunge', 'New Romantic', 'Hard Rock', 'Southern Rap', 'Emo & Hardcore', 'Pop Punk', 'Rap Comedy', 'Heavy Metal', 'Rock', 'Classical', 'Brazilian Pop', 'European Traditional', 'Samba', 'Industrial', 'Other', 'Synth Pop', 'Alternative Rock', 'House', 'Electronica', 'Electric Blues', 'Punk', 'Urban', 'Old School Punk', 'Western Pop', 'Classic R&B', 'Classic Soul', 'Jazz', 'New Wave Rock', 'Brit Pop', 'Folk', 'Pop Electronica', 'Contemporary R&B', 'European Pop', 'Progressive House', 'Western Pop', 'New Wave Pop', 'East Coast Rap', 'Gangsta Rap', 'West Coast', 'Garage Rock Revival', 'Funk', 'Alternative', 'Midwestern Rap', 'Funk Metal', 'Lounge', 'Brit Rock', 'Classic Country', 'Caribbean Pop', 'African Pop', 'Goth', 'Ska Revival', 'Rockabilly Revival', 'Southern African', 'Pop' ]