Last active
May 3, 2020 11:07
-
-
Save Brittt94/d134814da299a90ec17a9dae7dc1228c to your computer and use it in GitHub Desktop.
Graded Exercise 4
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
import json | |
import requests | |
import pandas as pd | |
from urllib.request import Request, urlopen | |
import ssl | |
from bs4 import BeautifulSoup | |
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 BQA2k1n-q8cI9I65AYl-q5IP__NjBxDXTLquLmeJcWQS2n8a928pk1Tg_TRFfIeW7eB492xNBe-NL_2ArSnql1HvN--O9dACl5j5_7TEpHTCh0c7AAKRajFffffuFcwDLzA20mwVZio', | |
} | |
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']) | |
imgfile = open(json_data['src'],'wb') # Create a new, empty picture file | |
imgfile.write(urlopen(imgurl).read()) # Write picture information into empty file | |
imgfile.close() # Close file | |
print('Picture downloaded...') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment