-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredditflairenforcer.py
73 lines (59 loc) · 2.71 KB
/
redditflairenforcer.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
import praw
from prawcore import PrawcoreException
import time
import json
from threading import Thread
no_flair_removal = """
**Unfortunately, we've had to remove your post.**
___
___
[**Here are our subreddit rules.**](https://www.reddit.com/r/{}/wiki/rules) - If you have any queries about this, you can contact us via [Moderator Mail](https://www.reddit.com/message/compose?to=%2Fr%2F{})."""
class RedditFlairEnforcer:
def __init__(self):
self.start_time = time.time()
self.reddit = self.login()
print('--- logged in to reddit')
self.post_storage = []
self.thread_storage = {}
print('--- storage initalized')
for sub in config['reddit']['subreddits']:
self.thread_storage[sub] = Thread(target=self.get_posts, args=[sub])
self.thread_storage[sub].start()
Thread(target=self.check_flair).start()
print(f'--- {len(self.thread_storage)} threads started and stored')
print('--- post processing started')
def login(self):
return praw.Reddit(
client_id=config['reddit']['id'],
client_secret=config['reddit']['secret'],
user_agent=config['reddit']['agent'],
username=config['reddit']['username'],
password=config['reddit']['password']
)
def get_posts(self, sub):
try:
for post in self.reddit.subreddit(sub).stream.submissions():
if post.created_utc - self.start_time > 0:
self.post_storage.append({'key': post.id, 'sub': sub, 'time': post.created_utc})
except PrawcoreException as e:
print(f'exception: {e}')
def check_flair(self):
while True:
try:
filtered = filter(lambda x: x['time'] + 30 * 60 - time.time() < 0, self.post_storage)
for data in filtered:
post = self.reddit.submission(data['key'])
self.post_storage.remove(data)
if post.link_flair_text is None or post.link_flair_css_class is None:
post.reply(no_flair_removal.format(data['sub'], data['sub'])).mod.distinguish(how='yes', sticky=True)
post.mod.remove()
post.mod.lock()
print(f'post removed (no flair): {post.id} | {time.time()}')
else:
print(f'all checks passed: {post.id} ({post.subreddit.display_name}, {post.domain}, {post.link_flair_css_class})')
except PrawcoreException as e:
print(f'exception: {e}')
if __name__ == '__main__':
with open('./config.json') as config_file:
config = json.load(config_file)
RedditFlairEnforcer()