-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.py
94 lines (75 loc) · 2.69 KB
/
summary.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
import json
import random
from datetime import datetime
from typing import List, Optional
from utilities import flatten
from network import client as spotify
def save_album_history(
year: int,
allpath: Optional[str]=None,
yearpath: Optional[str]=None,
library_playlist_id: Optional[str]=None,
year_playlist_id: Optional[str]=None,
release_all: bool=False
):
albums = spotify.get_library_albums(datetime(year - 1, 12, 31, 0, 0, 0))[::-1]
def name_fmt(alb, release=False):
released = f' ({alb.released})' if release else ''
return f'{alb.added.strftime("%m-%d")} - {alb.name} by {alb.artist}{released}\n'
if allpath:
with open(allpath, "w+") as allf:
for alb in albums:
allf.write(name_fmt(alb, release=release_all))
if yearpath:
with open(yearpath, "w+") as yearf:
for alb in [a for a in albums if a.released.startswith(f"{year}")]:
yearf.write(name_fmt(alb, release=True))
if library_playlist_id:
spotify.add_playlist_tracks(
library_playlist_id,
flatten([t.tracks for t in albums])
)
if year_playlist_id:
spotify.add_playlist_tracks(
year_playlist_id,
[a.tracks[0] for a in albums if a.released.startswith(f"{year}")]
)
def create_shuffled(in_id: str, out_id: str):
ts = set()
for i in range(2):
# heads up that the playlist endpoint doesn't support offset, only tracks does
ts |= {
t['track']['uri'] for t in spotify.get(
f"https://api.spotify.com/v1/playlists/{in_id}/tracks?offset={i * 100}&limit=100"
).json()['items']
}
tracks = list(ts)
random.shuffle(tracks)
BATCH_SIZE = 100
batched = [
tracks[i:i+BATCH_SIZE] for i in range(0, len(tracks), BATCH_SIZE)
][::-1]
for b in batched:
spotify.post(
f"https://api.spotify.com/v1/playlists/{out_id}/tracks",
data=json.dumps({"uris": b, "position": 0})
)
def combine_playlists(target_id: str, *pids: List[str]):
tracks = []
for p in pids:
tracks.extend([
t['track']['uri'] for t in spotify.get(
f"https://api.spotify.com/v1/playlists/{p}/tracks?offset=0&limit=100"
).json()['items']
])
BATCH_SIZE = 100
batched = [
tracks[i:i+BATCH_SIZE] for i in range(0, len(tracks), BATCH_SIZE)
][::-1]
for b in batched:
spotify.post(
f"https://api.spotify.com/v1/playlists/{target_id}/tracks",
data=json.dumps({"uris": b, "position": 0})
)
if __name__ == "__main__":
pass