This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
forked from sevgit/Its_URUGUAY_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
141 lines (113 loc) · 3.7 KB
/
script.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import datetime
import random
import string
import traceback
import praw
import botlogin
comment_log_path = "log.txt"
misspells = {
"Uruguay": [
"urugay",
"uraguay",
"uragay",
"urogway",
"uruguauy",
"uruguary",
],
"Uruguayan": [
"urugayan",
"uraguayan",
"uragayan",
"urogwayan",
"uruguauyan",
"uruguaryan",
]
}
replies = [
"Did you mean **{}**?",
"I'm pretty sure you meant **{}**",
]
def update_log(id, log_path):
""" For comments that were already replied"""
with open(log_path, 'a') as my_log:
my_log.write(id + "\n")
def load_log(log_path):
""" For comments that were already replied"""
with open(log_path) as my_log:
log = my_log.readlines()
log = [x.strip('\n') for x in log]
return log
def output_log(text):
""" Used for debugging."""
output_log_path = './logs/{}_output_log.txt'.format(
datetime.date.today().strftime('%Y_%m')
)
with open(output_log_path, 'a') as myLog:
s = "[{}]: {}\n".format(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), text
)
myLog.write(s)
def find_substring(needle, haystack):
""" Only returns true if needle is found and it's a whole word."""
index = haystack.find(needle)
if index == -1:
return False
if index != 0 and haystack[index-1] in string.letters:
return False
L = index + len(needle)
if L < len(haystack) and haystack[L] in string.letters:
return False
return True
def is_quote(text):
return len(text) > 1 and text[0] == '>'
def check_misspells(text, misspells):
for version in misspells:
if find_substring(version, text.lower()):
return True
def get_reply():
return random.choice(replies)
def check_condition(comment):
""" Has the bot been called?"""
paragraphs = [
paragraph for paragraph in comment.body.split('\n')
if not is_quote(paragraph)
]
for paragraph in paragraphs:
# Separate by paragraphs to avoid triggering by quoted text.
for word in misspells:
if check_misspells(paragraph, misspells[word]):
return get_reply().format(word)
signature = (
"\n\n Script by \/u/Sevg, hosting by \/u/DirkGentle"
"*^and ^yes, ^weed ^is ^legal ^here*"
)
source = "\n\n [Source.](https://github.com/sevgit/Its_URUGUAY_bot)"
epilogue = "\n\n*****\n\n{}\n\n{}".format(signature, source)
print("RUNNING")
if __name__ == "__main__":
while True:
try:
output_log("Starting the script")
log = load_log(comment_log_path)
reddit = praw.Reddit(
client_id=botlogin.client_id,
client_secret=botlogin.client_secret,
password=botlogin.password,
user_agent=(
"Its_URUGUAY_bot script by "
"Sevg/Dirkgentle/ElectrWeakHyprCharge"
),
username=botlogin.username
)
print("Logged to reddit as {}".format(reddit.user.me().name))
output_log("Logged to reddit as: {}".format(reddit.user.me().name))
for comment in reddit.subreddit('all').stream.comments():
reply = check_condition(comment)
if reply and comment.id not in log:
comment.reply("{} {}".format(reply, epilogue))
output_log("{{{}}}".format(reply))
log.append(comment.id)
update_log(comment.id, comment_log_path)
except Exception as exception:
output_log(str(exception))
output_log(traceback.format_exc())