forked from glomatico/spotify-web-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownloader.py
276 lines (249 loc) · 9.57 KB
/
downloader.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from __future__ import annotations
import datetime
import functools
import re
import shutil
import subprocess
from pathlib import Path
import requests
from mutagen.mp4 import MP4, MP4Cover, MP4FreeForm
from pywidevine import Cdm, Device
from .constants import *
from .enums import RemuxMode
from .hardcoded_wvd import HARDCODED_WVD
from .models import DownloadQueueItem, UrlInfo
from .spotify_api import SpotifyApi
class Downloader:
ILLEGAL_CHARACTERS_REGEX = r'[\\/:*?"<>|;]'
def __init__(
self,
spotify_api: SpotifyApi,
output_path: Path = Path("./Spotify"),
temp_path: Path = Path("./temp"),
wvd_path: Path = None,
ffmpeg_path: str = "ffmpeg",
mp4box_path: str = "MP4Box",
mp4decrypt_path: str = "mp4decrypt",
aria2c_path: str = "aria2c",
nm3u8dlre_path: str = "N_m3u8DL-RE",
remux_mode: RemuxMode = RemuxMode.FFMPEG,
date_tag_template: str = "%Y-%m-%dT%H:%M:%SZ",
exclude_tags: str = None,
truncate: int = 40,
silence: bool = False,
):
self.spotify_api = spotify_api
self.output_path = output_path
self.temp_path = temp_path
self.wvd_path = wvd_path
self.ffmpeg_path = ffmpeg_path
self.mp4box_path = mp4box_path
self.mp4decrypt_path = mp4decrypt_path
self.aria2c_path = aria2c_path
self.nm3u8dlre_path = nm3u8dlre_path
self.remux_mode = remux_mode
self.date_tag_template = date_tag_template
self.exclude_tags = exclude_tags
self.truncate = truncate
self.silence = silence
self._set_binaries_full_path()
self._set_exclude_tags_list()
self._set_truncate()
self._set_subprocess_additional_args()
def _set_binaries_full_path(self):
self.ffmpeg_path_full = shutil.which(self.ffmpeg_path)
self.mp4box_path_full = shutil.which(self.mp4box_path)
self.mp4decrypt_path_full = shutil.which(self.mp4decrypt_path)
self.aria2c_path_full = shutil.which(self.aria2c_path)
self.nm3u8dlre_path_full = shutil.which(self.nm3u8dlre_path)
def _set_exclude_tags_list(self):
self.exclude_tags_list = (
[i.lower() for i in self.exclude_tags.split(",")]
if self.exclude_tags is not None
else []
)
def _set_truncate(self):
self.truncate = None if self.truncate < 4 else self.truncate
def _set_subprocess_additional_args(self):
if self.silence:
self.subprocess_additional_args = {
"stdout": subprocess.DEVNULL,
"stderr": subprocess.DEVNULL,
}
else:
self.subprocess_additional_args = {}
def set_cdm(self) -> None:
if self.wvd_path:
self.cdm = Cdm.from_device(Device.load(self.wvd_path))
else:
self.cdm = Cdm.from_device(Device.loads(HARDCODED_WVD))
def get_url_info(self, url: str) -> UrlInfo:
url_regex_result = re.search(r"(album|playlist|track)/(\w{22})", url)
if url_regex_result is None:
raise Exception("Invalid URL")
return UrlInfo(type=url_regex_result.group(1), id=url_regex_result.group(2))
def get_download_queue(self, url_info: UrlInfo) -> list[DownloadQueueItem]:
download_queue = []
if url_info.type == "album":
download_queue.extend(
[
DownloadQueueItem(metadata=track_metadata)
for track_metadata in self.spotify_api.get_album(url_info.id)[
"tracks"
]["items"]
]
)
elif url_info.type == "playlist":
download_queue.extend(
[
DownloadQueueItem(metadata=track_metadata["track"])
for track_metadata in self.spotify_api.get_playlist(url_info.id)[
"tracks"
]["items"]
]
)
elif url_info.type == "track":
download_queue.append(
DownloadQueueItem(metadata=self.spotify_api.get_track(url_info.id))
)
return download_queue
def get_sanitized_string(self, dirty_string: str, is_folder: bool) -> str:
dirty_string = re.sub(self.ILLEGAL_CHARACTERS_REGEX, "_", dirty_string)
if is_folder:
dirty_string = dirty_string[: self.truncate]
if dirty_string.endswith("."):
dirty_string = dirty_string[:-1] + "_"
else:
if self.truncate is not None:
dirty_string = dirty_string[: self.truncate - 4]
return dirty_string.strip()
def get_release_date_datetime_obj(self, metadata_gid: dict) -> datetime.datetime:
metadata_gid_release_date = metadata_gid["album"]["date"]
if metadata_gid_release_date.get("day"):
datetime_obj = datetime.datetime(
year=metadata_gid_release_date["year"],
month=metadata_gid_release_date["month"],
day=metadata_gid_release_date["day"],
)
elif metadata_gid_release_date.get("month"):
datetime_obj = datetime.datetime(
year=metadata_gid_release_date["year"],
month=metadata_gid_release_date["month"],
day=1,
)
else:
datetime_obj = datetime.datetime(
year=metadata_gid_release_date["year"],
month=1,
day=1,
)
return datetime_obj
def get_release_date_tag(self, datetime_obj: datetime.datetime) -> str:
return datetime_obj.strftime(self.date_tag_template)
def get_artist(self, artist_list: list[dict]) -> str:
if len(artist_list) == 1:
return artist_list[0]["name"]
return (
", ".join(i["name"] for i in artist_list[:-1])
+ f' & {artist_list[-1]["name"]}'
)
def get_cover_url(self, metadata_gid: dict, size: str) -> str:
return "https://i.scdn.co/image/" + next(
i["file_id"]
for i in metadata_gid["album"]["cover_group"]["image"]
if i["size"] == size
)
def get_encrypted_path(
self,
track_id: str,
file_extension: str,
) -> Path:
return self.temp_path / (f"{track_id}_encrypted" + file_extension)
def get_decrypted_path(
self,
track_id: str,
file_extension: str,
) -> Path:
return self.temp_path / (f"{track_id}_decrypted" + file_extension)
def get_remuxed_path(
self,
track_id: str,
file_extension: str,
) -> Path:
return self.temp_path / (f"{track_id}_remuxed" + file_extension)
def decrypt_mp4decrypt(
self,
encrypted_path: Path,
decrypted_path: Path,
decryption_key: str,
):
subprocess.run(
[
self.mp4decrypt_path_full,
encrypted_path,
"--key",
f"1:{decryption_key}",
decrypted_path,
],
check=True,
**self.subprocess_additional_args,
)
@staticmethod
@functools.lru_cache()
def get_image_bytes(url: str) -> bytes:
return requests.get(url).content
def apply_tags(self, fixed_location: Path, tags: dict, cover_url: str):
to_apply_tags = [
tag_name
for tag_name in tags.keys()
if tag_name not in self.exclude_tags_list
]
mp4_tags = {}
for tag_name in to_apply_tags:
if tag_name in ("disc", "disc_total"):
if mp4_tags.get("disk") is None:
mp4_tags["disk"] = [[0, 0]]
if tag_name == "disc":
mp4_tags["disk"][0][0] = tags[tag_name]
elif tag_name == "disc_total":
mp4_tags["disk"][0][1] = tags[tag_name]
elif tag_name in ("track", "track_total"):
if mp4_tags.get("trkn") is None:
mp4_tags["trkn"] = [[0, 0]]
if tag_name == "track":
mp4_tags["trkn"][0][0] = tags[tag_name]
elif tag_name == "track_total":
mp4_tags["trkn"][0][1] = tags[tag_name]
elif tag_name == "compilation":
mp4_tags["cpil"] = tags["compilation"]
elif tag_name == "isrc":
mp4_tags["----:com.apple.iTunes:ISRC"] = [
MP4FreeForm(tags["isrc"].encode("utf-8"))
]
elif tag_name == "label":
mp4_tags["----:com.apple.iTunes:LABEL"] = [
MP4FreeForm(tags["label"].encode("utf-8"))
]
elif (
MP4_TAGS_MAP.get(tag_name) is not None
and tags.get(tag_name) is not None
):
mp4_tags[MP4_TAGS_MAP[tag_name]] = [tags[tag_name]]
if "cover" not in self.exclude_tags_list:
mp4_tags["covr"] = [
MP4Cover(
self.get_image_bytes(cover_url), imageformat=MP4Cover.FORMAT_JPEG
)
]
mp4 = MP4(fixed_location)
mp4.clear()
mp4.update(mp4_tags)
mp4.save()
def move_to_final_path(self, fixed_path: Path, final_path: Path):
final_path.parent.mkdir(parents=True, exist_ok=True)
shutil.move(fixed_path, final_path)
@functools.lru_cache()
def save_cover(self, cover_path: Path, cover_url: str):
cover_path.write_bytes(self.get_image_bytes(cover_url))
def cleanup_temp_path(self):
shutil.rmtree(self.temp_path)