Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions PostCleaner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import praw
import time
from datetime import datetime
Expand Down Expand Up @@ -106,9 +107,17 @@ def delete_old_posts(reddit, username, days_old):
if submission.created_utc < (time.time() - (days_old * 86400)):
# save post title, date, karma, and subreddit deleted to a file with utf-8 encoding
with open("deleted_posts.txt", "a", encoding="utf-8") as f:
created_at = datetime.utcfromtimestamp(submission.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
f.write(f"{submission.title}, {created_at}, {deleted_at}, {submission.score}, {submission.subreddit.display_name}\n")
f.write(json.dumps({
"deleted_at": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.utcfromtimestamp(submission.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": submission.name,
"subreddit": submission.subreddit.display_name,
"score": submission.score,
"title": submission.title,
"permalink": f"https://reddit.com{submission.permalink}",
"num_comments": submission.num_comments,
"source": "cli",
}) + "\n")
try:
submission.edit(".")
submission.delete()
Expand Down
40 changes: 31 additions & 9 deletions commentCleaner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import praw
import time
from datetime import datetime, timedelta
Expand Down Expand Up @@ -102,10 +103,17 @@ def delete_old_comments(reddit, username, days_old, comments_deleted):
"""
for comment in reddit.redditor(username).comments.new(limit=None):
if time.time() - comment.created_utc > days_old * 24 * 60 * 60:
created_at = datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
with open('deleted_comments.txt', 'a', encoding='utf-8') as f:
f.write(f"{deleted_at} | {created_at} | {comment.score} | {comment.subreddit} | {comment.body}\n")
f.write(json.dumps({
"deleted_at": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": comment.name,
"subreddit": str(comment.subreddit),
"score": comment.score,
"permalink": f"https://reddit.com{comment.permalink}",
"body": comment.body,
"source": "cli-mode-1",
}) + "\n")
try:
comment.edit(".")
comment.delete()
Expand All @@ -128,10 +136,17 @@ def remove_comments_with_negative_karma(reddit, username, comments_deleted):
"""
for comment in reddit.redditor(username).comments.new(limit=None):
if comment.score <= 0:
created_at = datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
with open('deleted_comments.txt', 'a', encoding='utf-8') as f:
f.write(f"{deleted_at} | {created_at} | {comment.score} | {comment.subreddit} | {comment.body}\n")
f.write(json.dumps({
"deleted_at": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": comment.name,
"subreddit": str(comment.subreddit),
"score": comment.score,
"permalink": f"https://reddit.com{comment.permalink}",
"body": comment.body,
"source": "cli-mode-2",
}) + "\n")
try:
comment.edit(".")
comment.delete()
Expand Down Expand Up @@ -161,10 +176,17 @@ def remove_comments_with_one_karma_and_no_replies(reddit, username, comments_del
comment.refresh()
# Check if the comment meets the criteria
if comment.score <= 1 and len(comment.replies) == 0 and datetime.utcfromtimestamp(comment.created_utc) < one_week_ago:
created_at = datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
with open('deleted_comments.txt', 'a', encoding='utf-8') as f:
f.write(f"{deleted_at} | {created_at} | {comment.score} | {comment.subreddit} | {comment.body}\n")
f.write(json.dumps({
"deleted_at": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": comment.name,
"subreddit": str(comment.subreddit),
"score": comment.score,
"permalink": f"https://reddit.com{comment.permalink}",
"body": comment.body,
"source": "cli-mode-3",
}) + "\n")
try:
comment.edit(".")
comment.delete()
Expand Down
3 changes: 3 additions & 0 deletions tests/test_web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def test_deletes_comment(self, authed_client, tmp_path, monkeypatch):
mock_comment = MagicMock()
mock_comment.created_utc = 1700000000.0
mock_comment.score = -1
mock_comment.name = "t1_abc123"
mock_comment.subreddit = "testsubreddit"
mock_comment.permalink = "/r/testsubreddit/comments/abc/test/abc123/"
mock_comment.body = "bad comment"

mock_reddit = MagicMock()
Expand Down
34 changes: 22 additions & 12 deletions web/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import sys
from datetime import datetime
Expand Down Expand Up @@ -126,10 +127,17 @@ def api_delete():
for cid in comment_ids:
try:
comment = reddit.comment(cid)
created_at = datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
with open(DELETED_COMMENTS_FILE, "a", encoding="utf-8") as f:
f.write(f"{deleted_at} | {created_at} | {comment.score} | {comment.subreddit} | {comment.body}\n")
f.write(json.dumps({
"deleted_at": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.utcfromtimestamp(comment.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": comment.name,
"subreddit": str(comment.subreddit),
"score": comment.score,
"permalink": f"https://reddit.com{comment.permalink}",
"body": comment.body,
"source": "web",
}) + "\n")
comment.edit(".")
comment.delete()
deleted_comments += 1
Expand All @@ -139,16 +147,18 @@ def api_delete():
for pid in post_ids:
try:
submission = reddit.submission(pid)
created_at = datetime.utcfromtimestamp(submission.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
with open(DELETED_POSTS_FILE, "a", encoding="utf-8") as f:
f.write(
f"{submission.title}, "
f"{created_at}, "
f"{deleted_at}, "
f"{submission.score}, "
f"{submission.subreddit.display_name}\n"
)
f.write(json.dumps({
"deleted_at": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.utcfromtimestamp(submission.created_utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": submission.name,
"subreddit": submission.subreddit.display_name,
"score": submission.score,
"title": submission.title,
"permalink": f"https://reddit.com{submission.permalink}",
"num_comments": submission.num_comments,
"source": "web",
}) + "\n")
submission.edit(".")
submission.delete()
deleted_posts += 1
Expand Down
34 changes: 22 additions & 12 deletions weekly_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import argparse
import json
import os
from datetime import datetime, timezone

Expand Down Expand Up @@ -89,13 +90,20 @@ def main(dry_run: bool = False):
print("Scanning comments…")
for comment in reddit.redditor(username).comments.new(limit=None):
if _should_delete(comment):
created_at = datetime.fromtimestamp(comment.created_utc, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
if dry_run:
print(f" [DRY RUN] Would delete comment (score={comment.score}) in r/{comment.subreddit}: {comment.body[:80]!r}")
else:
with open("deleted_comments.txt", "a", encoding="utf-8") as f:
f.write(f"{deleted_at} | {created_at} | {comment.score} | {comment.subreddit} | {comment.body}\n")
f.write(json.dumps({
"deleted_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.fromtimestamp(comment.created_utc, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": comment.name,
"subreddit": str(comment.subreddit),
"score": comment.score,
"permalink": f"https://reddit.com{comment.permalink}",
"body": comment.body,
"source": "ci",
}) + "\n")
try:
comment.edit(".")
comment.delete()
Expand All @@ -112,16 +120,18 @@ def main(dry_run: bool = False):
if dry_run:
print(f" [DRY RUN] Would delete post '{submission.title}' (score={submission.score}) in r/{submission.subreddit}")
else:
created_at = datetime.fromtimestamp(submission.created_utc, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
deleted_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
with open("deleted_posts.txt", "a", encoding="utf-8") as f:
f.write(
f"{submission.title}, "
f"{created_at}, "
f"{deleted_at}, "
f"{submission.score}, "
f"{submission.subreddit.display_name}\n"
)
f.write(json.dumps({
"deleted_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"created_at": datetime.fromtimestamp(submission.created_utc, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"id": submission.name,
"subreddit": submission.subreddit.display_name,
"score": submission.score,
"title": submission.title,
"permalink": f"https://reddit.com{submission.permalink}",
"num_comments": submission.num_comments,
"source": "ci",
}) + "\n")
try:
submission.edit(".")
submission.delete()
Expand Down