move fetching album bio to a class

This commit is contained in:
geoffrey45
2022-05-10 13:17:01 +03:00
parent 8dfa6834ad
commit d52c8ac8fe
2 changed files with 23 additions and 14 deletions
+6 -6
View File
@@ -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():
+17 -8
View File
@@ -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