-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete.py
177 lines (163 loc) · 7.79 KB
/
delete.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
import time
import datetime
import praw
import os
from praw.exceptions import RedditAPIException
from authentication import *
from askDate import ask_for_date
from manageText import manageText
def delete(flag_SubDate, flag_commentsPosts, flag_beforeAfter, flag_deleteAndEdit):
reddit = praw.Reddit(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
password=PASSWORD,
user_agent=USER_AGENT,
username=USERNAME
)
file = open("LOGS.md", "a+")
if os.path.getsize("LOGS.md") != 0:
file.write("\n---\n")
current_time = datetime.datetime.now()
time_string = current_time.strftime("%Y-%m-%d %H:%M:%S")
log_message = f"## Script executed at: {time_string}\n"
file.write(log_message)
else:
# LOGS.md wasn't found in the directory
print("No Previously stored Logs found. Creating a new LOGS.md...")
file.write(
"# Logs of using [RedditRefresh](https://github.com/karan51ngh/RedditRefresh)\n")
file.write("\n---\n")
current_time = datetime.datetime.now()
time_string = current_time.strftime("%Y-%m-%d %H:%M:%S")
log_message = f"## Script executed at: {time_string}\n"
file.write(log_message)
user = reddit.redditor(USERNAME)
if flag_SubDate == True:
subreddit_name = input("Enter Subbredit Name: ")
if flag_commentsPosts == True:
# from SUBREDDIT delete COMMENTS
print(f"Iterating over your Comments from {subreddit_name}")
try:
for comment in user.comments.new(limit=None):
if comment.subreddit.display_name.lower() == subreddit_name.lower():
hashed_text = manageText(
comment.body, file, flag_commentsPosts)
print(f"COMMENT: {comment.body}")
print(f"HASH: {hashed_text}")
comment.edit(hashed_text)
time.sleep(2)
if flag_deleteAndEdit:
comment.delete()
time.sleep(2)
file.write("\n---\n")
file.close()
except RedditAPIException as e:
print(
f"Reddit API Exception raised while deleting comments: [{e.error_type}: {e.message}]")
else:
# from SUBREDDIT delete POSTS
print(f"Iterating over your Posts from {subreddit_name}")
try:
for submission in user.submissions.new(limit=None):
if submission.subreddit.display_name.lower() == subreddit_name.lower():
hashed_text = manageText(
submission.title, file, flag_commentsPosts, submission.selftext)
print(f"POST TITLE: {submission.title}")
print(f"HASH: {hashed_text}")
if submission.selftext != "":
submission.edit(hashed_text)
time.sleep(2)
if flag_deleteAndEdit:
submission.delete()
time.sleep(2)
file.write("\n---\n")
file.close()
except RedditAPIException as e:
print(
f"Reddit API Exception raised while deleting comments: [{e.error_type}: {e.message}]")
else:
dt = ask_for_date()
if flag_commentsPosts == True:
if flag_beforeAfter == True:
# all COMMENTS BEFORE a DATE
print(f"Iterating over your Comments posted before {dt}")
try:
for comment in user.comments.new(limit=None):
if comment.created_utc <= dt.timestamp():
hashed_text = manageText(
comment.body, file, flag_commentsPosts)
print(f"COMMENT: {comment.body}")
print(f"HASH: {hashed_text}")
comment.edit(hashed_text)
time.sleep(2)
if flag_deleteAndEdit:
comment.delete()
time.sleep(2)
file.write("\n---\n")
file.close()
except RedditAPIException as e:
print(
f"Reddit API Exception raised while deleting comments: [{e.error_type}: {e.message}]")
else:
# all COMMENTS AFTER a DATE
print(f"Iterating over your Comments posted after {dt}")
try:
for comment in user.comments.new(limit=None):
if comment.created_utc >= dt.timestamp():
hashed_text = manageText(
comment.body, file, flag_commentsPosts)
print(f"COMMENT: {comment.body}")
print(f"HASH: {hashed_text}")
comment.edit(hashed_text)
time.sleep(2)
if flag_deleteAndEdit:
comment.delete()
time.sleep(2)
file.write("\n---\n")
file.close()
except RedditAPIException as e:
print(
f"Reddit API Exception raised while deleting comments: [{e.error_type}: {e.message}]")
else:
if flag_beforeAfter == True:
# all POSTS BEFORE a DATE
print(f"Iterating over your Posts before {dt}")
try:
for submission in user.submissions.new(limit=None):
if submission.created_utc <= dt.timestamp():
hashed_text = manageText(
submission.title, file, flag_commentsPosts, submission.selftext)
print(f"POST TITLE: {submission.title}")
print(f"HASH: {hashed_text}")
if submission.selftext != "":
submission.edit(hashed_text)
time.sleep(2)
if flag_deleteAndEdit:
submission.delete()
time.sleep(2)
file.write("\n---\n")
file.close()
except RedditAPIException as e:
print(
f"Reddit API Exception raised while deleting comments: [{e.error_type}: {e.message}]")
else:
# all POSTS AFTER a DATE
print(f"Iterating over your Posts after {dt}")
try:
for submission in user.submissions.new(limit=None):
if submission.created_utc >= dt.timestamp():
hashed_text = manageText(
submission.title, file, flag_commentsPosts, submission.selftext)
print(f"POST TITLE: {submission.title}")
print(f"HASH: {hashed_text}")
if submission.selftext != "":
submission.edit(hashed_text)
time.sleep(2)
if flag_deleteAndEdit:
submission.delete()
time.sleep(2)
file.write("\n---\n")
file.close()
except RedditAPIException as e:
print(
f"Reddit API Exception raised while deleting comments: [{e.error_type}: {e.message}]")