-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
109 lines (77 loc) · 3.78 KB
/
bot.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import praw
import pdb
import re
import os
import csv
import pymongo
from classes import Phrase, Acronym, Subreddit
client = pymongo.MongoClient(my_connection_string)
sub_db = client["acronym_bot"]["subreddit"]
comment_db = client["acronym_bot"]["comment"]
reddit = praw.Reddit('bot1')
disclaimer = "*************** \n\n^(Je suis un bot. Manage settings @ [Bot Website](http://google.com) | [Source Code](https://github.com/Shreya-7/expandForMe))"
comments_replied_to = [i for i in comment_db.distinct(
'parent_comment_id')]
cursor = sub_db.find({"opted": 1})
subreddits = {}
# finding the names of all the required subreddits
for item in cursor:
item.pop('_id')
subreddits[item['name']] = item
# get the subreddit instance of all the required subreddits
subreddit = reddit.subreddit(''.join(subreddits.keys()))
# monitor the continuous stream of comments
for comment in subreddit.stream.comments():
# if the comment has not been replied to
if comment.id not in comments_replied_to:
# if the comment is not my own
if comment.author.name != 'BookAcronymBot':
acronyms_done = []
reply = ''
# find which sub the comment belongs to
comment_sub = sub_db.find_one(
{'name': comment.subreddit.display_name})
# if auto or if called
if comment_sub['auto'] == 1 or '!expandForMe' in comment.body:
# find the initial bot comment - if commented, there must be one entry with the post id
bot_comment = comment_db.find_one(
{'post_id': comment.submission.id})
if bot_comment is not None:
acronyms_done = bot_comment['acronyms_done']
old_acronym_count = len(acronyms_done)
# if it has been called, get parent_id
if '!expandForMe' in comment.body:
parent_id = comment.parent_id
# if the parent is a comment
if parent_id[:3] == 't1_':
text_body = reddit.comment(parent_id).body
# if the parent is a post
else:
text_body = reddit.submission(parent_id).body
else:
text_body = comment.body
for key, value in comment_sub['acronym_list'].items():
# if match found
if key in text_body:
if bot_comment is None or (key not in bot_comment['acronyms_done']):
acronyms_done = list(set(acronyms_done + [key]))
if len(acronyms_done) != old_acronym_count:
for acronym in acronyms_done:
reply += comment_sub['phrase'].format(
**comment_sub['acronym_list'][acronym]) + "\n\n"
if bot_comment is None or comment_sub['comment_frequency'] == 1:
new_bot_comment = comment.reply(reply+disclaimer)
else:
reply = bot_comment['comment_body'] + reply
new_bot_comment = comment.submission.reply(
reply+disclaimer)
comment_db.update_one({'post_id': comment.submission.id}, {'$set': {
'post_id': comment.submission.id,
'parent_comment_id': comment.id,
'subreddit_name': comment_sub['name'],
'comment_id': [new_bot_comment.id],
'comment_body': reply,
'acronyms_done': acronyms_done
}}, upsert=True)
# update the object with the parent comment ID
comments_replied_to.append(comment.id)