add related albums route

This commit is contained in:
mungai-njoroge 2023-07-02 00:11:21 +03:00
parent 95214396e7
commit 6ef3cc3545
3 changed files with 44 additions and 3 deletions

View File

@ -3,6 +3,7 @@ Contains all the album routes.
""" """
from dataclasses import asdict from dataclasses import asdict
import random
from flask import Blueprint, request from flask import Blueprint, request
@ -12,6 +13,7 @@ from app.store.albums import AlbumStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
from app.db.sqlite.albums import SQLiteAlbumMethods as adb from app.db.sqlite.albums import SQLiteAlbumMethods as adb
from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb
from app.db.sqlite.lastfm.similar_artists import SQLiteLastFMSimilarArtists as lastfmdb
from app.utils.hashing import create_hash from app.utils.hashing import create_hash
from app.serializers.album import serialize_for_card from app.serializers.album import serialize_for_card
@ -168,3 +170,44 @@ def get_album_versions():
a.get_date_from_tracks(tracks) a.get_date_from_tracks(tracks)
return {"data": albums} return {"data": albums}
@api.route("/album/similar", methods=["GET"])
def get_similar_albums():
"""
Returns similar albums to the given album.
"""
data = request.args
if data is None:
return {"msg": "No artisthash provided"}
artisthash: str = data["artisthash"]
limit: int = data.get("limit")
if limit is None:
limit = 6
limit = int(limit)
similar_artists = lastfmdb.get_similar_artists_for(artisthash)
if similar_artists is None:
return {"albums": []}
artisthashes = similar_artists.get_artist_hash_set()
if len(artisthashes) == 0:
return {"albums": []}
albums = [AlbumStore.get_albums_by_artisthash(a) for a in artisthashes]
albums = [a for sublist in albums for a in sublist]
albums = list({a.albumhash: a for a in albums}.values())
try:
albums = random.sample(albums, min(len(albums), limit))
except ValueError:
pass
return {"albums": [serialize_for_card(a) for a in albums]}

View File

@ -19,6 +19,7 @@ def serialize_for_card(album: Album):
"albumartist_hashes", "albumartist_hashes",
"og_title", "og_title",
"base_title", "base_title",
"genres",
} }
return album_serializer(album, props_to_remove) return album_serializer(album, props_to_remove)

View File

@ -2,7 +2,6 @@
This file is used to run the application. This file is used to run the application.
""" """
import logging import logging
# import sys
from app.api import create_api from app.api import create_api
from app.arg_handler import HandleArgs from app.arg_handler import HandleArgs
@ -14,8 +13,6 @@ from app.start_info_logger import log_startup_info
from app.utils.filesystem import get_home_res_path from app.utils.filesystem import get_home_res_path
from app.utils.threading import background from app.utils.threading import background
# sys.stderr = open("./logs.txt", "w")
werkzeug = logging.getLogger("werkzeug") werkzeug = logging.getLogger("werkzeug")
werkzeug.setLevel(logging.ERROR) werkzeug.setLevel(logging.ERROR)