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_wMgApU... |
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
| { | |
| "endTime" : "2019–01–26 07:49", | |
| "artistName" : "Tom Waits", | |
| "trackName" : "Heartattack And Vine", | |
| "msPlayed" : 8850 | |
| } |
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 pandas as pd | |
| df = pd.DataFrame(with_features) | |
| df.to_csv('streaming_history.csv') |
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 |
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
| 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_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
| 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
| {"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
| print(token) | |
| >> T2_wi8XsUpJrbD0BHQH4vX0BrSuYV0D88sVrQ_wMgApU9hP3g17MFIGBJwy6VJAB BwJKttS11Egej9RzGDuN4U4UUTLCECxVBWSrmrTT0iomaUtuapPkMgWa_uTjYijhnHI2czgPCZItzO4KAD |