-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (38 loc) · 1.73 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
import praw
import pandas as pd
def get_top_posts(subreddit_list='MachineLearning', limit=1000, time_filter='all'):
assert limit >= 1, 'Please enter a request of at least 1'
reddit = praw.Reddit(client_id='thGxXn50aXKMCqWwz3boSg',
client_secret='2333NbmJYn-ksIZ77baMpQ9juMWQvg',
redirect_uri='http://localhost:8080',
user_agent='Kyrptix')
post_df = []
posts = reddit.subreddit(subreddit_list).top(time_filter=time_filter, limit=limit)
for post in posts:
post_df.append({
'post_id': post.id,
'subreddit': post.subreddit,
'created_utc': post.created_utc,
'selftext': post.selftext,
'post_url': post.url,
'post_title': post.title,
'link_flair_text': post.link_flair_text,
'score': post.score,
'num_comments': post.num_comments,
'upvote_ratio': post.upvote_ratio
})
return pd.DataFrame(post_df), reddit
if __name__ == "__main__":
create_csv = True
posts_df, reddit = get_top_posts(subreddit_list='MachineLearning+datascience+artificial', limit=10, time_filter='all')
comments_list = []
for post_id in posts_df['post_id']:
submission = reddit.submission(post_id)
submission.comments.replace_more(limit=None)
for comment in submission.comments.list():
comments_list.append({'post_id': post_id,
'comment': comment.body})
comments_df = pd.DataFrame(comments_list)
if create_csv == True:
posts_df.to_csv('DS_ML_AI_posts.csv', header=True, index=False)
comments_df.to_csv('DS_ML_AI_comments.csv', header=True, index=False)