def test_force_no_requests_session(self): with_no_session = spotipy.Spotify( client_credentials_manager=SpotifyClientCredentials(), requests_session=False) self.assertNotIsInstance(with_no_session._session, requests.Session) user = with_no_session.user(user="******") self.assertEqual(user["uri"], "spotify:user:akx")
def related(uri): auth_man = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) sp = spotipy.Spotify(auth_manager=auth_man) #artist = sp.artist(uri) return sp.artist_related_artists(uri)
def get_track(track_id: str = None): """Get the link to the specified track.""" spotify = spotipy.Spotify( client_credentials_manager=SpotifyClientCredentials( client_id=SPOTIFY_CLIENT, client_secret=SPOTIFY_SECRET)) results = spotify.track(track_id=track_id) return results['external_urls']['spotify']
def playlists(spotify_user, user): auth_man = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) sp = spotipy.Spotify(auth_manager=auth_man) playlists = sp.user_playlists(spotify_user) return (playlists, sp)
def test_force_no_requests_session(self): from requests import Session with_no_session = spotipy.Spotify( client_credentials_manager=SpotifyClientCredentials(), requests_session=False) self.assertFalse(isinstance(with_no_session._session, Session)) self.assertTrue( with_no_session.user(user="******")["uri"] == "spotify:user:akx")
def test_search_timeout(self): client_credentials_manager = SpotifyClientCredentials() sp = spotipy.Spotify(requests_timeout=0.01, client_credentials_manager=client_credentials_manager) # depending on the timing or bandwidth, this raises a timeout or connection error" self.assertRaises((requests.exceptions.Timeout, requests.exceptions.ConnectionError), lambda: sp.search(q='my*', type='track'))
def get_track_from_spotify(id, country: str = "US"): """ Lookup the metadata by using the ID on spotify """ spotify = Spotify(auth_manager=SpotifyClientCredentials( client_id=CLIENT_ID, client_secret=CLIENT_SECRET)) track = spotify.track(id, market=country) return SpotifySong(track)
def test_custom_requests_session(self): sess = requests.Session() sess.headers["user-agent"] = "spotipy-test" with_custom_session = spotipy.Spotify( client_credentials_manager=SpotifyClientCredentials(), requests_session=sess) self.assertTrue( with_custom_session.user(user="******")["uri"] == "spotify:user:akx") sess.close()
def register_spotify_client(cls, cache_handler: Optional[CacheHandler] = None ) -> Spotify: if not cache_handler: cache_handler = InMemoryCacheHandler() client_credentials_manager = SpotifyClientCredentials( cache_handler=cache_handler) cls._spotify_client = Spotify( client_credentials_manager=client_credentials_manager) return cls._spotify_client
def setUpClass(self): missing = list(filter(lambda var: not os.getenv(CCEV[var]), CCEV)) if missing: raise Exception(('Please set the client credentials for the test ' 'the following environment variables: {}').format( CCEV.values())) self.username = os.getenv(CCEV['client_username']) self.scope = 'user-library-read' self.token = prompt_for_user_token(self.username, scope=self.scope) self.spotify = Spotify( client_credentials_manager=SpotifyClientCredentials())
def test_search_timeout(self): client_credentials_manager = SpotifyClientCredentials() sp = spotipy.Spotify( client_credentials_manager=client_credentials_manager, requests_timeout=.01) try: sp.search(q='my*', type='track') self.assertTrue(False, 'unexpected search timeout') except requests.exceptions.Timeout: self.assertTrue(True, 'expected search timeout')
def test_max_retries_reached(self): spotify_no_retry = Spotify( client_credentials_manager=SpotifyClientCredentials(), retries=0) i = 0 while i < 100: try: spotify_no_retry.search(q='foo') except spotipy.exceptions.SpotifyException as e: self.assertIsInstance(e, spotipy.exceptions.SpotifyException) return i += 1 self.fail()
def __init__(self, bot): self.bot = bot self.yt_rx = re.compile( r"(https?\:\/\/)?(www\.youtube\.com|youtu\.be)\/.+") # check if both spotify id and secret are not none if not None in (Config.SPOTIFY_ID, Config.SPOTIFY_SECRET): self.sp = spotipy.Spotify( client_credentials_manager=SpotifyClientCredentials( Config.SPOTIFY_ID, Config.SPOTIFY_SECRET)) else: self.sp = None self.deezer = deezer.Client()
def test_max_retries_reached_get(self): spotify_no_retry = Spotify( client_credentials_manager=SpotifyClientCredentials(), retries=0) i = 0 while i < 100: try: spotify_no_retry.search(q='foo') except SpotifyException as e: self.assertIsInstance(e, SpotifyException) self.assertEqual(e.http_status, 429) return i += 1 self.fail()
def __init__(self, user_id=None): # getting spotify credentials if user_id: self.spotify = self.get_sp(user_id) else: # getting spotipy setup with open(dir_path + "/config.json", 'r') as config_file: config = json.load(config_file) # getting spotipy setup client_credentials_manager = SpotifyClientCredentials( client_id=config['client_id'], client_secret=config['client_secret']) self.spotify = spotipy.Spotify( client_credentials_manager=client_credentials_manager)
def make_auth(self): # TODO : 커스텀 CLIENT_ID / CLIENT_SECRET 사용 가능하게 """ CONFIG.py의 CLIENT_ID와 CLIENT_SECRET을 이용하여 인증합니다. Returns: 성공적으로 인증하면 spotipy.Spotify를 리턴합니다. """ cred = SpotifyClientCredentials( client_id=CONFIG.CLIENT_ID, client_secret=CONFIG.CLIENT_SECRET, requests_timeout=5, ) sp = Spotify(client_credentials_manager=cred) self.spoti = sp
def login(self, username, client_id, client_secret, redirect_uri): token = prompt_for_user_token( username=username, scope=SCOPE, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, ) client_credentials_manager = SpotifyClientCredentials( client_id=client_id, client_secret=client_secret) spotify = Spotify( auth=token, client_credentials_manager=client_credentials_manager) spotify.trace = False self.spotify = spotify
def get_sp(self, user_id): with open(dir_path + "/config.json", 'r') as config_file: config = json.load(config_file) # getting spotipy setup client_credentials_manager = SpotifyClientCredentials( client_id=config['client_id'], client_secret=config['client_secret']) scope = 'playlist-modify-public playlist-modify-private user-library-read user-library-modify user-read-playback-state user-modify-playback-state user-read-currently-playing' token = util.prompt_for_user_token( 'spotify:user:'******'client_id'], client_secret=config['client_secret'], redirect_uri=config['redirect_uri']) if token: sp = spotipy.Spotify( client_credentials_manager=client_credentials_manager, auth=token) sp.trace = False else: sys.exit() return sp
def search_song(query, country: str = "US", limit: int = 25) -> List[SpotifySong]: """ Search the song using the API through spotipy and accordingly return the results. """ spotify = Spotify(auth_manager=SpotifyClientCredentials( client_id=CLIENT_ID, client_secret=CLIENT_SECRET)) response = spotify.search(f"track:{query}", limit=limit, type="track", market=country) items = [SpotifySong(item) for item in response["tracks"]["items"]] return items
def find_track_ids(tracks): sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials()) for track in tqdm.tqdm(tracks): name = track.name artist = track.artist album = track.album track_id = search_track_id(album, artist, name, sp) if len(track_id) == 0: track_id = search_track_id(album, '', name, sp) track.track_id = track_id found_tracks = [track for track in tracks if len(track.track_id) > 0] missing_tracks = [track for track in tracks if len(track.track_id) == 0] return found_tracks, missing_tracks
def setUpClass(self): self.spotify = Spotify( client_credentials_manager=SpotifyClientCredentials()) self.spotify.trace = False
def __init__(self): self.spotify_dir() self.sp = Spotify(auth_manager=SpotifyClientCredentials())
async def spotify(self, ctx: Context, url: str = None, type_: str = None): if not url: return await ctx.send(embed=ErrorEmbed( author=ctx.author, title=':x: Missing Spotify link or ID' )) elif not type_: try: type_ = url.split('&')[0].split('?')[0].split('/')[3] except IndexError: pass if type_ == 'user': return await ctx.send(embed=ErrorEmbed( author=ctx.author, title=':x: User profiles are not supported', description='...yet?' )) elif type_ not in ['track', 'album', 'artist', 'playlist']: return await ctx.send(embed=ErrorEmbed( author=ctx.author, title=':x: What is this?', description='Is it `track`, `album`, `artist` or `playlist`?' )) if url.startswith(('http://open.spotify.com', 'https://open.spotify.com')): url = url.split('?')[0].split('/')[-1] type_ = type_.lower() try: sp = Spotify(auth_manager=SpotifyClientCredentials( client_id=spotify_client_id(), client_secret=spotify_client_secret() )) except SpotifyOauthError: sp = None if not sp: return await ctx.send(embed=ErrorEmbed( author=ctx.author, title=':x: Unable to connect to Spotify!' )) result = error_code = None em = SuccessEmbed( author=ctx.author ).set_author( name=f'{ctx.author.display_name} shared a{"n" if type_[0] == "a" else ""} {type_}:', icon_url=ctx.author.avatar_url ) if type_ == 'track': try: result = sp.track(url) except SpotifyException as e: error_code = int(e.http_status) elif type_ == 'album': try: result = sp.album(url) except SpotifyException as e: error_code = int(e.http_status) elif type_ == 'playlist': try: result = sp.playlist(url) except SpotifyException as e: error_code = int(e.http_status) elif type_ == 'artist': try: result = sp.artist(url) except SpotifyException as e: error_code = int(e.http_status) else: return await ctx.send(embed=ErrorEmbed( author=ctx.author, title=':x: Unknown object type', description='Check `>help` for valid object types.' )) if error_code: if error_code == 400: d = 'Invalid ID or URL.' elif error_code == 429: d = 'Unable to do that now, please try again in 5 minutes.' elif str(error_code).startswith('5'): d = 'Spotify is not responding.' else: d = 'Unknown error. Please try again in a few minutes and please make sure URL or ID is valid.' return await ctx.send(embed=ErrorEmbed( author=ctx.author, title=':x: An error occurred!', description=d )) elif not result: return await ctx.send(embed=ErrorEmbed( author=ctx.author, title=':x: Unable to find anything on Spotify', description='Probably URL/ID is wrong.' )) title = result['name'] # Artists if type_ not in ['artist', 'playlist']: artists = list(map(lambda x: [x['name'], x['external_urls']['spotify']], result['artists'])) elif type_ in ['playlist']: artists = [[result['owner']['display_name'], result['owner']['external_urls']['spotify']]] else: artists = None # Released if type_ == 'track': released = result['album']['release_date'] elif type_ == 'album': released = result['release_date'] else: released = None # Genres if type_ in ['artist', 'album']: genres = ', '.join(result['genres']) or 'Not specified' else: genres = None ex_url = result['external_urls']['spotify'] thumbnail = result['album']['images'][0]['url'] if type_ == 'track' else result['images'][0]['url'] # Title if title: em.add_field( name='Name' if type_ in ['artist'] else 'Title', value=title ) # Author / Artist(s) if artists: em.add_field( name='Author' if type_ == 'playlist' else 'Artist' if len(artists) == 1 else 'Artists', value=', '.join(map(lambda x: f'[{x[0]}]({x[1]} "Check it on Spotify")', artists)) ) # Followers if type_ in ['artist', 'playlist']: em.add_field( name='Followers', value=result['followers']['total'] ) # Album if type_ == 'track': em.add_field( name='Album', value=f'[{result["name"]}]({result["album"]["external_urls"]["spotify"]} "Check it on Spotify")' ) # Released if released: em.add_field( name='Released', value=released ) # Tracks if type_ in ['playlist', 'album']: em.add_field( name='Tracks', value=str(result['tracks']['total']) ) # Genres if genres: em.add_field( name='Genres', value=genres ) # Popularity if type_ in ['track', 'artist', 'album']: em.add_field( name='Popularity', value=str(result['popularity']) ) # Label elif type_ == 'album': em.add_field( name='Label', value=result['label'] ) # Spotify link if ex_url: em.add_field( name='Spotify', value=ex_url, inline=False ) # YouTube link if type_ == 'track': # Lookup YouTube query = '{} {}'.format(result['name'], ' '.join(map(lambda x: x['name'], result['artists']))) yt = SearchVideos( query, mode='dict', max_results=1 ).result() # noinspection PyTypeChecker yt = yt['search_result'][0]['link'] if yt else None em.add_field( name='YouTube', value=yt, inline=False ) # Thumbnail if thumbnail: em.set_thumbnail( url=thumbnail ) await ctx.send(embed=em) try: await ctx.message.delete() except Forbidden or NotFound or HTTPException: pass
def get_client_sp(): """ Returnour client credential. This can only be used to retrieve general info WITHOUT user-specific info.""" client_credentials_manager = SpotifyClientCredentials() return spotipy.Spotify(client_credentials_manager=client_credentials_manager)
import sys import pandas as pd import spotipy from spotipy import SpotifyClientCredentials from sklearn import preprocessing import numpy as np import seaborn as sn from IPython.display import IFrame from matplotlib.pyplot import figure try: client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret) sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) except: print("Spotipy credentials not found.") # Functions: # - get_features(trackid) # Returns audio features and track information def get_features(tracks): allTrackFeatures = [] for _, album in enumerate(tracks): for _, name in enumerate(tracks[album]): trackFeatures = sp.audio_features(tracks[album][name])[0] trackInformation = sp.track(tracks[album][name]) trackFeatures['popularity'] = trackInformation['popularity'] trackFeatures['track_number'] = trackInformation['track_number']
def main(): parser = ArgumentParser() parser.add_argument('post_data_dirs', nargs='+') parser.add_argument('--music_API_auth_data', default='../../data/culture_metadata/spotify_auth.csv') parser.add_argument('--out_dir', default='../../data/culture_metadata/') args = vars(parser.parse_args()) logging_file = '../../output/extract_spotify_musicians_from_posts.txt' if (os.path.exists(logging_file)): os.remove(logging_file) logging.basicConfig( filename=logging_file, level=logging.INFO, format= '%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') ## load post data ## and extract URLs post_data_dirs = args['post_data_dirs'] file_matcher = re.compile('tweets.gz') music_URL_matcher = re.compile(r'(?<=spotify\.com/track/)[a-zA-Z0-9]+') combined_URL_matches = set() for post_data_dir in post_data_dirs: post_data_files = list( filter(lambda x: file_matcher.search(x) is not None, os.listdir(post_data_dir))) post_data_files = list( map(lambda x: os.path.join(post_data_dir, x), post_data_files)) for post_data_file in post_data_files: URL_matches = extract_URL_matches(post_data_file, music_URL_matcher) combined_URL_matches.update(set(URL_matches)) combined_URL_matches = list(combined_URL_matches) print('%d total URL matches' % (len(combined_URL_matches))) # extract music-related links track_IDs = set() for post_URL in combined_URL_matches: music_URL_match = music_URL_matcher.search(post_URL) track_ID = music_URL_match.group(0) track_IDs.update([track_ID]) track_IDs = list(track_IDs) # clean up IDs bad_ID_matcher = re.compile('http[s:]{,}$') track_IDs = list(map(lambda x: bad_ID_matcher.sub('', x), track_IDs)) print('%d music track IDs' % (len(track_IDs))) ## mine track data # set up API music_API_auth_data_file = args['music_API_auth_data'] spotify_auth_data = pd.read_csv(music_API_auth_data_file, sep=',', header=None) spotify_auth_data = dict( zip(spotify_auth_data.loc[:, 0], spotify_auth_data.loc[:, 1])) spotify_creds = SpotifyClientCredentials( client_id=spotify_auth_data['client_id'], client_secret=spotify_auth_data['client_secret']) spotify_api = Spotify(client_credentials_manager=spotify_creds) out_dir = args['out_dir'] track_data_file = os.path.join(out_dir, 'spotify_track_data.tsv') if (os.path.exists(track_data_file)): old_track_data = pd.read_csv(track_data_file, sep='\t', index_col=False) old_track_data.fillna('', inplace=True) old_track_data = old_track_data[old_track_data.loc[:, 'artist_id'] != ''] for i in old_track_data.loc[:, 'artist_id'].values: try: literal_eval(i) except Exception as e: print('bad artist IDs %s' % (i)) break old_track_data = old_track_data.assign( **{ 'artist_id': old_track_data.loc[:, 'artist_id'].apply( lambda x: literal_eval(x)) }) old_track_IDs = old_track_data.loc[:, 'id'].values track_IDs = list(set(track_IDs) - set(old_track_IDs)) else: old_track_data = [] track_data = list( map(lambda x: collect_track_data(x, spotify_api), track_IDs)) print('%d track IDs' % (len(track_data))) track_data = pd.concat(list(map(lambda x: pd.Series(x), track_data)), axis=1).transpose() if (len(old_track_data) > 0): track_data = pd.concat([old_track_data, track_data], axis=0) # write to file track_data.to_csv(track_data_file, sep='\t', index=False) ## mine musician data # get artist IDs track_data = track_data[track_data.loc[:, 'artist_id'].apply( lambda x: type(x) is list)] flat_musician_IDs = list( reduce(lambda x, y: x + y, track_data.loc[:, 'artist_id'].values)) musician_IDs = list(set(flat_musician_IDs)) print('%d musician IDs' % (len(musician_IDs))) musician_data_file = os.path.join(out_dir, 'spotify_musician_data.tsv') if (os.path.exists(musician_data_file)): old_musician_data = pd.read_csv(musician_data_file, sep='\t', index_col=False) old_musician_IDs = set(old_musician_data.loc[:, 'artist_id'].unique()) musician_IDs = list(set(musician_IDs) - old_musician_IDs) else: old_musician_data = [] musician_data = list( map(lambda x: collect_artist_data(x, spotify_api), musician_IDs)) musician_data = pd.concat(list(map(lambda x: pd.Series(x), musician_data)), axis=1).transpose() if (len(old_musician_data) > 0): musician_data = pd.concat([old_musician_data, musician_data], axis=0) # write to file musician_data.to_csv(musician_data_file, sep='\t', index=False)
import spotipy import time from IPython.core.display import clear_output from spotipy import SpotifyClientCredentials, util client_id='346f54daa4fb4995a0f061fc114c1f9b' client_secret='e262f6807d1546e1a42353e6bcb5936f' redirect_uri= 'http://localhost:8888/callback' username = '******' scope = 'playlist-modify-public' #Credentials to access the Spotify Music Data manager = SpotifyClientCredentials(client_id,client_secret) sp = spotipy.Spotify(client_credentials_manager=manager) #Credentials to access to the Spotify User's Playlist, Favorite Songs, etc. token = util.prompt_for_user_token(username,scope,client_id,client_secret,redirect_uri) print(token) spt = spotipy.Spotify(auth=token) print(spt) def get_albums_id(ids): album_ids = [] results = sp.artist_albums(ids) for album in results['items']: album_ids.append(album['id']) return album_ids def get_album_songs_id(ids):
def connect_to_spotify(): client_credentials_manager = SpotifyClientCredentials( client_id=client_id, client_secret=client_secret) sp = spotipy.Spotify( client_credentials_manager=client_credentials_manager) return sp
import logging from typing import List, Union import spotipy from dotenv import load_dotenv from fastapi import FastAPI, status, Query from fastapi.responses import JSONResponse from fastapi.encoders import jsonable_encoder from spotipy import SpotifyClientCredentials from spotifyapi.models import Track, SimpleTrack, convert_track_to_simple_track load_dotenv() client_credentials_manager = SpotifyClientCredentials() sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) app = FastAPI() @app.get( "/search", response_model=Union[Track, SimpleTrack], responses={ 404: { "description": "No track found for this query", }, 200: { "description": "Found a track for this query" }, },
import pandas as pd import numpy as np import spotipy from spotipy import SpotifyClientCredentials import os import pickle import swifter from tqdm import tqdm # Change this to the location of your project directory data_path = '/home/rajsuryan/Desktop/PopEvol_1960-2020/Data/' #Setup API credentials client_credentials_manager = SpotifyClientCredentials( client_id="8b6b3fab9bb04c17ab4aa187c5dd826b", client_secret="833badffedd8400293eb09d35c096455") sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager, requests_timeout=20, retries=3) def get_ID(track, artist): #Search for Spofity song ID songs = sp.search(q='track:' + track + ' ' + 'artist:' + artist + '*', type='track') results = songs['tracks']['items'] if len(results) == 0: return (0) else: track = results[0] if results[0]["artists"][0]['name'].lower() == artist.lower():