This repository has been archived by the owner on Apr 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
collapse.py
194 lines (127 loc) · 5.68 KB
/
collapse.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
import re
import sys
import praw
import time
from datetime import datetime as dt, timedelta as td, date
# This is a file in the same folder (called config.py)
import config
# Created by /u/epicmindwarp
# 2020-04-03
RGX_SENTENCE_3 = r'(?:.*?\.)(?:.*?\.)(?:.*?\.)' # Don't touch if you don't understand
SUB_NAME = 'collapse' # Set subreddit here
USER_AGENT = f'Post Removal Bot Bot for /r/{SUB_NAME} - v0.1' # Info for reddit API
MINIMUM_HOURS = 2 # Number of hours a post must be
SLEEP_SECONDS = 300 # Number of seconds to sleep between scans (300 = 5 minutes)
REMOVAL_REPLY = '''
Your post has been removed due to failing to submit a submission statement within 2 hours of posting.
'''
def reddit_login():
print('Connecting to reddit...')
try:
reddit = praw.Reddit( client_id= config.client_id,
client_secret= config.client_secret,
user_agent=USER_AGENT,
username=config.username,
password=config.password)
except Exception as e:
print(f'\t### ERROR - Could not login.\n\t{e}')
print(f'Logged in as: {reddit.user.me()}')
return reddit
def get_latest_submissions(subreddit):
print(f'Getting posts from {SUB_NAME}...')
submissions = subreddit.new(limit=10)
return submissions
def check_submissions(submissions, valid_posts):
for submission in submissions:
# Ignore self posts
if submission.is_self:
continue
# Get the UTC unix timestamp
ts = submission.created_utc
# Convert to datetime format
post_time = dt.utcfromtimestamp(ts)
# Skip any post before today
if post_time <= dt(2020, 4, 19, 0, 0):
continue
# Print a line break between each post
print('\n')
# Current the current UTC time
current_time = dt.utcnow()
# Number of whole hours (seconds / 60 / 60) between posts
hours_since_post = int((current_time - post_time).seconds / 3600)
print(f'{post_time} - ({hours_since_post} hrs) - {submission.title}')
# Check if we've already marked this as valid
if submission.id in valid_posts:
print('\t # Already checked - valid.')
# Go to next loop
continue
# Check if passed the minimum
if hours_since_post >= MINIMUM_HOURS:
# Once the minimum has passed
# Create a flag, if this stays False, post to be removed
op_left_correct_comment = False
# Get all top level comments from the post
for top_level_comment in submission.comments:
# Look for a comment by the author
if top_level_comment.is_submitter:
print('\tOP has commented')
# Reset the variable
match_found = None
# Grab the body
comment_body = top_level_comment.body
# Check if it matches our regex - multiline not required as it displays \n line breaks
match_found = re.search(RGX_SENTENCE_3, comment_body)
# If there is no match fiound
if not match_found is None:
# Flag as correct
op_left_correct_comment = True
# Leave this loop
break
# Check if the flag has changed
if not op_left_correct_comment:
print('\tOP has NOT left a valid comment!')
# # Remove and lock the post
submission.mod.remove()
submission.mod.lock()
# # Leave a comment and remove it
removal_comment = submission.reply(REMOVAL_REPLY)
removal_comment.mod.lock()
removal_comment.mod.distinguish(how='yes', sticky=True)
print('\t# Post removed.')
else:
# If correct, add to exceptions list
print('\t # Post valid')
valid_posts.append(submission.id)
# Send back the posts we've marked as valid
return valid_posts
############################################################################
############################################################################
############################################################################
# Bot starts here
if __name__ == "__main__":
try:
# Connect to reddit and return the object
r = reddit_login()
# Connect to the sub
subreddit = r.subreddit(SUB_NAME)
except Exception as e:
print('\t\n### ERROR - Could not connect to reddit.')
sys.exit(1)
# A list of posts already valid, keep this in memory so we don't keep checking these
valid_posts = []
# Loop 4eva
while True:
try:
# Get the latest submissions after emptying variable
submissions = None
submissions = get_latest_submissions(subreddit)
except Exception as e:
print('\t### ERROR - Could not get posts from reddit')
# If there are posts, start scanning
if not submissions is None:
# Once you have submissions, check valid posts
valid_posts = check_submissions(submissions, valid_posts)
# Loop every X seconds (5 minutes)
sleep_until = (dt.now() + td(0, SLEEP_SECONDS)).strftime('%H:%M:%S') # Add 0 days, 300 seconds
print(f'\nSleeping until {sleep_until}') #%Y-%m-%d
time.sleep(SLEEP_SECONDS)