Refactoring Shazam client

This commit is contained in:
Fabio Scotto di Santolo
2024-11-13 12:04:39 +01:00
parent ff9bf4269c
commit ce12891a9a
5 changed files with 1042 additions and 36 deletions

View File

@@ -1,13 +1,27 @@
import logging
import time
from typing import Any
from shazamio import Shazam
async def recognize(data: str) -> dict[str, Any]:
api: Shazam = Shazam()
return await api.recognize(data)
logger = logging.getLogger(__name__)
async def album(album_id: int) -> dict[str, Any]:
api: Shazam = Shazam()
return await api.search_album(album_id=album_id)
class ShazamClient:
def __init__(self):
self.__count: int = 0
self.__client: Shazam = Shazam()
async def recognize(self, data: str) -> dict[str, Any]:
self.__wait()
return await self.__client.recognize(data)
async def album(self, album_id: int) -> dict[str, Any]:
self.__wait()
return await self.__client.search_album(album_id=album_id)
def __wait(self):
self.__count += 1
if self.__count == 10:
logger.debug("Waiting 30s every 10 requests")
time.sleep(30)