diff --git a/PostCleaner.py b/PostCleaner.py index aa645fd..4f0375e 100644 --- a/PostCleaner.py +++ b/PostCleaner.py @@ -1,3 +1,4 @@ +import json import praw import time from datetime import datetime @@ -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() diff --git a/commentCleaner.py b/commentCleaner.py index 825d4da..42dcee0 100644 --- a/commentCleaner.py +++ b/commentCleaner.py @@ -1,3 +1,4 @@ +import json import praw import time from datetime import datetime, timedelta @@ -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() @@ -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() @@ -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() diff --git a/tests/test_web_app.py b/tests/test_web_app.py index 1d1fe9f..753615d 100644 --- a/tests/test_web_app.py +++ b/tests/test_web_app.py @@ -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() diff --git a/web/app.py b/web/app.py index 7d9f414..7acf718 100644 --- a/web/app.py +++ b/web/app.py @@ -1,3 +1,4 @@ +import json import os import sys from datetime import datetime @@ -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 @@ -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 diff --git a/weekly_cleanup.py b/weekly_cleanup.py index 7fa1be7..8e64ca6 100644 --- a/weekly_cleanup.py +++ b/weekly_cleanup.py @@ -22,6 +22,7 @@ """ import argparse +import json import os from datetime import datetime, timezone @@ -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() @@ -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()