-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-top-tracks-async.py
30 lines (25 loc) · 1.07 KB
/
user-top-tracks-async.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import aiohttp
import asyncio
import time
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from decouple import config
start_time = time.time()
scope = "user-top-read"
ranges = ['short_term', 'medium_term', 'long_term']
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=config("client_id"),
client_secret=config("client_secret"),
redirect_uri=config("redirect_uri"),
scope=scope))
async def main():
async with aiohttp.ClientSession() as session:
for sp_range in ranges:
print("range:", sp_range)
results = sp.current_user_top_tracks(time_range=sp_range, limit=50)
async with session.get(results) as resp:
results = await resp.json()
for i, item in enumerate(results['items']):
print(i, item['name'], '//', item['artists'][0]['name'])
print()
asyncio.run(main())
print("--- %s seconds ---" % (time.time() - start_time))