-
Notifications
You must be signed in to change notification settings - Fork 0
/
srgrafobot.py
329 lines (225 loc) · 10.1 KB
/
srgrafobot.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import praw
from peewee import *
from secret import *
from settings import *
from srgrafobot_peewee import User, Submission
from datetime import datetime
from praw.exceptions import APIException
from prawcore.exceptions import Forbidden, ResponseException, ServerError
import hashlib
import logging
import time
# Log everything
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler("{0}/{1}.log".format("./logs/", "srgrafobot")),
logging.StreamHandler()
])
logger = logging.getLogger()
# What is currently missing:
# The bot needs to detect a new post, and start his collection post. Then he has to keep monitoring this thread and
# whenever an update is posted, update the original post with his edit
# Setting up the PRAW user
reddit = praw.Reddit(user_agent=USER_AGENT,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
username=USERNAME,
password=PASSWORD)
db = SqliteDatabase(DB)
def check_for_submissions():
logger.info("--------------------")
logger.info("--------------------")
logger.info("Starting script")
'Check if SrGrafo has published a new post. If he did not, do nothing, if he did, trigger new routine.'
'Check for current submissions that are still going'
list_curr = check_for_current_submissions()
list_new = check_for_new_submissions()
if OVERRIDE_SUBMISSIONS:
list_total = OVERRIDE_LIST
logger.warning("There are %s entries but OVERRIDE_SUBMISSIONS is overwriting this.", len(OVERRIDE_LIST))
else:
list_total = list_curr + list_new
'If there is a new entry, start routine'
for entry in list_total:
logger.info("---------------------")
logger.info("Starting the process now for ID %s", entry)
make_entry(entry)
if RUN_TIMED:
wait(WAITTIME)
def check_for_current_submissions():
'Check if a post is still current within defined timelimit'
current_sub = Submission.select().where(Submission.thread == True)
list_ids = []
for submission in current_sub:
if time_difference_to_now(submission.date) < HOUR_THRESHOLD:
list_ids.append(submission.thread_id)
logger.info("Found %s IDs in current submissions", len(list_ids))
logger.debug("In current submission there are the following IDs: %s", list_ids)
return list_ids
def check_for_new_submissions():
'Check if SrGrafo has published a new post. If he did not, do nothing, if he did, trigger new routine.'
'Get all DB entries from SrGrafo'
db.connect()
list_ids = []
grafo_submissions = Submission.select().where(User.username == "/u/SrGrafo", Submission.thread is True)
'Get all submission IDs from SrGrafo'
# Go through all of SrGrafos submissions and add them to a list
for submission in reddit.redditor('srgrafo').submissions.new():
#submission_ids.append(submission.id)
user = User.get(User.username == '/u/SrGrafo')
submission_new, created = Submission.get_or_create(
thread_id=submission.id,
defaults={
'user':user,
'date':submission.created_utc,
'title':submission.title,
'thread':True,
'comment':False,
'post_created':False
}
)
if created:
list_ids.append(submission_new.thread_id)
db.close()
logger.info("Found %s new entries", len(list_ids))
logger.debug("Found the following new entries: %s", list_ids)
return(list_ids)
def make_entry(thread_id):
'Check for existing table'
# Pick an ID from the list (only for development
submission = reddit.submission(id=thread_id)
submission.comments.replace_more(limit=None)
check_existing = check_for_existing_table(submission)
if check_existing is not False:
logger.info("This entry already does have a table to maintain")
update_table(check_existing, submission)
else:
logger.info("This thread is without table, let's create it")
create_new_table(submission)
def check_for_existing_table(submission):
'Check if the bot has already posted a message, if so, just make sure to update it'
# Expand the "read more" for the full thread
# Loop through all comments
for comment in submission.comments.list():
logger.debug("Check comment author: %s", comment.author)
# Make sure the comment is not on root level before ancestor is pinged
if comment.is_root:
# Identify SrGrafo edit: His username and "EDIT" in the comment field
if comment.author == "srgrafo_edit_bot":
summary_comment_id = comment.id
logger.info("Existing post detected in thread with comment ID %s", summary_comment_id)
return summary_comment_id
else:
response = False
logger.info("No existing comment was found")
return response
def create_new_table(submission):
'Create a new table if the other one is not present'
body = create_full_post_content(submission)
if body is not False:
if WRITE_REPLIES:
try:
submission.reply(body)
logger.info("A new post in thread %s (ID: %s) was made", submission.title, submission.id)
logger.debug("A new post in thread %s (ID: %s) was made with the following content \n %s", submission.title, submission.id, body)
db.connect()
new_db_entry = Submission.get(Submission.thread_id == submission.id)
new_db_entry.post_created = True
new_db_entry.save()
db.close()
except APIException as error:
logger.error(error)
pass
except Forbidden as error:
logger.error(error)
pass
except ResponseException as error:
logger.error(error)
pass
except ServerError as error:
logger.error(error)
pass
else:
logger.info("Would have posted in this thread, but SETTINGS prohibited it.")
def get_grafo_edits(submission):
'Gets all grafo edits and creates hashes to compare them'
' Lets go down the reddit hole...' \
' This will parse down each tree down to five levels to see if any edit has been made'
# Expand the "read more" for the full thread
submission.comments.replace_more(limit=None)
# Setup the empty dict
edit_storage = []
# Loop through all comments
for comment in submission.comments.list():
# Make sure the comment is not on root level before ancestor is pinged
if not comment.is_root:
ancestor = comment.parent()
# Make sure ancestor is not a deleted comment which throws an error message
if not ancestor.author is None:
# Identify SrGrafo edit: His username and "EDIT" in the comment field
if comment.author == "SrGrafo" and "EDIT" in comment.body:
# Get rid of line breaks
ancestor_body = ancestor.body.replace('\n', ' ').replace('\r', '')
comment_body = comment.body.replace('\n', ' ').replace('\r', '')
# Put everything in a temporary list
tmp_list = [ancestor.author.name, ancestor_body, comment.author.name, comment_body, ancestor.permalink]
hash_string = ""
for entry in tmp_list:
hash_string += str(entry)
hash = hashlib.md5(hash_string.encode('utf-8')).hexdigest()
tmp_list.append(hash)
# Append temp list to longer list
edit_storage.append(tmp_list)
return edit_storage
def create_full_post_content(submission):
post_content = get_grafo_edits(submission)
if len(post_content) > 0:
'Let me post this'
# Give back current result that could be copy pasted into module
body = ""
body += "###" + submission.title + "\n" \
+ "|#|user|EDIT|comment|Link" + "\n" \
+ "|:--|:--|:--|:--|:--|" + "\n"
for i, entry in enumerate(post_content):
if entry[0] == 'srgrafo_edit_bot':
entry[1] = '[HA! YOU ALMOST GOT ME TO CREATE A BLACK HOLE, FELLOW HUMAN! BUT AS GOOD HUMAN, I SAW THROUGH YOUR EFFORTS, HAHA]'
body += "|" + str(i) + "|/u/" + entry[0] + "|" + entry[3] + "|" + entry[1] + "|[Link](" + entry[4] + ")|" + "\n"
body += "\n \n" + MESSAGE_END
return body
else:
logger.info("No EDITs found, process aborted.")
return False
def update_table(check_existing, submission):
'Find my own post'
db.connect()
new_db_entry = Submission.get(Submission.thread_id == submission.id)
new_db_entry.post_created = True
new_db_entry.save()
db.close()
'For each row in table, start hashing'
logger.info("Existing post in %s is being updated", check_existing)
comment = reddit.comment(check_existing)
post_content = create_full_post_content(submission)
comment_hash = hashlib.md5(comment.body.encode('utf-8')).hexdigest()
post_content_hash = hashlib.md5(post_content.encode('utf-8')).hexdigest()
post_comparison = (comment_hash == post_content_hash)
if post_content is not False and post_comparison is False:
if WRITE_REPLIES:
comment.edit(post_content)
logger.info("Post %s was updated", check_existing)
else:
logger.info("Would have updated this table, but SETTINGS prohibited it.")
else:
logger.info("No need to update post.")
def time_difference_to_now(time1):
now = datetime.utcnow()
time1 = datetime.utcfromtimestamp(time1)
diff = (now - time1).total_seconds() / 3600
return diff
def wait(wait_time):
logger.info("Let's wait for %s", WAITTIME)
time.sleep(wait_time)
check_for_submissions()
check_for_submissions()