mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
move fetching album bio to a class
This commit is contained in:
@@ -4,13 +4,13 @@ Contains all the album routes.
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from app import api
|
from app import api
|
||||||
from app import functions
|
|
||||||
from app import helpers
|
from app import helpers
|
||||||
from app import models
|
from app import models
|
||||||
from app.lib import albumslib
|
from app.lib import albumslib
|
||||||
from app.lib import trackslib
|
from app.lib import trackslib
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from flask import request
|
from flask import request
|
||||||
|
from app.functions import FetchAlbumBio
|
||||||
|
|
||||||
album_bp = Blueprint("album", __name__, url_prefix="")
|
album_bp = Blueprint("album", __name__, url_prefix="")
|
||||||
|
|
||||||
@@ -58,14 +58,14 @@ def get_album_tracks():
|
|||||||
def get_album_bio():
|
def get_album_bio():
|
||||||
"""Returns the album bio for the given album."""
|
"""Returns the album bio for the given album."""
|
||||||
data = request.get_json()
|
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 None:
|
||||||
|
|
||||||
if bio is not None:
|
|
||||||
return {"bio": bio}
|
|
||||||
else:
|
|
||||||
return {"bio": "No bio found."}, 404
|
return {"bio": "No bio found."}, 404
|
||||||
|
|
||||||
|
return {"bio": bio}
|
||||||
|
|
||||||
|
|
||||||
@album_bp.route("/album/artists", methods=["POST"])
|
@album_bp.route("/album/artists", methods=["POST"])
|
||||||
def get_albumartists():
|
def get_albumartists():
|
||||||
|
|||||||
+17
-8
@@ -6,6 +6,7 @@ import time
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from tqdm import tqdm
|
||||||
from app import api
|
from app import api
|
||||||
from app import helpers
|
from app import helpers
|
||||||
from app import settings
|
from app import settings
|
||||||
@@ -73,15 +74,14 @@ def fetch_artist_images():
|
|||||||
|
|
||||||
artists = []
|
artists = []
|
||||||
|
|
||||||
for song in api.DB_TRACKS:
|
for song in tqdm(api.DB_TRACKS, desc="Gathering artists"):
|
||||||
this_artists = song["artists"].split(", ")
|
this_artists = song["artists"].split(", ")
|
||||||
|
|
||||||
for artist in this_artists:
|
for artist in this_artists:
|
||||||
if artist not in artists:
|
if artist not in artists:
|
||||||
artists.append(artist)
|
artists.append(artist)
|
||||||
|
|
||||||
_bar = Bar("Processing images", max=len(artists))
|
for artist in tqdm(artists, desc="Fetching images"):
|
||||||
for artist in artists:
|
|
||||||
file_path = (
|
file_path = (
|
||||||
helpers.app_dir + "/images/artists/" + artist.replace("/", "::") + ".webp"
|
helpers.app_dir + "/images/artists/" + artist.replace("/", "::") + ".webp"
|
||||||
)
|
)
|
||||||
@@ -96,12 +96,8 @@ def fetch_artist_images():
|
|||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
_bar.next()
|
|
||||||
|
|
||||||
_bar.finish()
|
def fetch_album_bio(title: str, albumartist: str) -> str | None:
|
||||||
|
|
||||||
|
|
||||||
def fetch_album_bio(title: str, albumartist: str):
|
|
||||||
"""
|
"""
|
||||||
Returns the album bio for a given album.
|
Returns the album bio for a given album.
|
||||||
"""
|
"""
|
||||||
@@ -123,6 +119,19 @@ def fetch_album_bio(title: str, albumartist: str):
|
|||||||
return bio
|
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
|
# TODO
|
||||||
# - Move the populate function to a new file and probably into a new class
|
# - Move the populate function to a new file and probably into a new class
|
||||||
# - Start movement from functional programming to OOP to OOP
|
# - Start movement from functional programming to OOP to OOP
|
||||||
|
|||||||
Reference in New Issue
Block a user