From e20b14a9df242cefc390f35d2cccd76f285a8545 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Fri, 19 Apr 2024 12:37:14 +0200 Subject: [PATCH] feat: added fetching artworks to bandcamp --- development/actual_donwload.py | 2 +- music_kraken/pages/bandcamp.py | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/development/actual_donwload.py b/development/actual_donwload.py index ff96706..333da4f 100644 --- a/development/actual_donwload.py +++ b/development/actual_donwload.py @@ -8,7 +8,7 @@ if __name__ == "__main__": commands = [ "s: #a Ghost Bath", "0", - "1", + "d: 1", ] diff --git a/music_kraken/pages/bandcamp.py b/music_kraken/pages/bandcamp.py index f63e30e..a670026 100644 --- a/music_kraken/pages/bandcamp.py +++ b/music_kraken/pages/bandcamp.py @@ -254,7 +254,7 @@ class Bandcamp(Page): artist.source_collection.append(source) return artist - def _parse_track_element(self, track: dict) -> Optional[Song]: + def _parse_track_element(self, track: dict, artwork: Artwork) -> Optional[Song]: lyrics_list: List[Lyrics] = [] _lyrics: Optional[str] = track.get("item", {}).get("recordingOf", {}).get("lyrics", {}).get("text") @@ -264,7 +264,8 @@ class Bandcamp(Page): return Song( title=clean_song_title(track["item"]["name"]), source_list=[Source(self.SOURCE_TYPE, track["item"]["mainEntityOfPage"])], - tracksort=int(track["position"]) + tracksort=int(track["position"]), + artwork=artwork, ) def fetch_album(self, source: Source, stop_at_level: int = 1) -> Album: @@ -297,12 +298,32 @@ class Bandcamp(Page): )] ) + artwork: Artwork = Artwork() + + def _get_artwork_url(_data: dict) -> Optional[str]: + if "image" in _data: + return _data["image"] + for _property in _data.get("additionalProperty", []): + if _property.get("name") == "art_id": + return f"https://f4.bcbits.com/img/a{_property.get('value')}_2.jpg" + + _artwork_url = _get_artwork_url(data) + if _artwork_url is not None: + artwork.append(url=_artwork_url, width=350, height=350) + else: + for album_release in data.get("albumRelease", []): + _artwork_url = _get_artwork_url(album_release) + if _artwork_url is not None: + artwork.append(url=_artwork_url, width=350, height=350) + break + + for i, track_json in enumerate(data.get("track", {}).get("itemListElement", [])): if DEBUG: dump_to_file(f"album_track_{i}.json", json.dumps(track_json), is_json=True, exit_after_dump=False) try: - album.song_collection.append(self._parse_track_element(track_json)) + album.song_collection.append(self._parse_track_element(track_json, artwork=artwork)) except KeyError: continue