-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-recurse.py
31 lines (27 loc) · 941 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
29
30
31
#!/usr/bin/python3
"""Contains recurse function"""
import requests
def recurse(subreddit, hot_list=[], after="", count=0):
"""Returns a list of titles of all hot posts on a given subreddit."""
url = "https://www.reddit.com/r/{}/hot/.json".format(subreddit)
headers = {
"User-Agent": "0x16-api_advanced:project:\
v1.0.0 (by /u/firdaus_cartoon_jr)"
}
params = {
"after": after,
"count": count,
"limit": 100
}
response = requests.get(url, headers=headers, params=params,
allow_redirects=False)
if response.status_code == 404:
return None
results = response.json().get("data")
after = results.get("after")
count += results.get("dist")
for c in results.get("children"):
hot_list.append(c.get("data").get("title"))
if after is not None:
return recurse(subreddit, hot_list, after, count)
return hot_list