-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaylist.py
More file actions
89 lines (77 loc) · 3.13 KB
/
Playlist.py
File metadata and controls
89 lines (77 loc) · 3.13 KB
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
from datetime import date
class Playlist:
song_ids = []
audio_features = []
def __init__(self, user, spotipy, id=None, playlist_name=None):
self.user = user
self.sp = spotipy
self.id = id
if id:
self._load()
elif playlist_name:
self._get_id_by_name(playlist_name)
if not self.id:
self.create(playlist_name)
self._load()
def get_all_song_ids_and_audio_features(self):
retrieving = True
tracks = self.playlist['tracks']
if not tracks['items']: return
while retrieving:
batch_songs = tracks['items']
batch_song_ids = []
for s in batch_songs:
song_id = s['track']['id']
batch_song_ids.append(song_id)
self.song_ids.append(song_id)
afs = self.sp.audio_features(tracks=batch_song_ids)
for idx, af in enumerate(afs):
af['song_name'] = batch_songs[idx]['track']['name']
af['artist'] = batch_songs[idx]['track']['artists'][0]['name']
self.audio_features.append(af)
if tracks['next']:
tracks = self.sp.next(tracks)
else:
retrieving = False
def create(self, playlist_name):
new_playlist = self.sp.user_playlist_create(
user=self.user,
name=playlist_name,
public=True,
collaborative=False,
description=f"Last updated on: {date.today().strftime('%B %d, %Y')}"
)
self.id = new_playlist['id']
def update(self, song_id_list):
self._clear()
self._stagger_spotipy_request(self.sp.playlist_add_items, song_id_list)
self.sp.playlist_change_details(self.id, description=f"Last updated on: {date.today().strftime('%B %d, %Y')}")
def _clear(self):
self.get_all_song_ids_and_audio_features()
self._stagger_spotipy_request(self.sp.playlist_remove_all_occurrences_of_items, self.song_ids)
def _stagger_spotipy_request(self, operation, song_ids):
staggering = True
while staggering:
if len(song_ids) > 100:
operation(self.id, song_ids[:100])
song_ids = song_ids[100:]
elif len(song_ids) <= 100 and len(song_ids) > 0:
operation(self.id, song_ids)
staggering = False
else: # Song array is empty (either no songs or exactly 100, 200, 300... songs)
staggering = False
def _load(self):
self.playlist = self.sp.playlist(playlist_id=self.id)
def _get_id_by_name(self, playlist_name):
playlist_metadata = self.sp.user_playlists(self.user)
_playlists = playlist_metadata['items']
while _playlists:
for p in _playlists:
if p['name'] == playlist_name:
self.id = p['id']
return
if playlist_metadata['next']:
_playlists = self.sp.next(playlist_metadata)
else:
self.id = None
break