Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache aiohttp.ClientSession #83

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ from shazamio import Shazam
async def main():
shazam = Shazam()
out = await shazam.recognize_song('dora.ogg')
await shazam.stop()
print(out)

loop = asyncio.get_event_loop()
Expand All @@ -63,6 +64,7 @@ async def main():
artist_id = 43328183
about_artist = await shazam.artist_about(artist_id)
serialized = Serialize.artist(about_artist)
await shazam.stop()

print(about_artist) # dict
print(serialized) # serialized from dataclass factory
Expand Down Expand Up @@ -91,6 +93,7 @@ async def main():
track_id = 552406075
about_track = await shazam.track_about(track_id=track_id)
serialized = Serialize.track(data=about_track)
await shazam.stop()

print(about_track) # dict
print(serialized) # serialized from dataclass factory
Expand Down Expand Up @@ -119,6 +122,7 @@ async def main():
shazam = Shazam()
track_id = 559284007
count = await shazam.listening_counter(track_id=track_id)
await shazam.stop()
print(count)

loop = asyncio.get_event_loop()
Expand All @@ -144,6 +148,7 @@ async def main():
track_id = 546891609
related = await shazam.related_tracks(track_id=track_id, limit=5, offset=2)
# ONLY №3, №4 SONG
await shazam.stop()
print(related)

loop = asyncio.get_event_loop()
Expand All @@ -168,6 +173,7 @@ async def main():
for artist in artists['artists']['hits']:
serialized = Serialize.artist(data=artist)
print(serialized)
await shazam.stop()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Expand All @@ -190,6 +196,7 @@ from shazamio import Shazam
async def main():
shazam = Shazam()
tracks = await shazam.search_track(query='Lil', limit=5)
await shazam.stop()
print(tracks)

loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -229,6 +236,8 @@ async def main():
for i in serialized.data[0].views.top_songs.data:
print(i.attributes.name)

await shazam.stop()


loop = asyncio.get_event_loop_policy().get_event_loop()
loop.run_until_complete(main())
Expand Down Expand Up @@ -260,6 +269,8 @@ async def main():
# SERIALIZE FROM DATACLASS FACTORY
print(serialized)

await shazam.stop()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Expand All @@ -285,6 +296,7 @@ async def main():
for track in top_five_track_from_amsterdam['tracks']:
serialized = Serialize.track(data=track)
print(serialized)
await shazam.stop()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Expand All @@ -311,6 +323,7 @@ async def main():
genre=GenreMusic.HIP_HOP_RAP,
limit=4
)
await shazam.stop()
print(top_spain_rap)

loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -339,6 +352,8 @@ async def main():
serialized_track = Serialize.track(data=track)
print(serialized_track.spotify_url)

await shazam.stop()


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Expand All @@ -365,6 +380,7 @@ async def main():
for track in top_world_tracks['tracks']:
serialized = Serialize.track(track)
print(serialized)
await shazam.stop()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Expand All @@ -390,6 +406,7 @@ async def main():
for track in top_five_track_from_amsterdam['tracks']:
serialized = Serialize.track(data=track)
print(serialized.title)
await shazam.stop()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Expand Down
26 changes: 15 additions & 11 deletions shazamio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@


class HTTPClient:
@staticmethod
async def request(method: str, url: str, *args, **kwargs) -> dict:
async with aiohttp.ClientSession() as session:
if method.upper() == "GET":
async with session.get(url, **kwargs) as resp:
return await validate_json(resp, *args)
elif method.upper() == "POST":
async with session.post(url, **kwargs) as resp:
return await validate_json(resp, *args)
else:
raise BadMethod("Accept only GET/POST")
def __init__(self, *args, **kwargs):
self.http_session = aiohttp.ClientSession()

async def stop(self):
await self.http_session.close()

async def request(self, method: str, url: str, *args, **kwargs) -> dict:
if method.upper() == "GET":
async with self.http_session.get(url, **kwargs) as resp:
return await validate_json(resp, *args)
elif method.upper() == "POST":
async with self.http_session.post(url, **kwargs) as resp:
return await validate_json(resp, *args)
else:
raise BadMethod("Accept only GET/POST")
3 changes: 3 additions & 0 deletions tests/test_recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async def song_bytes():
async def test_recognize_song_file():
shazam = Shazam()
out = await shazam.recognize_song(data="examples/data/dora.ogg")
await shazam.stop()

assert out.get("matches") != []
assert out["track"]["key"] == "549679333"
Expand All @@ -25,6 +26,7 @@ async def test_recognize_song_file():
async def test_recognize_song_bytes(song_bytes: bytes):
shazam = Shazam()
out = await shazam.recognize_song(data=song_bytes)
await shazam.stop()

assert out.get("matches") != []
assert out["track"]["key"] == "549679333"
Expand All @@ -42,6 +44,7 @@ async def test_recognize_song_too_short():

shazam = Shazam()
out = await shazam.recognize_song(data=short_audio_segment)
await shazam.stop()

assert out.get("matches") == []
assert "track" not in out