Skip to content

Commit

Permalink
added method Shazam.search_album(album_id=...)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotX12 committed Mar 28, 2024
1 parent 9bb5a32 commit 1c74c88
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 18 deletions.
15 changes: 15 additions & 0 deletions examples/album_info.py
Original file line number Diff line number Diff line change
@@ -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())
5 changes: 2 additions & 3 deletions examples/artist_albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions shazamio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions shazamio/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
41 changes: 41 additions & 0 deletions shazamio/schemas/album.py
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions shazamio/schemas/artist/views/full_albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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([])
4 changes: 2 additions & 2 deletions shazamio/schemas/artist/views/last_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions shazamio/schemas/artist/views/top_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions shazamio/schemas/artist/views/top_song.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
21 changes: 15 additions & 6 deletions shazamio/schemas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion shazamio/serializers.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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)

0 comments on commit 1c74c88

Please sign in to comment.