-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
115 lines (85 loc) · 3.3 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth
def get_spotipy_client():
auth_manager = SpotifyOAuth(
client_id=os.environ["SPOTIPY_CLIENT_ID"],
client_secret=os.environ["SPOTIPY_CLIENT_SECRET"],
redirect_uri="https://example.com/callback",
# scope="user-library-read",
scope="playlist-modify-private",
)
sp = spotipy.Spotify(auth_manager=auth_manager)
return sp
sp = get_spotipy_client()
class Playlist:
# NOTE: may want to change this to just take the playlist id
def __init__(self, js, with_bpm=False):
self.js = js
self.name = js["name"]
self.id = js["id"]
self.uri = js["uri"]
self.tracks = self.get_tracks(with_bpm)
def get_tracks(self, with_bpm=False):
raw_tracks = self.js["tracks"]["items"]
tracks = [Track(rt["track"]) for rt in raw_tracks]
bpms = get_bpms_from_tracks(tracks)
if with_bpm:
for track, bpm in zip(tracks, bpms):
track.bpm = bpm
return tracks
def __str__(self):
return self.name
def get_tracks_in_bpm_range(self, min_bpm, max_bpm):
return get_tracks_in_bpm_range(self.tracks, min_bpm, max_bpm)
class Track:
def __init__(self, js, with_bpm=False):
self.js = js
self.name = js["name"]
self.id = js["id"]
self.uri = js["uri"]
if with_bpm:
self.bpm = self.get_bpm()
def get_bpm(self):
afs = sp.audio_features([self.id])
assert len(afs) == 1
return afs[0]["tempo"]
def __str__(self):
return self.name
def get_bpms_from_tracks(tracks: list[Track]):
if not tracks:
return []
track_ids = [track.id for track in tracks]
audio_features = sp.audio_features(track_ids)
bpms = [float(af["tempo"]) for af in audio_features]
assert len(track_ids) == len(bpms)
return bpms
def get_tracks_in_bpm_range(tracks, min_bpm, max_bpm):
"""Return only the tracks within the bpm range (inclusive)."""
# filter function
def track_is_in_bpm_range(track):
return min_bpm <= track.bpm <= max_bpm
tracks_in_bpm_range = filter(track_is_in_bpm_range, tracks)
return list(tracks_in_bpm_range)
def get_playlist_from_id(playlist_id):
js = sp.playlist(playlist_id)
return Playlist(js, with_bpm=True)
def add_bpm_subset_to_new_playlist(
source_playlist_id, target_playlist_id, min_bpm, max_bpm
):
# take a playlist
source_playlist = get_playlist_from_id(source_playlist_id)
# extract all the songs within a bpm range
tracks_in_range = get_tracks_in_bpm_range(source_playlist.tracks, min_bpm, max_bpm)
track_ids_in_range = [track.id for track in tracks_in_range]
# add those songs to another playlist
sp.playlist_add_items(target_playlist_id, track_ids_in_range)
def list_my_playlist_names_and_ids():
"""Return a list of playlists, their name and id."""
items = sp.current_user_playlists()["items"]
return [{k: item[k] for k in ["name", "id"]} for item in items]
def delete_all_tracks_in_playlist(playlist_id):
js = sp.playlist(playlist_id)
target_playlist = Playlist(js)
target_playlist_track_ids = [t.id for t in target_playlist.tracks]
sp.playlist_remove_all_occurrences_of_items(playlist_id, target_playlist_track_ids)