-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.py
More file actions
38 lines (30 loc) · 1.28 KB
/
async.py
File metadata and controls
38 lines (30 loc) · 1.28 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
import selectors
import sys
import asyncio
import aiohttp
import json
async def main():
async with aiohttp.ClientSession() as client:
await asyncio.gather(get_reddit_top('python', client),
get_reddit_top('programming', client),
get_reddit_top('compsci', client))
async def get_json(client, url):
async with client.get(url) as response:
assert response.status == 200
return await response.read()
async def get_reddit_top(subreddit, client):
data1 = await get_json(client, 'https://www.reddit.com/r/' + subreddit + '/top.json?sort=top&t=day&limit=5')
j = json.loads(data1.decode('utf-8'))
for i in j['data']['children']:
score = i['data']['score']
title = i['data']['title']
link = i['data']['url']
print(f"{score}: {title} ({link})")
print('DONE:', subreddit + '\n')
#Since I am using python 3.8, which changed the EventLoop for asyncio
#and aiohttp does not support it just yet (https://github.com/aio-libs/aiohttp/issues/4324)
#I am changing the Event Loop type.
#for python 3.7 or when aiohttp gets updated,
#asyncio.run(main()) is the only line you need.
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())