Skip to content

Commit fcf869b

Browse files
Recurse it
1 parent e44ff38 commit fcf869b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: 0x16-api_advanced/2-recurse.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python3
2+
"""add to a list of hot list"""
3+
import requests
4+
5+
6+
def recurse(subreddit, hot_list=[], after="", count=0):
7+
"""recurse the value"""
8+
params = {
9+
'after': after,
10+
'count': count,
11+
'limit': 100
12+
}
13+
headers = {'User-Agent': 'Alx Task'}
14+
url = 'http://www.reddit.com/r/{}/top/.json'.format(subreddit)
15+
res = requests.get(url, params=params,
16+
headers=headers)
17+
if res.status_code >= 400:
18+
return None
19+
result = res.json().get('data')
20+
after = result.get('after')
21+
count += result.get('dist')
22+
resp = result.get('children')
23+
for child in resp:
24+
hot_list.append(child.get('data').get('title'))
25+
if after is not None:
26+
return recurse(subreddit, hot_list, after, count)
27+
else:
28+
return hot_list

0 commit comments

Comments
 (0)