Created
March 31, 2021 08:23
-
-
Save wkdalsgh192/1f0aeacba471061e479bfbd1fad47dad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install spotipy in your command line | |
pip install spotipy --upgrade | |
# import spotipy to set up your client account | |
from spotipy.oauth2 import SpotifyClientCredentials | |
import spotipy | |
# copy your client id and secret key on your spotify dashboard | |
# set them up using spotipy client credentials object to let spotipy retrive data by your query | |
client_id="your_id" | |
client_secret = "your_secret" | |
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) | |
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager) | |
# sp.search() will collect JSON data and save the values you need through a nested loop into arrays. | |
# Note that Spotify has set the maximum offset to 10,000. | |
track_id=[] | |
artist=[] | |
title=[] | |
url=[] | |
for i in range(0,n,50): | |
results = sp.search(q='genre:' + genre, type='track',offset=i,limit=50) | |
tracks = results['tracks']['items'] | |
for track in tracks: | |
if track['preview_url'] is not None: | |
track_id.append(track['id']) | |
artist.append(track['artists'][0]['name']) | |
title.append(track['name']) | |
url.append(track['preview_url']+'.wav') | |
else: | |
pass | |
# Load Data into Pandas DataFrame | |
df_gen = pd.DataFrame({'artist':artist,'title':title,'genre':genre,'id':track_id,'url':url}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment