-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy path2-recurse.py
executable file
·28 lines (22 loc) · 962 Bytes
/
2-recurse.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
#!/usr/bin/python3
"""Module for task 2"""
def recurse(subreddit, hot_list=[], count=0, after=None):
"""Queries the Reddit API and returns all hot posts
of the subreddit"""
import requests
sub_info = requests.get("https://www.reddit.com/r/{}/hot.json"
.format(subreddit),
params={"count": count, "after": after},
headers={"User-Agent": "My-User-Agent"},
allow_redirects=False)
if sub_info.status_code >= 400:
return None
hot_l = hot_list + [child.get("data").get("title")
for child in sub_info.json()
.get("data")
.get("children")]
info = sub_info.json()
if not info.get("data").get("after"):
return hot_l
return recurse(subreddit, hot_l, info.get("data").get("count"),
info.get("data").get("after"))