-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-recurse.py
More file actions
31 lines (28 loc) · 1.02 KB
/
2-recurse.py
File metadata and controls
31 lines (28 loc) · 1.02 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
#!/usr/bin/python3
"""
Contains the recurse function
"""
import requests
def recurse(subreddit, hot_list=[], after=None):
"""returns a list of all hot post titles for a given subreddit"""
if subreddit is None or type(subreddit) is not str:
return None
r = requests.get('http://www.reddit.com/r/{}/hot.json'.format(subreddit),
headers={'User-Agent': 'Python/requests:APIproject:\
v1.0.0 (by /u/aaorrico23)'},
params={'after': after}).json()
after = r.get('data', {}).get('after', None)
posts = r.get('data', {}).get('children', None)
if posts is None or (len(posts) > 0 and posts[0].get('kind') != 't3'):
if len(hot_list) == 0:
return None
return hot_list
else:
for post in posts:
hot_list.append(post.get('data', {}).get('title', None))
if after is None:
if len(hot_list) == 0:
return None
return hot_list
else:
return recurse(subreddit, hot_list, after)