From 447bcbc3fa6475a3f4f84f57326c3370b6304f52 Mon Sep 17 00:00:00 2001 From: codey Date: Sun, 11 Aug 2024 17:28:43 -0400 Subject: [PATCH] resolves #7 --- endpoints/lastfm.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ main.py | 2 ++ 2 files changed, 53 insertions(+) create mode 100644 endpoints/lastfm.py diff --git a/endpoints/lastfm.py b/endpoints/lastfm.py new file mode 100644 index 0000000..9db2a0b --- /dev/null +++ b/endpoints/lastfm.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3.12 + +import importlib +from fastapi import FastAPI +from pydantic import BaseModel + +class ValidArtistSearchRequest(BaseModel): + """ + - **a**: artist name + """ + + a: str + +class LastFM(FastAPI): + """Last.FM Endpoints""" + def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called + self.app = app + self.util = util + self.constants = constants + self.lastfm = importlib.import_module("lastfm_wrapper").LastFM() + + self.endpoints = { + "get_artist_by_name": self.artist_by_name_handler, + #tbd + } + + for endpoint, handler in self.endpoints.items(): + app.add_api_route(f"/{endpoint}/", handler, methods=["POST"]) + + async def artist_by_name_handler(self, data: ValidArtistSearchRequest): + """ + Get artist info + """ + artist = data.a.strip() + if not artist: + return { + 'err': True, + 'errorText': 'No artist specified' + } + + artist_result = await self.lastfm.search_artist(artist=artist) + if not artist_result or "err" in artist_result.keys(): + return { + 'err': True, + 'errorText': 'Search failed (no results?)' + } + + return { + 'success': True, + 'result': artist_result + } diff --git a/main.py b/main.py index 6841397..12defdf 100644 --- a/main.py +++ b/main.py @@ -54,6 +54,8 @@ Actionable Routes randmsg_endpoint = importlib.import_module("endpoints.rand_msg").RandMsg(app, util, constants) # Below also provides: /lyric_cache_list/ (in addition to /lyric_search/) lyric_search_endpoint = importlib.import_module("endpoints.lyric_search").LyricSearch(app, util, constants) +# Below provides numerous LastFM-fed endpoints +lastfm_endpoints = importlib.import_module("endpoints.lastfm").LastFM(app, util, constants)