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
| pip install spotipy | |
| pip install requests | |
| pip install pandas |
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 ast | |
| from typing import List | |
| from os import listdir | |
| def get_streamings(path: str = 'MyData') -> List[dict]: | |
| files = ['MyData/' + x for x in listdir(path) | |
| if x.split('.')[0][:-1] == 'StreamingHistory'] | |
| all_streamings = [] | |
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 spotipy.util as util | |
| username = 'your-spotify-username' | |
| client_id ='your-client-id' | |
| client_secret = 'your-client-secret' | |
| redirect_uri = 'http://localhost:7777/callback' | |
| scope = 'user-read-recently-played' | |
| token = util.prompt_for_user_token(username=username, | |
| scope=scope, |
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
| print(token) | |
| >> T2_wi8XsUpJrbD0BHQH4vX0BrSuYV0D88sVrQ_wMgApU9hP3g17MFIGBJwy6VJAB BwJKttS11Egej9RzGDuN4U4UUTLCECxVBWSrmrTT0iomaUtuapPkMgWa_uTjYijhnHI2czgPCZItzO4KAD |
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
| {"access_token": some-string, | |
| "token_type": "Bearer", | |
| "expires_in": 3600, | |
| "refresh_token": another-string, | |
| "scope": "user-read-recently-played", | |
| "expires_at": 1580462935} |
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 requests | |
| def get_id(track_name: str, token: str) -> str: | |
| headers = { | |
| 'Accept': 'application/json', | |
| 'Content-Type': 'application/json', | |
| 'Authorization': f'Bearer ' + token, | |
| } | |
| params = [ | |
| ('q', track_name), | |
| ('type', 'track'), |
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
| lucy_id = get_id('Lucy', token, artist = 'The Beatles') | |
| print(lucy_id) | |
| >> '25yQPHgC35WNnnOUqFhgVR' |
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
| 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 |
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
| lucy_features = get_features(lucy_id, token) | |
| print(lucy_features) | |
| >> {'danceability': 0.311, | |
| 'energy': 0.325, | |
| 'key': 2, | |
| 'loudness': -9.042, | |
| 'mode': 1, | |
| 'speechiness': 0.0283 | |
| ...} |
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
| streamings = get_streamings() | |
| unique_tracks = list(set([streaming['trackName'] | |
| for streaming in streamings])) | |
| all_features = {} | |
| for track in unique_tracks: | |
| track_id = get_id(track, token) | |
| features = get_features(track_id, token) | |
| if features: | |
| all_features[track] = features |
OlderNewer