-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRedditProf.py
42 lines (36 loc) · 1.29 KB
/
RedditProf.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
import time
import json
import praw
from praw.models import MoreComments
def authenticate():
return praw.Reddit('prof_bot', user_agent="find_prof bot v01")
# Time delay required by Reddit
time_delay = 2
# Bot logs in to reddit
reddit_bot = authenticate()
subreddit_names = "yorku"
profs_name = "Hamzeh Roumani"
profs_name = profs_name.lower()
prof_permutations = profs_name.split()
prof_permutations.append(profs_name)
prof_post_and_comment = {}
# Counter to iterate through all prof permutations
i = 0
# Searches through all of subreddit's threads for keyword
for submission in reddit_bot.subreddit(subreddit_names).search(prof_permutations[i]):
title = submission.title.lower()
prof_post_and_comment[title] = []
# Sorts all submission's comments by best
submission.comment_sort = 'best'
# Takes 5 top comments
submission.comment_limit = 1
# Appends post titles and top comment bodies to json data
for top_level_comment in submission.comments:
if isinstance(top_level_comment, MoreComments):
continue
prof_post_and_comment[title].append(top_level_comment.body)
time.sleep(time_delay)
json_data = json.dumps(prof_post_and_comment, sort_keys=True, indent=4)
i += 1
with open("juicy.json", "w") as file:
file.write(json_data)