-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusernote_sync.py
204 lines (184 loc) · 7.67 KB
/
usernote_sync.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
"""Macros and toolbox integration for moderation"""
from base64 import b64decode
import json
import os
import sys
import time
from zlib import decompress
import praw
import prawcore
import slack_python_logging
# Translate modtoolbox note labels to new reddit labels
label_translation = {
"gooduser": "HELPFUL_USER",
"spamwatch": "SPAM_WATCH",
"spamwarn": "SPAM_WARNING",
"abusewarn": "ABUSE_WARNING",
"ban": "BAN",
"permban": "PERMA_BAN",
}
class UsernoteSync:
"""Main bot class"""
def __init__(self):
"""Initialize class, requires appropriate environment variables"""
self.reddit = praw.Reddit(
client_id = os.environ["client_id"],
client_secret = os.environ["client_secret"],
refresh_token = os.environ["refresh_token"],
user_agent = "linux:usernote_sync:v0.1 (by /u/jenbanim)"
)
self.subreddit = self.reddit.subreddit(os.environ["subreddit"])
self.logger = slack_python_logging.getLogger(
app_name = "usernote_sync",
stream_loglevel = "DEBUG",
slack_loglevel = "CRITICAL"
)
self.start_time = time.time()
def get_usernotes(self, after_epoch = 0):
"""Load and format a user's notes from subreddit wiki"""
usernotes = json.loads(self.subreddit.wiki["usernotes"].content_md)
constants = usernotes["constants"]
notes = json.loads(
decompress(b64decode(usernotes["blob"])).decode("utf-8")
)
new_notes = []
for user in notes:
for note in notes[user]["ns"]:
# Build new note
note_author = constants["users"][note["m"]]
if note["t"] < after_epoch:
# Disregard notes from before after_epoch
continue
note_time = time.strftime(
"%Y-%m-%d",
time.localtime(note["t"])
)
note_text = note["n"]
new_note_text = f"{note_time} | {note_author} | {note_text}"
# Build new label
note_label = constants["warnings"][note["w"]]
new_note_label = label_translation.get(note_label, None)
# Build thing
note_link_split = note["l"].split(",")
if len(note_link_split) == 1:
# No link
new_thing = None
if len(note_link_split) == 2:
# Submission link
submission_id = note_link_split[1]
new_thing = self.reddit.submission(submission_id)
if len(note_link_split) == 3:
# Comment link
comment_id = note_link_split[2]
new_thing = self.reddit.comment(comment_id)
new_notes.append({
"label": new_note_label,
"note": new_note_text[:250],
"redditor": self.reddit.redditor(user),
"subreddit": self.subreddit,
"thing": new_thing
#"thing": None
})
return new_notes
def get_usernotes_file(self, path, after_epoch = 0):
"""Load and format a user's notes from subreddit wiki"""
with open(path) as f:
usernotes = json.load(f)
#usernotes = json.loads(self.subreddit.wiki["usernotes"].content_md)
constants = usernotes["constants"]
notes = json.loads(
decompress(b64decode(usernotes["blob"])).decode("utf-8")
)
new_notes = []
for user in notes:
for note in notes[user]["ns"]:
# Build new note
note_author = constants["users"][note["m"]]
if note["t"] < after_epoch:
# Disregard notes from before after_epoch
continue
note_time = time.strftime(
"%Y-%m-%d",
time.localtime(note["t"])
)
note_text = note["n"]
new_note_text = f"{note_time} | {note_author} | {note_text}"
# Build new label
note_label = constants["warnings"][note["w"]]
new_note_label = label_translation.get(note_label, None)
# Build thing
note_link_split = note["l"].split(",")
if len(note_link_split) == 1:
# No link
new_thing = None
if len(note_link_split) == 2:
# Submission link
submission_id = note_link_split[1]
new_thing = self.reddit.submission(submission_id)
if len(note_link_split) == 3:
# Comment link
comment_id = note_link_split[2]
new_thing = self.reddit.comment(comment_id)
new_notes.append({
"label": new_note_label,
"note": new_note_text[:250],
"redditor": self.reddit.redditor(user),
"subreddit": self.subreddit,
"thing": new_thing
#"thing": None
})
return new_notes
def upload_notes(self, new_notes):
for note in new_notes:
while True:
print(f"{note['note'][:10]}")
try:
self.reddit.notes.create(**note)
break
except prawcore.exceptions.TooManyRequests:
# I hate the API. I hate the API. I hate the API.
time.sleep(1)
continue
except praw.exceptions.RedditAPIException:
# Account deleted or suspended
break
def delete_notes(self, new_notes):
# Deletes notes from subreddit using the users in the new_notes list
# Intended as an undo action in case something goes wrong
users = list(set([note["redditor"].name for note in new_notes]))
me = self.reddit.user.me()
while users:
for user in users:
print(f"{len(users)} users remaining")
try:
for note in self.subreddit.mod.notes.redditors(user, limit=None):
if note.type != "NOTE":
continue
if note.moderator != me:
continue
note.delete()
users.remove(user)
except prawcore.exceptions.TooManyRequests:
time.sleep(5)
except praw.exceptions.RedditAPIException:
# Account deleted or suspended
users.remove(user)
except prawcore.exceptions.NotFound:
# Perhaps a deleted note? Unsure
users.remove(user)
if __name__ == "__main__":
"""Main program loop. Create a UsernoteSync instance and listen continuously."""
usernote_sync = UsernoteSync()
# Log uncaught exceptions to Slack before exiting
def log_excepthook(ex_type, ex_value, ex_traceback):
usernote_sync.logger.critical(
"Critical Exception caught, exiting",
exc_info=(ex_type, ex_value, ex_traceback)
)
sys.excepthook = log_excepthook # Wish I could use a lambda :/
after_epoch = time.time() # First run will be useless, but whateva
while True:
new_usernotes = usernote_sync.get_usernotes(after_epoch = after_epoch)
usernote_sync.upload_notes(new_usernotes)
after_epoch = time.time() # set for our next run
time.sleep(60)