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 | 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,
downloader.settings["threads"],
downloader.settings["ytm_data"],
downloader.settings["playlist_numbering"],
)
# 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["output"],
downloader.settings["format"],
downloader.settings["restrict"],
False,
)
return None
# If the query is a single file, download it
if len(query) == 1 and query[0].endswith(".spotdl") and not save_path:
# 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(
sync_data["query"],
downloader.settings["threads"],
downloader.settings["ytm_data"],
downloader.settings["playlist_numbering"],
)
# 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"]:
to_delete = [path for (path, url) in old_files if url not in new_urls]
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 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["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"
)
|