-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrw.py
executable file
·373 lines (335 loc) · 12.5 KB
/
rw.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#! /usr/bin/env python3
# pylint: disable=missing-module-docstring,missing-function-docstring,invalid-name,no-member,unused-argument,unused-variable,missing-class-docstring,line-too-long
import datetime
import os
import random
import sys
import wx
import praw
class mainWindow(wx.Frame):
def __init__(self, parent, title):
super(mainWindow, self).__init__(
parent, title=title, style=wx.DEFAULT_FRAME_STYLE, size=(525, 600)
)
self.Centre()
self.InitUI()
def InitUI(self):
self.CreateStatusBar()
panel = wx.Panel(self)
sizer = wx.GridBagSizer(5, 5)
st1 = wx.StaticText(panel, label="Username:")
sizer.Add(
st1,
pos=(0, 0),
flag=wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL,
border=5,
)
self.uname = wx.TextCtrl(panel)
username = os.environ["REDDIT_USERNAME"]
self.uname.SetValue(username)
sizer.Add(self.uname, pos=(0, 1), flag=wx.ALL | wx.ALIGN_LEFT, border=5)
st2 = wx.StaticText(panel, label="Password:")
sizer.Add(
st2,
pos=(0, 2),
flag=wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL,
border=5,
)
self.pword = wx.TextCtrl(panel)
password = os.environ["REDDIT_PASSWORD"]
self.pword.SetValue(password)
sizer.Add(self.pword, pos=(0, 3), flag=wx.ALL | wx.ALIGN_LEFT, border=5)
btn1 = wx.Button(panel, label="Update")
sizer.Add(btn1, pos=(1, 4), flag=wx.ALL, border=5)
self.comments = wx.CheckBox(panel, label="Wipe comments")
sizer.Add(
self.comments, pos=(1, 2), flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=5
)
self.submissions = wx.CheckBox(panel, label="Wipe submissions")
sizer.Add(
self.submissions,
pos=(1, 1),
flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL,
border=5,
)
self.verbose = wx.CheckBox(panel, label="Verbose output")
self.verbose.SetValue(True)
sizer.Add(
self.verbose, pos=(1, 3), flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=5
)
btn2 = wx.Button(panel, label="Wipe")
sizer.Add(btn2, pos=(2, 4), flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=5)
self.rbox = wx.RadioBox(
panel,
label="Age for deletion",
style=wx.RA_SPECIFY_ROWS,
choices=("Any age", "Older than X minutes"),
majorDimension=1,
)
sizer.Add(self.rbox, pos=(2, 0), span=(0, 3), flag=wx.ALL, border=5)
self.age = wx.SpinCtrl(
panel,
value="0",
max=99999999,
name="Min age to delete",
style=wx.ALIGN_RIGHT,
)
sizer.Add(self.age, pos=(2, 3), flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL)
legendtext = "".join(
"1 day = {:,} minutes, 1 month = {:,} minutes, 1 year = {:,} minutes".format(
60 * 24, 60 * 24 * 30, 60 * 24 * 365
)
)
legend = wx.StaticText(panel, label=legendtext)
sizer.Add(
legend,
pos=(3, 0),
span=(0, 5),
flag=wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL,
border=5,
)
self.found = wx.TextCtrl(panel)
sizer.Add(
self.found,
pos=(4, 0),
span=(0, 5),
flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TE_READONLY,
border=5,
)
self.tc2 = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.TE_READONLY)
sizer.Add(
self.tc2,
pos=(5, 0),
span=(0, 5),
flag=wx.EXPAND | wx.LEFT | wx.RIGHT,
border=5,
)
sizer.AddGrowableRow(5)
panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.Login, id=btn1.GetId())
self.Bind(wx.EVT_BUTTON, self.Process, id=btn2.GetId())
self.Bind(wx.EVT_SPINCTRL, self.UpdateAge)
self.loggedin = False
def UpdateAge(self, e):
if self.age.GetValue() > 0:
self.rbox.SetSelection(1)
else:
self.rbox.SetSelection(0)
def OnQuit(self, e):
self.Close()
def Process(self, e):
sb = self.GetStatusBar()
if self.submissions.GetValue:
sb.SetStatusText("Wiping submissions ...")
self.start_delete_submissions(self)
if self.comments.GetValue:
sb.SetStatusText("Wiping comments ...")
self.start_delete_comments(self)
self.Update_counts()
def get_comment_total(self, e):
if self.rbox.GetSelection() == 1:
age = self.age.GetValue()
else:
age = 0
comment_count = 0
textbox = self.tc2
now = datetime.datetime.now().timestamp()
expire_seconds = age * 60
if self.verbose.GetValue():
if age > 0:
textbox.AppendText(
"Looking for comments older than {:0,} minutes from {}\n".format(
age, self.uname.GetValue()
)
)
else:
textbox.AppendText(
"Looking for all comments from {}\n".format(self.uname.GetValue())
)
for reply in self.reddit.redditor(self.uname.GetValue()).comments.new(
limit=self.limitation
):
comment_age = now - reply.created_utc
if self.verbose.GetValue():
subreddit = reply.subreddit
textbox.AppendText(
"Found {} in /r/{} it is {:,} minutes old.\n".format(
reply.id, subreddit, int(comment_age / 60)
)
)
if age == 0 or comment_age > expire_seconds:
comment_count += 1
return comment_count
def get_submission_total(self, e):
age = self.age.GetValue()
submission_count = 0
textbox = self.tc2
now = datetime.datetime.now().timestamp()
expire_seconds = age * 60
if self.verbose.GetValue():
if age > 0:
textbox.AppendText(
"Looking for submissions older than {:0,} minutes from {}\n".format(
age, self.uname.GetValue()
)
)
else:
textbox.AppendText(
"Looking for all submissions from {}\n".format(
self.uname.GetValue()
)
)
for submission in self.reddit.redditor(self.uname.GetValue()).submissions.new(
limit=self.limitation
):
submission_age = now - submission.created_utc
if self.verbose.GetValue():
subreddit = submission.subreddit
textbox.AppendText(
"Found {} in /r/{} it is {:,} minutes old.\n".format(
submission.id, subreddit, int(submission_age / 60)
)
)
if age == 0 or submission_age > expire_seconds:
submission_count += 1
return submission_count
def start_delete_comments(self, e):
age = self.age.GetValue()
now = datetime.datetime.now().timestamp()
expire_seconds = age * 60
comment_count = self.get_comment_total(self)
textbox = self.tc2
while comment_count > 0:
for comment in self.reddit.redditor(self.uname.GetValue()).comments.new(
limit=self.limitation
):
comment_to_delete = self.reddit.comment(comment)
comment_age = now - comment_to_delete.created_utc
if age == 0 or comment_age > expire_seconds:
if self.verbose.GetValue():
textbox.AppendText(
"Working on comment {}: {}\n".format(
comment_to_delete.id, comment_to_delete.body[0:15]
)
)
for i in range(2):
comment_to_delete.edit(self.Random_words())
if self.verbose.GetValue():
textbox.AppendText(
"Working on comment {}: Changed text to {}\n".format(
comment_to_delete.id, comment_to_delete.body
)
)
textbox.AppendText(
"Deleted comment {}\n".format(comment_to_delete.id)
)
comment_to_delete.delete()
comment_count -= 1
def start_delete_submissions(self, e):
age = self.age.GetValue()
now = datetime.datetime.now().timestamp()
expire_seconds = age * 60
submission_count = self.get_submission_total(self)
textbox = self.tc2
while submission_count > 0:
for submission in self.reddit.redditor(
self.uname.GetValue()
).submissions.new(limit=self.limitation):
submission_to_delete = self.reddit.submission(submission)
submission_age = now - submission_to_delete.created_utc
if age == 0 or submission_age > expire_seconds:
if self.verbose.GetValue():
textbox.AppendText(
"Working on submission {}: {}".format(
submission_to_delete.id, submission_to_delete.title
)
)
if submission_to_delete.post_hint == "self":
textbox.AppendText(" text post\n")
for i in range(2):
submission_to_delete.edit(self.Random_words())
if self.verbose.GetValue():
textbox.AppendText(
"Working on submission {}: Changed text to {}\n".format(
submission_to_delete.id,
submission_to_delete.selftext,
)
)
else:
textbox.AppendText(" link post, cannot overwrite\n")
textbox.AppendText(
"Deleted submission {}\n".format(submission_to_delete.id)
)
submission_to_delete.delete()
submission_count -= 1
def Update_counts(self):
sb = self.GetStatusBar()
sb.SetStatusText("Getting submission/comment count ...")
comments = self.get_comment_total(self.reddit)
submissions = self.get_submission_total(self.reddit)
result = "Found {} submissions, {} comments".format(submissions, comments)
self.found.SetValue(result)
sb.SetStatusText(result)
def Login(self, e):
sb = self.GetStatusBar()
sb.SetStatusText("Logging in...")
try:
clientid = os.environ["REDDIT_CLIENT_ID"]
clientsecret = os.environ["REDDIT_CLIENT_SECRET"]
except KeyError:
print(
"Environment variables REDDIT_CLIENT_ID and REDDIT_CLIENT_SECRET must be set."
)
sys.exit(1)
try:
user_agent = os.environ["REDDIT_USER_AGENT"]
except KeyError:
user_agent = "redditwipe"
self.limitation = None
self.reddit = praw.Reddit(
client_id=clientid,
client_secret=clientsecret,
user_agent=user_agent,
username=self.uname.GetValue(),
password=self.pword.GetValue(),
)
sb.SetStatusText("Logged in.")
self.Update_counts()
self.loggedin = True
phonetic = [
"alfa",
"bravo",
"charlie",
"delta",
"echo",
"foxtrot",
"golf",
"hotel",
"india",
"juliett",
"kilo",
"lima",
"mike",
"november",
"oscar",
"papa",
"quebec",
"romeo",
"sierra",
"tango",
"uniform",
"victor",
"whiskey",
"x-ray",
"yankee",
"zulu",
]
def Random_words():
return " ".join(random.choices(phonetic, k=5))
def main():
app = wx.App()
frame = mainWindow(None, title="Reddit wipe utility")
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()