reddit/app/delete_posts.py

67 lines
2.6 KiB
Python

import config
import os
import time
import sqlite3
import subprocess
def run():
connection = sqlite3.connect(config.db_file)
cursor = connection.cursor()
now = int(time.time())
max_created_utc = now - config.max_age_seconds
print("Deleting old posts")
delete = "DELETE FROM post WHERE created_utc < ? AND saved = ?"
binds = [max_created_utc, False]
cursor.execute(delete, binds)
print("Deleting posts from blocked subreddits")
select = "SELECT name FROM block WHERE name like '/r/%'"
binds = [row[0][3:] for row in cursor.execute(select).fetchall()]
bind_array = ",".join(["?"]*len(binds))
binds.append(False)
delete = f"DELETE FROM post WHERE subreddit IN ({bind_array}) AND saved = ?"
cursor.execute(delete, binds)
print("Deleting posts from blocked users")
select = "SELECT name FROM block WHERE name like '/u/%'"
binds = [row[0][3:] for row in cursor.execute(select).fetchall()]
bind_array = ",".join(["?"]*len(binds))
binds.append(False)
delete = f"DELETE FROM post WHERE author IN ({bind_array}) AND saved = ?"
cursor.execute(delete, binds)
print("Deleting posts with blocked words")
select = "SELECT name FROM block where name NOT LIKE '/u/%' AND name NOT LIKE '/r/%'"
binds = ["%"+row[0]+"%" for row in cursor.execute(select).fetchall()]
where_array = " OR ".join(["post LIKE ?"]*len(binds))
delete = f"DELETE FROM post WHERE {where_array}"
cursor.execute(delete, binds)
print("Deleting old media db rows")
delete = "DELETE FROM media WHERE permalink NOT IN (SELECT permalink FROM post)"
cursor.execute(delete)
print("Deleting media db for read posts")
delete = "DELETE FROM media WHERE permalink IN (SELECT permalink FROM post WHERE hidden = ?)"
binds = [True]
cursor.execute(delete, binds)
print("Updating media_fetched for read posts")
update = "UPDATE post SET media_fetched = ? WHERE hidden = ?"
binds = [False, True]
cursor.execute(update, binds)
all_files_local = subprocess.run(["find", "/reddit/media", "-type", "f"], capture_output=True, text=True)
all_files_local = set(all_files_local.stdout.splitlines())
select = "SELECT local from media"
results = cursor.execute(select).fetchall()
connection.commit()
connection.close()
all_files_db = set([row[0] for row in results])
extra_files = all_files_local - all_files_db
print("Deleting old files")
for file in extra_files:
print(f"Removing {file}")
os.remove(file)
empty_dirs = subprocess.run(["find", "/reddit/media", "-type", "d", "-empty"], capture_output=True, text=True)
empty_dirs = set(empty_dirs.stdout.splitlines())
print("Deleting empty directories")
for dir in empty_dirs:
print(f"Removind dir {dir}")
os.rmdir(dir)
print("Done")
run()