From 1c74c882fdca7064bd0f6c54c15befc1737ec6f3 Mon Sep 17 00:00:00 2001 From: dotX12 Date: Fri, 29 Mar 2024 00:08:54 +0300 Subject: [PATCH] added method Shazam.search_album(album_id=...) --- examples/album_info.py | 15 +++++++ examples/artist_albums.py | 5 +-- shazamio/api.py | 23 +++++++++++ shazamio/misc.py | 3 ++ shazamio/schemas/album.py | 41 +++++++++++++++++++ shazamio/schemas/artist/views/full_albums.py | 4 +- shazamio/schemas/artist/views/last_release.py | 4 +- shazamio/schemas/artist/views/top_music.py | 4 +- shazamio/schemas/artist/views/top_song.py | 4 +- shazamio/schemas/base.py | 21 +++++++--- shazamio/serializers.py | 8 +++- 11 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 examples/album_info.py create mode 100644 shazamio/schemas/album.py diff --git a/examples/album_info.py b/examples/album_info.py new file mode 100644 index 0000000..226b8be --- /dev/null +++ b/examples/album_info.py @@ -0,0 +1,15 @@ +import asyncio +from shazamio import Shazam, Serialize + + +async def main(): + shazam = Shazam() + albums = await shazam.search_album(album_id=1544741796) + serialized = Serialize.album_info(data=albums) + + for i in serialized.data[0].relationships.tracks.data: + msg = f"{i.id} | {i.attributes.album_name} | {i.attributes.artist_name} [{i.attributes.name}]" + print(msg) + +loop = asyncio.get_event_loop_policy().get_event_loop() +loop.run_until_complete(main()) diff --git a/examples/artist_albums.py b/examples/artist_albums.py index 6d21ab9..4ffe2c0 100644 --- a/examples/artist_albums.py +++ b/examples/artist_albums.py @@ -4,12 +4,11 @@ async def main(): shazam = Shazam() - artist_id = 1202214602 + artist_id = 1300793014 albums = await shazam.artist_albums(artist_id=artist_id) serialized = Serialize.artist_albums(data=albums) - for i in serialized.data: - print(f"{i.attributes.name} - {i.attributes.track_count} {i.attributes.release_date}") + print(f"{i.id} | {i.attributes.artist_name} ->>>>>>>> {i.attributes.name} - {i.attributes.track_count} {i.attributes.release_date}") loop = asyncio.get_event_loop_policy().get_event_loop() diff --git a/shazamio/api.py b/shazamio/api.py index 7165c29..2485434 100644 --- a/shazamio/api.py +++ b/shazamio/api.py @@ -455,6 +455,29 @@ async def artist_albums( proxy=proxy, ) + async def search_album( + self, + album_id: int, + proxy: Optional[str] = None, + ): + """ + Get album info by id + + :param album_id: Album number. Example (203347991) + :param proxy: Proxy server + :return: dict albums + """ + + return await self.http_client.request( + "GET", + ShazamUrl.ARTIST_ALBUM_INFO.format( + endpoint_country=self.endpoint_country, + album_id=album_id, + ), + headers=self.headers(), + proxy=proxy, + ) + async def get_youtube_data( self, link: str, diff --git a/shazamio/misc.py b/shazamio/misc.py index d275ecc..b32c000 100644 --- a/shazamio/misc.py +++ b/shazamio/misc.py @@ -56,6 +56,9 @@ class ShazamUrl: "https://www.shazam.com/services/amapi/v1/catalog/{endpoint_country}" "/artists/{artist_id}/albums?limit={limit}&offset={offset}" ) + ARTIST_ALBUM_INFO = ( + "https://www.shazam.com/services/amapi/v1/catalog/{endpoint_country}/albums/{album_id}" + ) class Request: diff --git a/shazamio/schemas/album.py b/shazamio/schemas/album.py new file mode 100644 index 0000000..fa71047 --- /dev/null +++ b/shazamio/schemas/album.py @@ -0,0 +1,41 @@ +from typing import Optional, List + +from pydantic import BaseModel, Field + +from shazamio.schemas.artist.views.full_albums import AttributesFullAlbums +from shazamio.schemas.artist.views.top_song import AttributesTopSong +from shazamio.schemas.base import BaseHref, BaseIdTypeHref + + +class TrackInfoDTO(AttributesTopSong): + has_credits: Optional[bool] = Field(None, alias="hasCredits") + + +class TrackInfoWithHref(BaseIdTypeHref): + id: str + type: str + href: str + attributes: TrackInfoDTO + + +class TrackModel(BaseHref): + href: str + data: List[TrackInfoWithHref] = Field([]) + + +class ArtistModel(BaseHref): + data: List[BaseIdTypeHref] = Field([]) + + +class AlbumRelationships(BaseModel): + artists: ArtistModel + tracks: TrackModel + + +class AlbumModel(BaseModel): + id: str + type: str + href: str + attributes: AttributesFullAlbums + relationships: AlbumRelationships + diff --git a/shazamio/schemas/artist/views/full_albums.py b/shazamio/schemas/artist/views/full_albums.py index 4b3a7b0..7fa6a68 100644 --- a/shazamio/schemas/artist/views/full_albums.py +++ b/shazamio/schemas/artist/views/full_albums.py @@ -5,7 +5,7 @@ from pydantic import BaseModel, Field from shazamio.schemas.attributes import AttributeName -from shazamio.schemas.base import BaseDataModel +from shazamio.schemas.base import BaseIdTypeHrefAttributesModel from shazamio.schemas.photos import ImageModel @@ -50,4 +50,4 @@ class AttributesFullAlbums(BaseModel): class FullAlbumsModel(BaseModel): href: Optional[str] = None attributes: Optional[AttributeName] = None - data: Optional[List[BaseDataModel[AttributesFullAlbums]]] = None + data: List[BaseIdTypeHrefAttributesModel[AttributesFullAlbums]] = Field([]) diff --git a/shazamio/schemas/artist/views/last_release.py b/shazamio/schemas/artist/views/last_release.py index ce9518e..06071e2 100644 --- a/shazamio/schemas/artist/views/last_release.py +++ b/shazamio/schemas/artist/views/last_release.py @@ -5,7 +5,7 @@ from pydantic import BaseModel, Field from shazamio.schemas.attributes import AttributeName -from shazamio.schemas.base import BaseDataModel +from shazamio.schemas.base import BaseAttributesModel from shazamio.schemas.photos import ImageModel @@ -39,4 +39,4 @@ class AttributeLastRelease(BaseModel): class LastReleaseModel(BaseModel): href: Optional[str] = None attributes: Optional[AttributeName] = None - data: Optional[List[BaseDataModel[AttributeLastRelease]]] = None + data: Optional[List[BaseAttributesModel[AttributeLastRelease]]] = None diff --git a/shazamio/schemas/artist/views/top_music.py b/shazamio/schemas/artist/views/top_music.py index 8886311..43d574c 100644 --- a/shazamio/schemas/artist/views/top_music.py +++ b/shazamio/schemas/artist/views/top_music.py @@ -5,7 +5,7 @@ from pydantic import BaseModel, Field from shazamio.schemas.attributes import AttributeName -from shazamio.schemas.base import BaseDataModel +from shazamio.schemas.base import BaseAttributesModel from shazamio.schemas.photos import ImageModel @@ -42,4 +42,4 @@ class Attributes(BaseModel): class TopMusicVideosView(BaseModel): href: Optional[str] = None attributes: Optional[AttributeName] = None - data: Optional[List[BaseDataModel[Attributes]]] = None + data: Optional[List[BaseAttributesModel[Attributes]]] = None diff --git a/shazamio/schemas/artist/views/top_song.py b/shazamio/schemas/artist/views/top_song.py index 8b0e135..2612c0a 100644 --- a/shazamio/schemas/artist/views/top_song.py +++ b/shazamio/schemas/artist/views/top_song.py @@ -6,7 +6,7 @@ from shazamio.schemas.artist.views.top_music import PlayParams from shazamio.schemas.attributes import AttributeName -from shazamio.schemas.base import BaseDataModel +from shazamio.schemas.base import BaseAttributesModel from shazamio.schemas.photos import ImageModel from shazamio.schemas.urls import UrlDTO @@ -41,4 +41,4 @@ class TopSong(BaseModel): type: Optional[str] = None href: Optional[str] = None attributes: Optional[AttributeName] = None - data: Optional[List[BaseDataModel[AttributesTopSong]]] = None + data: Optional[List[BaseAttributesModel[AttributesTopSong]]] = None diff --git a/shazamio/schemas/base.py b/shazamio/schemas/base.py index 7347a2b..46f61ab 100644 --- a/shazamio/schemas/base.py +++ b/shazamio/schemas/base.py @@ -5,21 +5,30 @@ from pydantic import BaseModel from pydantic.generics import GenericModel - T = TypeVar("T", bound=BaseModel) -class BaseIdTypeHref(BaseModel): +class BaseHref(BaseModel): + href: str + + +class BaseIdTypeHref(BaseHref): id: str type: str - href: str -class BaseDataModel(GenericModel, BaseModel, Generic[T]): +class BaseIdTypeHrefAttributesModel(GenericModel, BaseIdTypeHref, Generic[T]): attributes: T -class BaseHrefNextData(GenericModel, Generic[T]): - href: str +class BaseAttributesModel(GenericModel, BaseModel, Generic[T]): + attributes: T + + +class BaseDataModel(GenericModel, BaseModel, Generic[T]): + data: T + + +class BaseHrefNextData(GenericModel, BaseHref, Generic[T]): next: Optional[str] = None data: T diff --git a/shazamio/serializers.py b/shazamio/serializers.py index 9244d38..aadded8 100644 --- a/shazamio/serializers.py +++ b/shazamio/serializers.py @@ -1,5 +1,6 @@ -from typing import Union +from typing import Union, List +from shazamio.schemas.base import BaseDataModel from shazamio.factory_misc import FACTORY_ARTIST from shazamio.factory_misc import FACTORY_TRACK from shazamio.schemas.artist.views.full_albums import FullAlbumsModel @@ -9,6 +10,7 @@ from shazamio.schemas.models import ResponseTrack from shazamio.schemas.models import TrackInfo from shazamio.schemas.models import YoutubeData +from shazamio.schemas.album import AlbumModel class Serialize: @@ -35,3 +37,7 @@ def artist(cls, data): @classmethod def full_track(cls, data): return FACTORY_TRACK.load(data, ResponseTrack) + + @classmethod + def album_info(cls, data) -> BaseDataModel[List[AlbumModel]]: + return BaseDataModel[List[AlbumModel]].parse_obj(data)