mailing/delete_inactives.py

144 líneas
4.6 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime, timezone, timedelta
import time
import threading
import os
import sys
import os.path
import psycopg2
def delete_inactives(mailing_db, mailing_db_user, mailing_db_table, query, id_array, username_array):
conn = None
try:
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
# executem SELECT
cur.execute(query)
for row in cur:
id_array.append(row[0])
username_array.append(row[1])
i = 0
if len(id_array) == 0:
sys.exit("None inactive users found!")
while i < len(id_array):
print("\n")
print("Deleting user " + str(i) + " of " + str(len(id_array)) + " users")
print("Deleting user with id " + str(id_array[i]) + ", username: " + username_array[i])
os.system("cd " + mastodon_full_path + " && " + "RAILS_ENV=production " + rvm_ruby + " /var/www/vhosts/mastodont.cat/httpdocs/live/bin/tootctl accounts delete " + username_array[i])
delete_user(id_array[i], username_array[i])
i += 1
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
###################################################################################
# update user as deleted
###################################################################################
def delete_user(id, username):
conn = None
try:
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute("DELETE FROM " + mailing_db_table + " where account_id=(%s)", (str(id),))
print("Deleting user " + str(id) + ", username " + str(username))
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
###############################################################################
# INITIALISATION
###############################################################################
# Returns the parameter from the specified file
def get_parameter( parameter, file_path ):
# Check if secrets file exists
if not os.path.isfile(file_path):
if file_path == "secrets/secrets.txt":
print("File %s not found, exiting. Run setup.py."%file_path)
elif file_path == "config.txt":
print("File %s not found, exiting. Run db-setup.py."%file_path)
sys.exit(0)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
print("Run setup.py")
sys.exit(0)
# Load secrets from secrets file
secrets_filepath = "secrets/secrets.txt"
mastodon_full_path = get_parameter("mastodon_full_path", secrets_filepath)
# Load configuration from config file
config_filepath = "config.txt"
mailing_db = get_parameter("mailing_db", config_filepath)
mailing_db_user = get_parameter("mailing_db_user", config_filepath)
mailing_db_table = get_parameter("mailing_db_table", config_filepath)
###############################################################################
global rvm_ruby
rvm_ruby = os.environ['HOME'] + "/.rbenv/shims/ruby"
###############################################################################
# select users who replied the warning email saying yes to deletion
###############################################################################
query = "select account_id, username, email, to_be_deleted, feedback, recipient_error, elapsed_days from " + mailing_db_table + " where to_be_deleted = 't' and feedback = 't' and recipient_error = 'f' and emailed_at < now() - interval '31 days'"
id_array = []
username_array = []
delete_inactives(mailing_db, mailing_db_user, mailing_db_table, query, id_array, username_array)
###############################################################################
# select users who don't replied to email after 30 days
###############################################################################
query = "select account_id, username, email, to_be_deleted, feedback, recipient_error, elapsed_days from inactive_users where to_be_deleted = 'f' and feedback = 'f' and recipient_error = 'f' and emailed_at < now() - interval '31 days'"
id_array = []
username_array = []
delete_inactives(mailing_db, mailing_db_user, mailing_db_table, query, id_array, username_array)