From 9879f131e8730b0447d425cd938ac8238c21f6b2 Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Tue, 31 May 2022 00:05:37 +0300 Subject: [PATCH] rewrite getting artist images using classes - remove left padding on #acontent --- server/app/functions.py | 121 ++++++++++++++++++++-------- server/app/lib/populate.py | 12 +-- server/app/models.py | 15 +++- src/assets/css/global.scss | 1 + src/components/AlbumView/Header.vue | 6 +- src/views/Playlists.vue | 3 - 6 files changed, 107 insertions(+), 51 deletions(-) diff --git a/server/app/functions.py b/server/app/functions.py index 68e0f8ab..b553efa0 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -14,6 +14,7 @@ from app.lib import watchdoge from app.lib.populate import Populate from PIL import Image from progress.bar import Bar +from concurrent.futures import ThreadPoolExecutor from app.lib.trackslib import create_all_tracks @@ -23,11 +24,10 @@ def reindex_tracks(): """ Checks for new songs every 5 minutes. """ - is_underway = False while True: populate() - fetch_artist_images() + CheckArtistImages()() time.sleep(60) @@ -49,52 +49,103 @@ def populate(): api.TRACKS.extend(tracks) -@helpers.background -def fetch_image_path(artist: str) -> str or None: +class getArtistImage: """ - Returns a direct link to an artist image. + Returns an artist image url. """ - try: - url = f"https://api.deezer.com/search/artist?q={artist}" - response = requests.get(url) - data = response.json() + def __init__(self, artist: str): + self.artist = artist - return data["data"][0]["picture_medium"] - except requests.exceptions.ConnectionError: - time.sleep(5) - return None - except (IndexError, KeyError): - return None + def __call__(self): + try: + url = f"https://api.deezer.com/search/artist?q={self.artist}" + response = requests.get(url) + data = response.json() + + return data["data"][0]["picture_medium"] + except requests.exceptions.ConnectionError: + time.sleep(5) + return None + except (IndexError, KeyError): + return None -@helpers.background -def fetch_artist_images(): - """Downloads the artists images""" +class useImageDownloader: + def __init__(self, url: str, dest: str) -> None: + self.url = url + self.dest = dest - artists = [] + def __call__(self) -> None: + try: + img = Image.open(BytesIO(requests.get(self.url).content)) + img.save(self.dest, format="webp") + img.close() + except requests.exceptions.ConnectionError: + print("🔴🔴🔴🔴🔴🔴🔴") + time.sleep(5) - for song in tqdm(api.DB_TRACKS, desc="Finding artists"): - this_artists = song["artists"].split(", ") - for artist in this_artists: - if artist not in artists: - artists.append(artist) +class CheckArtistImages: + def __init__(self): + self.artists: list[str] = [] + print("Checking for artist images") - for artist in tqdm(artists, desc="Fetching images"): - file_path = ( - helpers.app_dir + "/images/artists/" + artist.replace("/", "::") + ".webp" + @staticmethod + def check_if_exists(img_path: str): + """ + Checks if an image exists on disk. + """ + + if os.path.exists(img_path): + return True + else: + return False + + def gather_artists(self): + """ + Loops through all the tracks and gathers all the artists. + """ + + for song in api.DB_TRACKS: + this_artists: list = song["artists"].split(", ") + + for artist in this_artists: + if artist not in self.artists: + self.artists.append(artist) + + @classmethod + def download_image(cls, artistname: str): + """ + Checks if an artist image exists and downloads it if not. + + :param artistname: The artist name + """ + + img_path = ( + helpers.app_dir + + "/images/artists/" + + artistname.replace("/", "::") + + ".webp" ) - if not os.path.exists(file_path): - img_path = fetch_image_path(artist) + if cls.check_if_exists(img_path): + return - if img_path is not None: - try: - img = Image.open(BytesIO(requests.get(img_path).content)) - img.save(file_path, format="webp") - except requests.exceptions.ConnectionError: - time.sleep(5) + url = getArtistImage(artistname)() + + if url is None: + return + + useImageDownloader(url, img_path)() + + def __call__(self): + self.gather_artists() + + with ThreadPoolExecutor() as pool: + pool.map(self.download_image, self.artists) + + print("Done fetching images") def fetch_album_bio(title: str, albumartist: str) -> str | None: diff --git a/server/app/lib/populate.py b/server/app/lib/populate.py index c5792257..d7d70b42 100644 --- a/server/app/lib/populate.py +++ b/server/app/lib/populate.py @@ -106,13 +106,13 @@ class Populate: """ s = time.time() - # print(f"Started tagging files") - # with ThreadPoolExecutor() as executor: - # executor.map(self.get_tags, self.files) + print(f"Started tagging files") + with ThreadPoolExecutor() as executor: + executor.map(self.get_tags, self.files) - with Pool(maxtasksperchild=10) as p: - tags = p.map(get_tags, tqdm(self.files)) - self.process_tags(tags) + # with Pool(maxtasksperchild=10, processes=10) as p: + # tags = p.map(get_tags, tqdm(self.files)) + # self.process_tags(tags) # for t in tqdm(self.files): # self.get_tags(t) diff --git a/server/app/models.py b/server/app/models.py index 27453ff1..4e6ff2c0 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -46,7 +46,12 @@ class Track: self.length = tags["length"] self.genre = tags["genre"] self.bitrate = tags["bitrate"] - self.image = tags["image"] + + try: + self.image = tags["image"] + except KeyError: + print(tags) + self.tracknumber = tags["tracknumber"] self.discnumber = tags["discnumber"] self.albumhash = tags["albumhash"] @@ -97,9 +102,11 @@ class Album: def get_p_track(ptrack): for track in api.TRACKS: - if (track.title == ptrack["title"] - and track.artists == ptrack["artists"] - and ptrack["album"] == track.album): + if ( + track.title == ptrack["title"] + and track.artists == ptrack["artists"] + and ptrack["album"] == track.album + ): return track diff --git a/src/assets/css/global.scss b/src/assets/css/global.scss index ff528022..ab6589f3 100644 --- a/src/assets/css/global.scss +++ b/src/assets/css/global.scss @@ -133,6 +133,7 @@ a { grid-area: content; max-width: 1504px; padding: $small; + padding-left: 0; overflow: auto; .nav { diff --git a/src/components/AlbumView/Header.vue b/src/components/AlbumView/Header.vue index 3dfd56e1..9322a514 100644 --- a/src/components/AlbumView/Header.vue +++ b/src/components/AlbumView/Header.vue @@ -15,7 +15,6 @@
Compilation
@@ -65,8 +64,9 @@ extrackColors(); height: 100%; background-color: $black; background-color: #000000; - background-image: linear-gradient(147deg, #436a91 0%, #2c3e50 74%); - .art { + background-image: linear-gradient(37deg, $black 20%, $gray, $black 90%); + +.art { width: 100%; height: 100%; left: 1rem; diff --git a/src/views/Playlists.vue b/src/views/Playlists.vue index b29cda31..834fcd1b 100644 --- a/src/views/Playlists.vue +++ b/src/views/Playlists.vue @@ -21,9 +21,6 @@ const pStore = usePStore();