Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sutto/453bb73e521dcb0af5db3994ccfe94ad to your computer and use it in GitHub Desktop.
Save Sutto/453bb73e521dcb0af5db3994ccfe94ad to your computer and use it in GitHub Desktop.
Graded Exercise 4
import json
import requests
import pandas as pd
data = pd.read_csv('artists.csv',sep=';') # Reading data from csv
data = data.T.to_dict().values() # Converting dataframe into list of dictionaries
json_data = [] # Empty list to temporarily save the new data, and at the end write it into a new CSV
# Iterate through the imported data
for item in data:
print('Searching for',item['Artist'])
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer BQCEh6JKyozS6x6TwXmgnjjUgn86TxScZadaIVnuo_pAdbC4sfxDQfZrJB4R9xU1VZvK8w_8KGCvtTv-bdx38E5p-bK6Ns1PvrU2mbCvw6GW-EhAi_B7DMKJYdTqedIrLiOoHDwcmVY',
}
params = (
('q', item['Artist']), # Per iteration, the value of q = the read artist
('type', 'artist'),
)
response = requests.get('https://api.spotify.com/v1/search', headers=headers, params=params) # request the response
# The endpoint base url is supplemented by the arguments in the variables headers and params
current_json = json.loads(response.text) # convert json response to text/dict
json_data.append(current_json)
print(json_data)
# for item in json_data:
# print(item['artists']['items'][0]['images'][0]['url']) # Isolating the genre information - as a list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment