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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261 | def sync(
query: List[str],
downloader: Downloader,
) -> None:
"""
Sync function for the console.
It will download the songs and remove the ones that are no longer
present in the playlists/albums/etc
### Arguments
- query: list of strings to search for.
- downloader: Already initialized downloader instance.
"""
save_path = downloader.settings["save_file"]
downloader.settings["save_file"] = None
m3u_file = downloader.settings["m3u"]
downloader.settings["m3u"] = None
# Query and save file
# Create initial sync file
if query and save_path:
if any(req for req in query if req.endswith(".spotdl")):
# If the query contains a .spotdl file, and we are about to create
# .spotdl file, raise an error.
raise ValueError(
"Cannot create a sync file with a .spotdl file in the query."
)
# Parse the query
songs_list = parse_query(
query=query,
threads=downloader.settings["threads"],
use_ytm_data=downloader.settings["ytm_data"],
playlist_numbering=downloader.settings["playlist_numbering"],
album_type=downloader.settings["album_type"],
playlist_retain_track_cover=downloader.settings[
"playlist_retain_track_cover"
],
)
# Create sync file
with open(save_path, "w", encoding="utf-8") as save_file:
json.dump(
{
"type": "sync",
"query": query,
"songs": [song.json for song in songs_list],
},
save_file,
indent=4,
ensure_ascii=False,
)
# Perform initial download
downloader.download_multiple_songs(songs_list)
# Create m3u file
if m3u_file:
gen_m3u_files(
songs_list,
m3u_file,
downloader.settings["m3u_output"],
downloader.settings["format"],
downloader.settings["restrict"],
False,
)
return None
# If the query is a single file, download it
if ( # pylint: disable=R1702
len(query) == 1 # pylint: disable=R1702
and query[0].endswith(".spotdl") # pylint: disable=R1702
and not save_path # pylint: disable=R1702
):
# Load the sync file
with open(query[0], "r", encoding="utf-8") as sync_file:
sync_data = json.load(sync_file)
# Verify the sync file
if (
not isinstance(sync_data, dict)
or sync_data.get("type") != "sync"
or sync_data.get("songs") is None
):
raise ValueError("Sync file is not a valid sync file.")
# Parse the query
songs_playlist = parse_query(
query=sync_data["query"],
threads=downloader.settings["threads"],
use_ytm_data=downloader.settings["ytm_data"],
playlist_numbering=downloader.settings["playlist_numbering"],
album_type=downloader.settings["album_type"],
playlist_retain_track_cover=downloader.settings[
"playlist_retain_track_cover"
],
)
# Get the names and URLs of previously downloaded songs from the sync file
old_files = []
for entry in sync_data["songs"]:
file_name = create_file_name(
Song.from_dict(entry),
downloader.settings["output"],
downloader.settings["format"],
downloader.settings["restrict"],
)
old_files.append((file_name, entry["url"]))
new_urls = [song.url for song in songs_playlist]
# Delete all song files whose URL is no longer part of the latest playlist
if not downloader.settings["sync_without_deleting"]:
# Rename songs that have "{list-length}", "{list-position}", "{list-name}",
# in the output path so that we don't have to download them again,
# and to avoid mangling the directory structure.
to_rename: List[Tuple[Path, Path]] = []
to_delete = []
for path, url in old_files:
if url not in new_urls:
to_delete.append(path)
else:
new_song = songs_playlist[new_urls.index(url)]
new_path = create_file_name(
Song.from_dict(new_song.json),
downloader.settings["output"],
downloader.settings["format"],
downloader.settings["restrict"],
)
if path != new_path:
to_rename.append((path, new_path))
# fix later Downloading duplicate songs in the same playlist
# will trigger a re-download of the song. To fix this we have to copy the song
# to the new location without removing the old one.
for old_path, new_path in to_rename:
if old_path.exists():
logger.info("Renaming %s to %s", f"'{old_path}'", f"'{new_path}'")
if new_path.exists():
old_path.unlink()
continue
try:
old_path.rename(new_path)
except (PermissionError, OSError) as exc:
logger.debug(
"Could not rename temp file: %s, error: %s", old_path, exc
)
else:
logger.debug("%s does not exist.", old_path)
if downloader.settings["sync_remove_lrc"]:
lrc_file = old_path.with_suffix(".lrc")
new_lrc_file = new_path.with_suffix(".lrc")
if lrc_file.exists():
logger.debug(
"Renaming lrc %s to %s",
f"'{lrc_file}'",
f"'{new_lrc_file}'",
)
try:
lrc_file.rename(new_lrc_file)
except (PermissionError, OSError) as exc:
logger.debug(
"Could not rename lrc file: %s, error: %s",
lrc_file,
exc,
)
else:
logger.debug("%s does not exist.", lrc_file)
for file in to_delete:
if file.exists():
logger.info("Deleting %s", file)
try:
file.unlink()
except (PermissionError, OSError) as exc:
logger.debug(
"Could not remove temp file: %s, error: %s", file, exc
)
else:
logger.debug("%s does not exist.", file)
if downloader.settings["sync_remove_lrc"]:
lrc_file = file.with_suffix(".lrc")
if lrc_file.exists():
logger.debug("Deleting lrc %s", lrc_file)
try:
lrc_file.unlink()
except (PermissionError, OSError) as exc:
logger.debug(
"Could not remove lrc file: %s, error: %s",
lrc_file,
exc,
)
else:
logger.debug("%s does not exist.", lrc_file)
if len(to_delete) == 0:
logger.info("Nothing to delete...")
else:
logger.info("%s old songs were deleted.", len(to_delete))
if m3u_file:
gen_m3u_files(
songs_playlist,
m3u_file,
downloader.settings["m3u_output"],
downloader.settings["format"],
downloader.settings["restrict"],
False,
)
# Write the new sync file
with open(query[0], "w", encoding="utf-8") as save_file:
json.dump(
{
"type": "sync",
"query": sync_data["query"],
"songs": [song.json for song in songs_playlist],
},
save_file,
indent=4,
ensure_ascii=False,
)
downloader.download_multiple_songs(songs_playlist)
return None
raise ValueError(
"Wrong combination of arguments. "
"Either provide a query and a save path. Or a single sync file in the query"
)
|