<jupyter_start><jupyter_text>Google Play MusicExport of music history with Google Takeout<jupyter_code>import os
os.getcwd()
SandsTitles = os.listdir('Takeout\Google Play Musique\Playlists\Sands\Titres')
SandsTitles[9]
import csv
with open('Takeout\Google Play Musique\Playlists\Sands\Titres\Bad Moon Rising.csv') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(', '.join(row))
f = open('Takeout\Google Play Musique\Playlists\Sands\Titres'+ '\\'+ SandsTitles[0])
reader = csv.reader(f)
header1 = next(reader)
header2 = next(reader)
content = next(reader)
header = header1 + header2[1:4]
header,content
import pandas as pd
header = ('title','album','artist','time','stars','played','deleted','playlist_index')
df = pd.DataFrame(columns=header)
df
import pandas as pd
df2 = pd.read_csv('Takeout\Google Play Musique\Playlists\Sands\Titres'+ '\\'+ SandsTitles[9],skiprows=2,header=None, names = header)
df.append(df2)
df2
import pandas as pd
header = ('title','album','artist','time','stars','played','deleted','playlist_index')
df = pd.DataFrame(columns=header)
for track in SandsTitles:
    # print(track)
    df = df.append(pd.read_csv('Takeout\Google Play Musique\Playlists\Sands\Titres'+ '\\'+ track,skiprows=2,header=None, names = header),ignore_index=True)
    
df
df.played.plot.bar()
df.sort_values('played',ascending = False).head()
df.sort_values('played',ascending = False).head().played.plot.bar()
Titles = os.listdir('Takeout\Google Play Musique\Titres')
import pandas as pd
header = ('title','album','artist','time','stars','played','deleted')
df = pd.DataFrame(columns=header)
for track in Titles:
    # print(track)
    df = df.append(pd.read_csv('Takeout\Google Play Musique\Titres'+ '\\'+ track,skiprows=2,header=None, names = header),ignore_index=True)
    
df
df.sort_values('played',ascending = False).head()
top100 = df.sort_values('played',ascending = False).head(100)
# top100.to_csv("top100.csv")
# top100.to_excel("top100.xlsx")
# top100.plot.bar('title','played')
import plotly.express as px
fig = px.bar(top100, x='title', y='played')
fig.show()
import pandas as pd

top100 = pd.read_csv('top100.csv',index_col=0)
top100.head()
import spotifyAPI
from secret import *

token  = spotifyAPI.get_token(clientId,clientSecret)
token
import spotifyAPI

track_id = []
for index, row in top100.iterrows():
    print(row['title'], ' - ', row['artist'])
    track_id.append(spotifyAPI.get_track_id2(row['title'], token, row['artist']))
track_id
top100.drop(columns = 'id')
top100['id'] = track_id
top100.head()
top100.to_csv('top100id.csv')
# import spotifyAPI
# tracks = list(top100['title'])
# all_features = {}
# for track in tracks:
#     track_id = spotifyAPI.get_track_id(track, token)
#     features = get_features(track_id, token)
#     if features:
#         all_features[track] = features
import spotipy
def get_features(track_id: str, token: str) -> dict:
    sp = spotipy.Spotify(auth=token)
    try:
        features = sp.audio_features([track_id])
        return features[0]
    except:
        return None
import spotifyAPI
all_features = {}
for index, row in top100.iterrows():
    track_id = spotifyAPI.get_track_id2(row['title'], token, row['artist'])
    features = get_features(track_id, token)
    if features:
        all_features[track] = features
with_features = []
for track_name, features in all_features.items():
    with_features.append({'name': track_name, **features})
import pandas as pd
df = pd.DataFrame(with_features)
df.to_csv('top100_features.csv')
df
import spotifyAPI
features = df.loc[: ,['acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'speechiness', 'valence']]
spotifyAPI.feature_plot(features)
import importlib
importlib.reload(spotifyAPI)
df.loc[0]
df.loc[[0]]
features1 = df.loc[[0]].loc[: ,['acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'speechiness', 'valence']]
features1
spotifyAPI.feature_plot(features1)
# Compare this song (in pink) 
# with overall features (in gray)
spotifyAPI.feature_plot2(features,features1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15625, 'fraction_numerical': 0.021240234375, 'mean_word_length': 5.794361525704809, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '662932', 'license': 'MIT', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1428, 'n_words': 288, 'path': 'googlemusic.ipynb', 'repo_name': 'slevin48/music', 'size': '4096'}