-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsave_usernotes.py
106 lines (96 loc) · 3.04 KB
/
save_usernotes.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
import json
import base64
import zlib
import re
import os
import sys
from datetime import datetime
subreddit = "SubTestBot1"
original_input_filename = "input.txt"
csv_filename = "notes.csv"
output_filename = "output.txt"
if not os.path.isfile(original_input_filename):
print(f"Input file {original_input_filename} is missing, please copy it down from the subreddit again")
sys.exit()
with open(original_input_filename, 'r') as input_file:
text = input_file.readline()
data = json.loads(text)
moderators = {}
for i, moderator in enumerate(data["constants"]["users"]):
moderators[moderator] = i
reasons = {}
for i, reason in enumerate(data["constants"]["warnings"]):
if reason is None:
reason = "None"
reasons[reason] = i
users = {}
note_count = 0
with open(csv_filename, 'r') as csv_file:
for line in csv_file:
note_count += 1
items = line.strip().split(",")
username = items[0]
date_time = datetime.strptime(items[1], "%Y-%m-%d %H:%M:%S")
if items[2] in moderators:
moderator = moderators[items[2]]
else:
print(f"Adding new moderator to list: {items[2]}")
data["constants"]["users"].append(items[2])
moderators[items[2]] = len(data["constants"]["users"]) - 1
reason_string = items[3]
if items[3] in reasons:
reason = reasons[items[3]]
else:
print(f"Adding new reason to list: {items[3]}")
data["constants"]["warnings"].append(items[3])
reasons[items[3]] = len(data["constants"]["warnings"]) - 1
if items[4] is not None:
groups = re.search(r'(?:comments/)(\w{3,8})(?:/_/)(\w{3,8})', items[4], flags=re.IGNORECASE)
if groups:
link_id = groups.group(1)
comment_id = groups.group(2)
link = f"l,{link_id},{comment_id}"
else:
groups = re.search(r'(?:comments/)(\w{3,8})', items[4], flags=re.IGNORECASE)
if groups:
link_id = groups.group(1)
link = f"l,{link_id}"
else:
groups = re.search(r'(?:message/messages/)(\w{3,8})', items[4], flags=re.IGNORECASE)
if groups:
message_id = groups.group(1)
link = f"m,{message_id}"
else:
link = items[4]
else:
link = ""
note_text = items[5]
if note_text.startswith("\""):
note_text = ','.join(items[5:])[1:-1]
note = {
'n': note_text,
't': int(date_time.timestamp()),
'm': moderator,
'l': link,
'w': reason
}
if username in users:
users[username]['ns'].append(note)
else:
users[username] = {'ns': [note]}
print(f"Loaded {note_count} notes across {len(users)} users")
output_dict = {
"ver": data["ver"],
"constants": {
"users": data["constants"]["users"],
"warnings": data["constants"]["warnings"]
}
}
blob = json.dumps(users).encode()
compressed_blob = zlib.compress(blob)
encoded_blob = base64.b64encode(compressed_blob).decode()
output_dict['blob'] = str(encoded_blob)
with open(output_filename, 'w') as output_file:
output_file.write(json.dumps(output_dict))
print(f"Dumped out to {output_filename}. Copy that back to https://www.reddit.com/r/{subreddit}/wiki/edit/usernotes")
print(f"You may need to go to the toolbox settings and click 'Clear your cache' before you can see the changes")