<jupyter_start><jupyter_text>Credenciales<jupyter_code># https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/
# Pide una autorización 'Basic' con: <client_id + : + client_secret> en base64. 
import base64


client_id = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
client_secret = "<KEY>"
usr_pass = client_id + ':' + client_secret
auth_b64 = base64.b64encode(usr_pass.encode()).decode()


url = "https://accounts.spotify.com/api/token"

payload='grant_type=client_credentials'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Authorization': 'Basic ' + auth_b64
}

response = requests.request("POST", url, headers=headers, data=payload)

credentials = response.json()
credentials<jupyter_output><empty_output><jupyter_text>Utilizando la API<jupyter_code>headers = {
  'Content-Type': 'application/json',
  'Authorization': credentials['token_type'] + ' ' + credentials['access_token']
}

file_name = "artist_sample_test_2"
URL = "https://api.spotify.com/v1/artists?ids="

# se guardan las respuestas en un .json
with open('../data/{}.json'.format(file_name), 'w') as f:
    f.write('[ \n')
    for idx, query in enumerate(tqdm(querys), start=1):
        try:
            response = requests.request("GET", URL + query, headers=headers)
            json.dump(response.json(), f, indent=2)
            if idx < len(querys):
                f.write(', \n')
        except Exception as err:
            console.log('Error: ', err)
    f.write('] \n')
# Ojo con el path.
with open('../data/{}.json'.format(file_name), 'r') as f:
    json_file = json.load(f)
df_spotify = pd.DataFrame(json_file)
df_spotify
df_explode = df_spotify.explode('artists').reset_index()
df_explode
df_json = df_explode.to_json(orient='records')
df_final = pd.json_normalize(json.loads(df_json), meta=['artists'])
df_final<jupyter_output><empty_output><jupyter_text>Serialización<jupyter_code>df_final.to_csv("../data/{}.csv.gz".format(file_name), 
           index=False, 
           compression="gzip")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1574074074074074, 'fraction_numerical': 0.007797270955165692, 'mean_word_length': 4.563685636856369, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '24319', 'license': 'CECILL-B', 'n_tokens_mistral': 733, 'n_tokens_neox': 720, 'n_words': 117, 'path': 'sandbox/API Spotify.ipynb', 'repo_name': 'alcazar90/CC5205-3-proyecto', 'size': '2076'}