Skip to content

Commit

Permalink
Fix top tracks, small refactoring.
Browse files Browse the repository at this point in the history
Shazam has changed the contract for obtaining top songs:
- Top tracks in country by genre
- Top tracks in country
- Top tracks in city
- Top tracks in world by genre
- Top tracks in world

We managed to maintain this functionality, but the format of the responses has changed, as have the response models! Please make corrections, the data has changed significantly!!!

Added `playlist` and `playlists` methods for Serialize.
  • Loading branch information
dotX12 committed Apr 27, 2024
1 parent 93561e6 commit 1e021ff
Show file tree
Hide file tree
Showing 29 changed files with 496 additions and 314 deletions.
2 changes: 1 addition & 1 deletion examples/about_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

async def main():
shazam = Shazam()
track_id = 552406075
track_id = 53982678
about_track = await shazam.track_about(track_id=track_id)
serialized = Serialize.track(data=about_track)

Expand Down
41 changes: 41 additions & 0 deletions examples/recognize_and_get_album.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import asyncio
import logging

from aiohttp_retry import ExponentialRetry

from shazamio import Shazam, Serialize, HTTPClient

logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(name)s - [%(filename)s:%(lineno)d - %(funcName)s()] - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)


async def main():
shazam = Shazam(
http_client=HTTPClient(
retry_options=ExponentialRetry(
attempts=12, max_timeout=204.8, statuses={500, 502, 503, 504, 429}
),
),
)

new_version_path = await shazam.recognize("data/Gloria.ogg")

album_info = await shazam.search_album(album_id=new_version_path["track"]["albumadamid"])
album_serialized = Serialize.album_info(data=album_info)
# Get album name
print(album_serialized.data[0].attributes.name)

# And get all tracks in album
for i in album_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())
28 changes: 14 additions & 14 deletions examples/recognize_song.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import logging

from shazamio import Shazam, Serialize
from aiohttp_retry import ExponentialRetry
from shazamio import Shazam, Serialize, HTTPClient

logger = logging.getLogger(__name__)
logging.basicConfig(
Expand All @@ -12,27 +13,26 @@


async def main():
# shazam = Shazam(
# http_client=HTTPClient(
# retry_options=ExponentialRetry(attempts=12, max_timeout=204.8, statuses={500, 502, 503, 504, 429}),
# ),
# )
# if u need override retry option...

shazam = Shazam()
shazam = Shazam(
http_client=HTTPClient(
retry_options=ExponentialRetry(
attempts=12, max_timeout=204.8, statuses={500, 502, 503, 504, 429}
),
),
)

# pass path (deprecated)
old_version = await shazam.recognize_song(data="data/dora.ogg") # deprecated
serialized_old = Serialize.full_track(old_version)
print(serialized_old)
# old_version = await shazam.recognize_song(data="data/dora.ogg") # deprecated
# serialized_old = Serialize.full_track(old_version)
# print(serialized_old)

# pass path
new_version_path = await shazam.recognize("data/dora.ogg")
new_version_path = await shazam.recognize("data/Gloria.ogg")
serialized_new_path = Serialize.full_track(new_version_path)
print(serialized_new_path)

# pass bytes
with open("data/dora.ogg", "rb") as file:
with open("data/Gloria.ogg", "rb") as file:
new_version_path = await shazam.recognize(file.read())
serialized_new_path = Serialize.full_track(new_version_path)
print(serialized_new_path)
Expand Down
14 changes: 7 additions & 7 deletions examples/top_tracks_city.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@


async def main():
shazam = Shazam(language="GB")
shazam = Shazam(language="EN")
top_ten_moscow_tracks = await shazam.top_city_tracks(
country_code="RU",
city_name="Moscow",
limit=10,
)
print(top_ten_moscow_tracks)
# ALL TRACKS DICT
for track in top_ten_moscow_tracks["tracks"]:
serialized = Serialize.track(data=track)
# SERIALIZE FROM DATACLASS FACTORY
print(serialized)
serialized = Serialize.playlists(top_ten_moscow_tracks)
print(serialized)

# for element in top_ten_moscow_tracks["data"]:
# serialized = Serialize.playlist(data=element)
# print(serialized)


loop = asyncio.get_event_loop_policy().get_event_loop()
Expand Down
9 changes: 6 additions & 3 deletions examples/top_tracks_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

async def main():
shazam = Shazam()
top_five_track_from_amsterdam = await shazam.top_country_tracks("NL", 5)
for track in top_five_track_from_amsterdam["tracks"]:
serialized = Serialize.track(data=track)
top_five_track_from_amsterdam = await shazam.top_country_tracks("NL", 100)
tracks = Serialize.playlists(top_five_track_from_amsterdam)
print(tracks)

for track in top_five_track_from_amsterdam["data"]:
serialized = Serialize.playlist(data=track)
print(serialized)


Expand Down
13 changes: 10 additions & 3 deletions examples/top_tracks_genre_country.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import asyncio
from shazamio import Shazam, GenreMusic
from shazamio import Shazam, GenreMusic, Serialize


async def main():
shazam = Shazam()
top_spain_rap = await shazam.top_country_genre_tracks(
country_code="ES", genre=GenreMusic.HIP_HOP_RAP, limit=4
country_code="ES",
genre=GenreMusic.HIP_HOP_RAP,
limit=4,
)
print(top_spain_rap)
serialized = Serialize.playlists(top_spain_rap)
print(serialized)

for playlist in top_spain_rap["data"]:
serialized = Serialize.playlist(data=playlist)
print(serialized)


loop = asyncio.get_event_loop_policy().get_event_loop()
Expand Down
9 changes: 6 additions & 3 deletions examples/top_tracks_genre_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ async def main():
shazam = Shazam()
top_rock_in_the_world = await shazam.top_world_genre_tracks(genre=GenreMusic.ROCK, limit=10)

for track in top_rock_in_the_world["tracks"]:
serialized_track = Serialize.track(data=track)
print(serialized_track)
serialized = Serialize.playlists(top_rock_in_the_world)
print(serialized)

for playlist in top_rock_in_the_world["data"]:
serialized = Serialize.playlist(data=playlist)
print(serialized)


loop = asyncio.get_event_loop_policy().get_event_loop()
Expand Down
8 changes: 5 additions & 3 deletions examples/top_world_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
async def main():
shazam = Shazam()
top_world_tracks = await shazam.top_world_tracks(limit=10)
print(top_world_tracks)
for track in top_world_tracks["tracks"]:
serialized = Serialize.track(track)
serialized = Serialize.playlists(top_world_tracks)
print(serialized)

for playlist in top_world_tracks["data"]:
serialized = Serialize.playlist(data=playlist)
print(serialized)


Expand Down
Loading

0 comments on commit 1e021ff

Please sign in to comment.