-
Notifications
You must be signed in to change notification settings - Fork 18
/
2-recurse.py
37 lines (31 loc) · 1.04 KB
/
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
32
33
34
35
36
37
#!/usr/bin/python3
"""
Recursive function that queries the Reddit API and returns
a list containing the titles of all hot articles for a given subreddit.
If no results are found for the given subreddit,
the function should return None.
"""
import requests
def recurse(subreddit, hot_list=[], after=""):
"""
Queries the Reddit API and returns
a list containing the titles of all hot articles for a given subreddit.
- If not a valid subreddit, return None.
"""
req = requests.get(
"https://www.reddit.com/r/{}/hot.json".format(subreddit),
headers={"User-Agent": "Custom"},
params={"after": after},
)
if req.status_code == 200:
for get_data in req.json().get("data").get("children"):
dat = get_data.get("data")
title = dat.get("title")
hot_list.append(title)
after = req.json().get("data").get("after")
if after is None:
return hot_list
else:
return recurse(subreddit, hot_list, after)
else:
return None