-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrikeCounter.py
244 lines (213 loc) · 10.4 KB
/
StrikeCounter.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
import RedditThread
import DataBase
import datetime
import time
import utilitymethods
import Actions
import logging
import Blacklist
import traceback
import re
class StrikeCounter(RedditThread.RedditThread):
"""
:type policy: DefaultPolicy
:type owner: CentralScrutinizer.CentralScrutinizer
"""
def __init__(self, owner, recount_strikes=False):
super(StrikeCounter, self).__init__(owner, owner.policy)
self.owner = owner
self.policy = owner.policy
self.database_file = self.owner.database_file
self.praw = utilitymethods.create_multiprocess_praw(self.owner.credentials)
self.sub = utilitymethods.get_subreddit(self.owner.credentials, self.praw)
self.blacklists = self.owner.blacklists
self.recount_strikes = recount_strikes
if self.recount_strikes:
with DataBase.DataBaseWrapper(self.database_file, False) as db:
db.cursor.execute('update channel_record set strike_count = 0')
db.cursor.execute('update reddit_record set processed = 0')
db.cursor.execute('update reddit_record set exception = 0')
db.cursor.execute('update reddit_record set old = 0')
self.regex_list = [ re.compile(r'^(?:\*\*)?/u/([\w\d_\-\*]+)[,\s]'), #Automod / removal reason
re.compile(r'^All apologies /u/([\w\d_\-\*]+)[,\s]'), #raddit
re.compile(r'/u/([\w\d\*-_]+), your submission') #generic
]
def check_for_submitter(self, post):
#check comments for submitter id
for comment in post.comments:
if comment.author and comment.distinguished == 'moderator': #not deleted, and from mod
for regex in self.regex_list:
search = regex.search(comment.body)
if search:
return search.group(1)
def check_exception(self, post):
try:
#check for link flair
if post.link_flair_css_class is None:
return True
except:
pass
try:
#check for removal reason
if post.removal_reason is not None:
return True
except:
pass
#check top level comments for specific keyword matches
try:
success = True
#check comments
for comment in post.comments:
#test comment
if not Actions.is_deleted(comment) and comment.distinguished == 'moderator':
#test keyword
for exception in self.policy.exception_list:
if re.search(exception, comment.body):
return True
return False
except Exception, e:
success = False
time.sleep(1)
def __get_unique(self, seq, seq2):
tally = []
for i,item in enumerate(seq):
tup = (seq[i], seq2[i])
if not any(val == tup for val in tally):
tally.append(tup)
return [val for val in tally]
def scan(self):
"""
Scans the previously collected reddit posts for deleted posts
"""
#scan old messages, see if deleted
with DataBase.DataBaseWrapper(self.database_file, False) as db:
now = datetime.datetime.now()
global_strike_date = now - self.policy.Strike_Counter_Global_Strike_History
history_date = now - self.policy.Strike_Counter_Scan_History
entries = db.get_reddit(date_added=history_date, processed=0, return_dateadded=True)
if entries is None:
logging.warning(u"No reddit entries found in database...")
return
new_strike_channels = []
#loop over entries
stride = 100
while len(entries):
num_loaded = min(stride, len(entries))
(ids, channels, domains, add_dates) = zip(*entries[:num_loaded])
ids = list(ids)
#see if we have submitters
have_submitters = db.have_submitter(ids)
#any new detected usernames go here
new_submitters_list = []
channels = list(channels)
domains = list(domains)
loaded = Actions.get_by_ids(self.praw, ids)
if not loaded:
logging.info(u"Historical posts not loaded...")
return
#make sure posts retrieved
posts = [post for post in loaded]
if not posts:
logging.info(u"Bad post retrieve")
return
#make sure channels exist
add_channels = []
exists = db.channel_exists([(channel, domains[i]) for i, channel in enumerate(channels)])
for i, e in enumerate(exists):
if not e:
#pull up the url
add_channels.append((channels[i], domains[i]))
#resolve all the added ids
if add_channels:
if not db.add_channels(add_channels):
logging.info(u"Error adding channels to channel_record, skipping processing of posts")
continue #if there was an error adding the channels, don't mark as processed
#check for deleted / exceptions
increment_posts = {}
processed_posts = []
excepted_posts = []
for i, post in enumerate(posts):
if Actions.is_deleted(post):
if not have_submitters[i]:
val = self.check_for_submitter(post)
if val is not None:
new_submitters_list.append((val, ids[i]))
if not self.check_exception(post):
#self.policy.info(u"Deleted post found {}".format(post.name), u"channel = {}, domain = {}".format(channels[i], domains[i]))
if add_dates[i] > global_strike_date or self.recount_strikes:
if not (channels[i], domains[i]) in increment_posts:
increment_posts[(channels[i], domains[i])] = 1
else:
increment_posts[(channels[i], domains[i])] += 1
if not (channels[i], domains[i]) in new_strike_channels:
new_strike_channels.append((channels[i], domains[i]))
else:
excepted_posts.append(post.name)
processed_posts.append(post.name)
if len(increment_posts):
#add strikes
db.add_strike([(increment_posts[key],) + key for key in increment_posts])
if __debug__:
logging.info(u"Strike Counter found {} new deleted posts...".format(len(increment_posts)))
if len(increment_posts) or len(excepted_posts):
#remove from consideration (so we don't count them over and over)
db.set_processed(processed_posts)
db.set_exception(excepted_posts)
#update submitters
if len(new_submitters_list):
db.update_submitter(new_submitters_list)
#forget old entries
entries = entries[num_loaded:]
#check for rule breaking channels
channels = db.get_channels(strike_count=self.policy.Strike_Count_Max, blacklist=Blacklist.BlacklistEnums.NotFound)
if channels and len(channels):
if __debug__:
logging.info(u"{} new channels added to the blacklist".format(len(channels)))
db.set_blacklist(channels, Blacklist.BlacklistEnums.Blacklisted, self.owner.credentials['USERNAME'],
u"Global strike count exceeded")
#check for user strike counts
user_strikes = db.max_processed_from_user(not_found_value=Blacklist.BlacklistEnums.NotFound,
strike_limit=self.policy.User_Strike_Count_Max)
if len(user_strikes):
reason_list = [u"User strike count exceeded by {} ({} strikes counted) for channel {} on domain {}" \
u"".format(user[1], user[0], user[2], user[3]) for user in user_strikes]
new_blacklist = [(user[2], user[3]) for user in user_strikes]
db.set_blacklist(new_blacklist, Blacklist.BlacklistEnums.Blacklisted, self.owner.credentials['USERNAME'],
reason_list)
#update global strike counts
#find posts older than scan period marked as processed
old_strikes = db.processed_older_than(global_strike_date, old_flag=0)
if old_strikes is not None and len(old_strikes):
decrement_count = {}
for pair in old_strikes:
if not pair in decrement_count:
decrement_count[pair] = 0
decrement_count[pair] += 1
#and remove them from the count
db.subtract_strikes_and_mark([(decrement_count[pair],) + pair for pair in decrement_count], global_strike_date)
#remove older than scan period
db.remove_reddit_older_than(history_date)
#turn off recount if true
if self.recount_strikes:
self.recount_strikes = False
logging.info(u'Strike recount completed successfully')
if __debug__:
logging.info(u"Strike count completed successfully at {}".format(datetime.datetime.now()))
def run(self):
while True:
#check for pause
if not self.check_status():
break
try:
self.scan()
except Exception, e:
logging.error(u"Exception occured while scanning old reddit posts")
if __debug__:
logging.exception(e)
self.log_error()
#only sleep 5 mionutes before retrying
time.sleep(self.policy.Scan_Error_Pause)
continue
time.sleep(self.policy.Strike_Counter_Frequency)
def shutdown(self):
pass