From d52c8ac8feecdf8cddcee95206fe27d21a35fa98 Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Tue, 10 May 2022 13:17:01 +0300 Subject: [PATCH] move fetching album bio to a class --- server/app/api/album.py | 12 ++++++------ server/app/functions.py | 25 +++++++++++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/server/app/api/album.py b/server/app/api/album.py index 53eb69b3..a7eef531 100644 --- a/server/app/api/album.py +++ b/server/app/api/album.py @@ -4,13 +4,13 @@ Contains all the album routes. from typing import List from app import api -from app import functions from app import helpers from app import models from app.lib import albumslib from app.lib import trackslib from flask import Blueprint from flask import request +from app.functions import FetchAlbumBio album_bp = Blueprint("album", __name__, url_prefix="") @@ -58,14 +58,14 @@ def get_album_tracks(): def get_album_bio(): """Returns the album bio for the given album.""" data = request.get_json() + fetch_bio = FetchAlbumBio(data["album"], data["albumartist"])() + bio = fetch_bio() - bio = functions.fetch_album_bio(data["album"], data["albumartist"]) - - if bio is not None: - return {"bio": bio} - else: + if bio is None: return {"bio": "No bio found."}, 404 + return {"bio": bio} + @album_bp.route("/album/artists", methods=["POST"]) def get_albumartists(): diff --git a/server/app/functions.py b/server/app/functions.py index d2b25f76..237998f8 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -6,6 +6,7 @@ import time from io import BytesIO import requests +from tqdm import tqdm from app import api from app import helpers from app import settings @@ -73,15 +74,14 @@ def fetch_artist_images(): artists = [] - for song in api.DB_TRACKS: + for song in tqdm(api.DB_TRACKS, desc="Gathering artists"): this_artists = song["artists"].split(", ") for artist in this_artists: if artist not in artists: artists.append(artist) - _bar = Bar("Processing images", max=len(artists)) - for artist in artists: + for artist in tqdm(artists, desc="Fetching images"): file_path = ( helpers.app_dir + "/images/artists/" + artist.replace("/", "::") + ".webp" ) @@ -96,12 +96,8 @@ def fetch_artist_images(): except requests.exceptions.ConnectionError: time.sleep(5) - _bar.next() - _bar.finish() - - -def fetch_album_bio(title: str, albumartist: str): +def fetch_album_bio(title: str, albumartist: str) -> str | None: """ Returns the album bio for a given album. """ @@ -123,6 +119,19 @@ def fetch_album_bio(title: str, albumartist: str): return bio +class FetchAlbumBio: + """ + Returns the album bio for a given album. + """ + + def __init__(self, title: str, albumartist: str): + self.title = title + self.albumartist = albumartist + + def __call__(self): + return fetch_album_bio(self.title, self.albumartist) + + # TODO # - Move the populate function to a new file and probably into a new class # - Start movement from functional programming to OOP to OOP