Skip to content

Commit a506cf7

Browse files
committed
Close session on exit
1 parent 9ff3e78 commit a506cf7

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ from shazamio import Shazam
3838
async def main():
3939
shazam = Shazam()
4040
out = await shazam.recognize_song('dora.ogg')
41+
await shazam.stop()
4142
print(out)
4243

4344
loop = asyncio.get_event_loop()
@@ -63,6 +64,7 @@ async def main():
6364
artist_id = 43328183
6465
about_artist = await shazam.artist_about(artist_id)
6566
serialized = Serialize.artist(about_artist)
67+
await shazam.stop()
6668

6769
print(about_artist) # dict
6870
print(serialized) # serialized from dataclass factory
@@ -91,6 +93,7 @@ async def main():
9193
track_id = 552406075
9294
about_track = await shazam.track_about(track_id=track_id)
9395
serialized = Serialize.track(data=about_track)
96+
await shazam.stop()
9497

9598
print(about_track) # dict
9699
print(serialized) # serialized from dataclass factory
@@ -119,6 +122,7 @@ async def main():
119122
shazam = Shazam()
120123
track_id = 559284007
121124
count = await shazam.listening_counter(track_id=track_id)
125+
await shazam.stop()
122126
print(count)
123127

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

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

172178
loop = asyncio.get_event_loop()
173179
loop.run_until_complete(main())
@@ -190,6 +196,7 @@ from shazamio import Shazam
190196
async def main():
191197
shazam = Shazam()
192198
tracks = await shazam.search_track(query='Lil', limit=5)
199+
await shazam.stop()
193200
print(tracks)
194201

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

239+
await shazam.stop()
240+
232241

233242
loop = asyncio.get_event_loop_policy().get_event_loop()
234243
loop.run_until_complete(main())
@@ -260,6 +269,8 @@ async def main():
260269
# SERIALIZE FROM DATACLASS FACTORY
261270
print(serialized)
262271

272+
await shazam.stop()
273+
263274
loop = asyncio.get_event_loop()
264275
loop.run_until_complete(main())
265276

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

289301
loop = asyncio.get_event_loop()
290302
loop.run_until_complete(main())
@@ -311,6 +323,7 @@ async def main():
311323
genre=GenreMusic.HIP_HOP_RAP,
312324
limit=4
313325
)
326+
await shazam.stop()
314327
print(top_spain_rap)
315328

316329
loop = asyncio.get_event_loop()
@@ -339,6 +352,8 @@ async def main():
339352
serialized_track = Serialize.track(data=track)
340353
print(serialized_track.spotify_url)
341354

355+
await shazam.stop()
356+
342357

343358
loop = asyncio.get_event_loop()
344359
loop.run_until_complete(main())
@@ -365,6 +380,7 @@ async def main():
365380
for track in top_world_tracks['tracks']:
366381
serialized = Serialize.track(track)
367382
print(serialized)
383+
await shazam.stop()
368384

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

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

shazamio/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def __init__(self, language: str = "en-US", endpoint_country: str = "GB"):
2727
self.language = language
2828
self.endpoint_country = endpoint_country
2929

30+
async def stop(self):
31+
super().stop()
32+
3033
async def top_world_tracks(self, limit: int = 200, offset: int = 0) -> Dict[str, Any]:
3134
"""
3235
Search top world tracks

shazamio/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66

77
class HTTPClient:
8-
http_session: aiohttp.ClientSession
9-
108
def __init__(self, *args, **kwargs):
119
self.http_session = aiohttp.ClientSession()
1210

11+
async def stop(self):
12+
await self.http_session.close()
13+
1314
async def request(self, method: str, url: str, *args, **kwargs) -> dict:
1415
if method.upper() == "GET":
1516
async with self.http_session.get(url, **kwargs) as resp:

shazamio/converter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99

1010
class Geo(HTTPClient):
11+
async def stop(self):
12+
super().stop()
13+
1114
async def city_id_from(self, country: Union[CountryCode, str], city: str) -> int:
1215
"""
1316
Return City ID from country name and city name.

tests/test_recognize.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async def song_bytes():
1616
async def test_recognize_song_file():
1717
shazam = Shazam()
1818
out = await shazam.recognize_song(data="examples/data/dora.ogg")
19+
await shazam.stop()
1920

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

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

4345
shazam = Shazam()
4446
out = await shazam.recognize_song(data=short_audio_segment)
47+
await shazam.stop()
4548

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

0 commit comments

Comments
 (0)