# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import spotipy
import spotipy.oauth2
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
import pandas as pd
import numpy as np
import statistics

import time
# -

dat = pd.read_csv('worldwide.csv')

dat.head(2)

# +
CLIENT_ID = '23888f5deee6452db5d78bd2d1091da5'
CLIENT_SECRET = 'f6ecea7a89d24b02b2c969745b88a9db'

client_credentials_manager = SpotifyClientCredentials(CLIENT_ID, CLIENT_SECRET)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# -

dat['URL'][0]

blah = sp.audio_analysis(dat['URL'][0]) #testing dummy

blah.keys() #works

blah = sp.audio_features(dat['URL'][0])

blah[0]

len(list(set(dat['URL']))) #get total non duplicates

# +
sp_feature_dict = {} #keys will be urls, values will be the sp.audio_analysis
seen_urls = set()

for i in range(0, len(dat)):
    curr_url = dat['URL'][i]
    if curr_url not in seen_urls:
        seen_urls.add(curr_url)
        sp_feature_dict[curr_url] = sp.audio_features(curr_url)
    if i % 100 == 0:
        print(len(dat) - i, 'rows left')
# -

fixed_feature_dict = sp_feature_dict.copy()

for key in fixed_feature_dict.keys():
    fixed_feature_dict[key] = fixed_feature_dict[key][0]

list(fixed_feature_dict.values())[0] #checking to see if fixed

feature_df

feature_df = feature_df.transpose()

feature_df['URL'] = feature_df.index

print(feature_df.shape)
feature_df.head(2)

dat_copy = dat.copy()

dat_copy.head(2)

blah = pd.merge(dat_copy, feature_df, how = 'left', on = 'URL')

print(blah.shape)
print(blah.shape[0] == dat_copy.shape[0], blah.shape[1] == (dat_copy.shape[1] + feature_df.shape[1] - 1))

blah.to_csv('rescrape_500mb_features.csv')



StarcoderdataJupyterScriptsDedupFiltered