-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotify-playlists.py
executable file
·208 lines (169 loc) · 6.33 KB
/
spotify-playlists.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
# Copyright (C) 2017 Felix Geyer <debfx@fobos.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or (at your option)
# version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import configparser
import os
import sys
import xml.etree.ElementTree
import jinja2
import spotipy
import spotipy.cache_handler
import spotipy.oauth2
import spotipy.util
SCOPES = (
"playlist-read-collaborative",
"playlist-read-private",
"user-library-read",
"playlist-modify-private",
"playlist-modify-public",
)
CONFIG_AUTH = "auth.ini"
PLAYLIST_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<title>{{ title }}</title>
{%- if location %}
<location>{{ location }}</location>
{%- endif %}
<extension application="https://github.com/debfx/spotify-playlists">
<public>{{ public | string | lower }}</public>
<collaborative>{{ collaborative | string | lower }}</collaborative>
<type>{{ type }}</type>
</extension>
<trackList>
{%- for track in tracklist %}
<track>
<title>{{ track.title }}</title>
<creator>{{ track.artists }}</creator>
<location>{{ track.uri }}</location>
</track>
{%- endfor %}
</trackList>
</playlist>
"""
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i : i + n]
def process_tracks(tracks):
result = []
for item in tracks["items"]:
track = item["track"]
if track is None:
# some playlists have extra "null" tracks (without any information), just skip them
continue
artists = ";".join([artist["name"] for artist in track["artists"]])
result.append({"title": track["name"], "artists": artists, "uri": track["uri"]})
return result
def write_playlist(name, dirname, tracks, type, location=None, public=False, collaborative=False):
env = jinja2.Environment(autoescape=True)
template = env.from_string(PLAYLIST_TEMPLATE)
content = template.render(
title=name,
location=location,
tracklist=tracks,
type=type,
public=public,
collaborative=collaborative,
)
xspf_path = "{}/{}.xspf".format(dirname, name.replace("/", "_"))
with open(xspf_path, "w") as f:
f.write(content)
def export_playlists(sp, username, dirname):
if not os.path.isdir(dirname):
os.mkdir(dirname)
playlists = sp.user_playlists(username)
playlist_items = playlists["items"]
while playlists["next"]:
playlists = sp.next(playlists)
playlist_items.extend(playlists["items"])
for playlist in playlist_items:
# playlist sometimes contain null entries, skip them
if playlist is None:
continue
tracks = sp.playlist_items(
playlist["id"],
fields="items(track(name,artists(name),uri)),next",
)
tracks_processed = process_tracks(tracks)
while tracks["next"]:
tracks = sp.next(tracks)
tracks_processed.extend(process_tracks(tracks))
write_playlist(
playlist["name"],
dirname,
tracks_processed,
type="playlist",
location=playlist["uri"],
public=playlist["public"],
collaborative=playlist["collaborative"],
)
tracks = sp.current_user_saved_tracks()
tracks_processed = process_tracks(tracks)
while tracks["next"]:
tracks = sp.next(tracks)
tracks_processed.extend(process_tracks(tracks))
write_playlist("Saved tracks", dirname, tracks_processed, type="saved_tracks")
def import_playlist(sp, username, filename):
tree = xml.etree.ElementTree.parse(filename)
root = tree.getroot()
name = root.find("{http://xspf.org/ns/0/}title").text
tracks = []
public = False
collaborative = False
for elem in root.findall("{http://xspf.org/ns/0/}trackList/{http://xspf.org/ns/0/}track"):
location = elem.find("{http://xspf.org/ns/0/}location").text
tracks.append(location)
elem_extension = root.find(
"{http://xspf.org/ns/0/}extension[@application='https://github.com/debfx/spotify-playlists']"
)
if elem_extension is not None:
elem_public = elem_extension.find("{http://xspf.org/ns/0/}public")
if elem_public is not None:
public = elem_public.text.lower() == "true"
elem_collaborative = elem_extension.find("{http://xspf.org/ns/0/}collaborative")
if elem_collaborative is not None:
collaborative = elem_collaborative.text.lower() == "true"
playlist_id = sp.user_playlist_create(username, name, public=public)["id"]
if collaborative:
sp.user_playlist_change_details(username, playlist_id, collaborative=collaborative)
# the Spotify API allows only 100 tracks per request
for tracks_chunk in chunks(tracks, 100):
sp.user_playlist_add_tracks(username, playlist_id, tracks_chunk)
def main():
if len(sys.argv) != 3 or sys.argv[1] not in ("import", "export"):
print(
"Usage: {} <import FILENAME / export DIR>".format(sys.argv[0]),
file=sys.stderr,
)
sys.exit(0)
command = sys.argv[1]
arg = sys.argv[2]
config = configparser.ConfigParser()
config.read(CONFIG_AUTH)
auth_manager = spotipy.oauth2.SpotifyOAuth(
client_id=config["spotify"]["client_id"],
client_secret=config["spotify"]["client_secret"],
redirect_uri=config["spotify"]["redirect_uri"],
scope=" ".join(SCOPES),
cache_handler=spotipy.cache_handler.CacheFileHandler(
username=config["spotify"]["username"],
),
)
sp = spotipy.Spotify(auth_manager=auth_manager)
if command == "import":
import_playlist(sp, config["spotify"]["username"], arg)
else:
export_playlists(sp, config["spotify"]["username"], arg)
if __name__ == "__main__":
main()