Skip to content

Instantly share code, notes, and snippets.

View vlad-ds's full-sized avatar

Vlad Gheorghe vlad-ds

View GitHub Profile
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:54
pip install spotipy
pip install requests
pip install pandas
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 = []
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:54
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,
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:54
print(token)
>> T2_wi8XsUpJrbD0BHQH4vX0BrSuYV0D88sVrQ_wMgApU9hP3g17MFIGBJwy6VJAB BwJKttS11Egej9RzGDuN4U4UUTLCECxVBWSrmrTT0iomaUtuapPkMgWa_uTjYijhnHI2czgPCZItzO4KAD
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:54
{"access_token": some-string,
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": another-string,
"scope": "user-read-recently-played",
"expires_at": 1580462935}
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'),
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:53
lucy_id = get_id('Lucy', token, artist = 'The Beatles')
print(lucy_id)
>> '25yQPHgC35WNnnOUqFhgVR'
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:53
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
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:53
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
...}
@vlad-ds
vlad-ds / spoty.py
Last active February 1, 2020 17:53
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