Skip to content

Commit

Permalink
Close session on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
averyanalex committed Feb 4, 2024
1 parent 9ff3e78 commit 8bb28f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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
5 changes: 3 additions & 2 deletions shazamio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@


class HTTPClient:
http_session: aiohttp.ClientSession

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:
Expand Down
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

0 comments on commit 8bb28f8

Please sign in to comment.