-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-top_ten.py
More file actions
22 lines (19 loc) · 746 Bytes
/
1-top_ten.py
File metadata and controls
22 lines (19 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/python3
"""
Contains the top_ten function
"""
import requests
def top_ten(subreddit):
"""prints the titles of the top ten hot posts for a given subreddit"""
if subreddit is None or type(subreddit) is not str:
print(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={'limit': 10}).json()
posts = r.get('data', {}).get('children', None)
if posts is None or (len(posts) > 0 and posts[0].get('kind') != 't3'):
print(None)
else:
for post in posts:
print(post.get('data', {}).get('title', None))