Skip to content

Commit 5a43930

Browse files
committed
better apply_tags
1 parent 56fa25e commit 5a43930

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

spotify_web_downloader/downloader.py

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -172,52 +172,48 @@ def get_image_bytes(url: str) -> bytes:
172172
return requests.get(url).content
173173

174174
def apply_tags(self, fixed_location: Path, tags: dict, cover_url: str):
175-
mp4_tags = {
176-
v: [tags[k]]
177-
for k, v in MP4_TAGS_MAP.items()
178-
if k not in self.exclude_tags_list and tags.get(k) is not None
179-
}
180-
if not {"track", "track_total"} & set(self.exclude_tags_list) and tags.get(
181-
"track"
182-
):
183-
mp4_tags["trkn"] = [[0, 0]]
184-
if not {"disc", "disc_total"} & set(self.exclude_tags_list) and tags.get(
185-
"disc"
186-
):
187-
mp4_tags["disk"] = [[0, 0]]
188-
if (
189-
"compilation" not in self.exclude_tags_list
190-
and tags.get("compilation") is not None
191-
):
192-
mp4_tags["cpil"] = tags["compilation"]
175+
to_apply_tags = [
176+
tag_name
177+
for tag_name in tags.keys()
178+
if tag_name not in self.exclude_tags_list
179+
]
180+
mp4_tags = {}
181+
for tag_name in to_apply_tags:
182+
if tag_name in ("disc", "disc_total"):
183+
if mp4_tags.get("disk") is None:
184+
mp4_tags["disk"] = [[0, 0]]
185+
if tag_name == "disc":
186+
mp4_tags["disk"][0][0] = tags[tag_name]
187+
elif tag_name == "disc_total":
188+
mp4_tags["disk"][0][1] = tags[tag_name]
189+
elif tag_name in ("track", "track_total"):
190+
if mp4_tags.get("trkn") is None:
191+
mp4_tags["trkn"] = [[0, 0]]
192+
if tag_name == "track":
193+
mp4_tags["trkn"][0][0] = tags[tag_name]
194+
elif tag_name == "track_total":
195+
mp4_tags["trkn"][0][1] = tags[tag_name]
196+
elif tag_name == "compilation":
197+
mp4_tags["cpil"] = tags["compilation"]
198+
elif tag_name == "isrc":
199+
mp4_tags["----:com.apple.iTunes:ISRC"] = [
200+
MP4FreeForm(tags["isrc"].encode("utf-8"))
201+
]
202+
elif tag_name == "label":
203+
mp4_tags["----:com.apple.iTunes:LABEL"] = [
204+
MP4FreeForm(tags["label"].encode("utf-8"))
205+
]
206+
elif (
207+
MP4_TAGS_MAP.get(tag_name) is not None
208+
and tags.get(tag_name) is not None
209+
):
210+
mp4_tags[MP4_TAGS_MAP[tag_name]] = [tags[tag_name]]
193211
if "cover" not in self.exclude_tags_list:
194212
mp4_tags["covr"] = [
195213
MP4Cover(
196214
self.get_image_bytes(cover_url), imageformat=MP4Cover.FORMAT_JPEG
197215
)
198216
]
199-
if "isrc" not in self.exclude_tags_list and tags.get("isrc") is not None:
200-
mp4_tags["----:com.apple.iTunes:ISRC"] = [
201-
MP4FreeForm(tags["isrc"].encode("utf-8"))
202-
]
203-
if "label" not in self.exclude_tags_list and tags.get("label") is not None:
204-
mp4_tags["----:com.apple.iTunes:LABEL"] = [
205-
MP4FreeForm(tags["label"].encode("utf-8"))
206-
]
207-
if "track" not in self.exclude_tags_list and tags.get("track") is not None:
208-
mp4_tags["trkn"][0][0] = tags["track"]
209-
if (
210-
"track_total" not in self.exclude_tags_list
211-
and tags.get("track_total") is not None
212-
):
213-
mp4_tags["trkn"][0][1] = tags["track_total"]
214-
if "disc" not in self.exclude_tags_list and tags.get("disc") is not None:
215-
mp4_tags["disk"][0][0] = tags["disc"]
216-
if (
217-
"disc_total" not in self.exclude_tags_list
218-
and tags.get("disc_total") is not None
219-
):
220-
mp4_tags["disk"][0][1] = tags["disc_total"]
221217
mp4 = MP4(fixed_location)
222218
mp4.clear()
223219
mp4.update(mp4_tags)

0 commit comments

Comments
 (0)