-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.py
29 lines (23 loc) · 900 Bytes
/
reddit.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
import requests
from pprint import pprint
from reddit_auth import get_auth_headers
headers = get_auth_headers()
def get_id_from_url(url):
return url.split('/')[6]
def get_top_n_posts(subreddit, n):
url = f'https://oauth.reddit.com/r/{subreddit}/hot'
posts = requests.get(url, headers=headers, params={'limit': n}).json()
post_ids = [post['data']['id'] for post in posts['data']['children']]
return post_ids
def get_all_comments_from_post(post_id):
url = f'https://oauth.reddit.com/comments/{post_id}'
comments = requests.get(url, headers=headers).json()
comment_bodies = []
for comment in comments[1]['data']['children']:
try:
comment_bodies.append(comment['data']['body'])
except:
pass
return comment_bodies
if __name__ == "__main__":
pprint(get_all_comments_from_post(get_top_n_posts('news', 1)[0]))