-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasynchronous.py
More file actions
46 lines (35 loc) · 1.32 KB
/
Copy pathasynchronous.py
File metadata and controls
46 lines (35 loc) · 1.32 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
import aiohttp
import logging
import requests
import os
from pprint import pformat
import time
from dotenv import load_dotenv
import json
async def main():
logging.info("Start")
url = "https://cricket.sportmonks.com/api/v2.0/players"
load_dotenv()
api_token = os.getenv("cricket_api_token")
payload = {"api_token": api_token, "filter[country_id]": 146}
response = requests.get(url=url, params=payload)
results = response.json()
results_list = results["data"][0:9]
start = time.time()
player_ids = [result["id"] for result in results_list]
player_names = [result["fullname"] for result in results_list]
logging.info(player_ids)
async with aiohttp.ClientSession() as session:
tasks = [api_call(session, url, player_id, payload) for player_id in player_ids]
results_async = await asyncio.gather(*tasks)
logging.info(pformat(results_async[0]))
# logging.info(f"Retrieved information for {player_name}")
end = time.time()
logging.info(f"Time to make all API calls: {end - start}s")
async def api_call(session, url, player_id, payload):
async with session.get(url=f"{url}/{player_id}", params=payload) as response:
return await response.json()
if __name__ == "__main__":
logging.basicConfig(level="INFO")
asyncio.run(main())