forked from caseychu/spotify-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify_split.py
More file actions
108 lines (80 loc) · 3.09 KB
/
Copy pathspotify_split.py
File metadata and controls
108 lines (80 loc) · 3.09 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
import argparse
from datetime import datetime
import utils
from constants import ALBUM_TYPE_COMPILATIONS, CLIENT_ID
from spotify_api import SpotifyAPI
utils.setup_logging()
SPLIT_MODE_DATE_ADDED = "date-added"
SPLIT_MODE_RELEASE_DATE = "release-date"
def parse_args():
parser = argparse.ArgumentParser(
description="Splits your playlist by the specified characteristic."
)
parser.add_argument(
"--mode",
default=SPLIT_MODE_RELEASE_DATE,
choices=[SPLIT_MODE_RELEASE_DATE, SPLIT_MODE_DATE_ADDED],
help=f"output format (default: {SPLIT_MODE_RELEASE_DATE})",
)
parser.add_argument(
"--sc",
"--separate-compilations",
dest="separateCompilations",
action="store_true",
help="split songs from compilations into separate playlist, because compilations have misleading release-dates (default: False)",
)
parser.set_defaults(separateCompilations=False)
return parser.parse_args()
def new_playlist_name(playlists, suffix: str):
return f"{playlists['name']}.{suffix}"
def main():
args = parse_args()
print(args)
# Log into the Spotify API.
spotify = SpotifyAPI.authorize(
client_id=CLIENT_ID,
scope="user-library-read playlist-read-private playlist-read-collaborative playlist-modify-private",
)
me = utils.login(spotify)
playlists = utils.get_playlists(spotify, me)
playlist = utils.choose_playlist(playlists)
utils.load_playlist(spotify, me, playlist)
new_playlists = {}
if args.mode == SPLIT_MODE_RELEASE_DATE:
split_release_date(playlist, new_playlists, args.separateCompilations)
elif args.mode == SPLIT_MODE_DATE_ADDED:
split_date_added(playlist, new_playlists)
for playlist_name in sorted(new_playlists.keys()):
utils.create_playlist(spotify, me, playlist_name, new_playlists[playlist_name])
def split_release_date(playlist, new_playlists: dict, separateCompilations: bool):
for t in playlist["tracks"]:
if (
separateCompilations
and t["track"]["album"]["album_type"] == ALBUM_TYPE_COMPILATIONS
):
name = new_playlist_name(playlist, ALBUM_TYPE_COMPILATIONS)
if name in new_playlists:
new_playlists[name].append(t)
else:
new_playlists[name] = [t]
continue
year = utils.release_to_year(t["track"]["album"]["release_date"])
decade = utils.year_to_decade_str(year)
name = new_playlist_name(playlist, decade)
if name in new_playlists:
new_playlists[name].append(t)
else:
new_playlists[name] = [t]
def split_date_added(playlist, new_playlists: dict):
for t in playlist["tracks"]:
time = t["added_at"]
year = datetime.fromisoformat(time.removesuffix("Z")).year
name = f"added{year}"
name = new_playlist_name(playlist, name)
if name in new_playlists:
new_playlists[name].append(t)
else:
new_playlists[name] = [t]
if __name__ == "__main__":
main()