Added delete_inactives.py to delete them

This commit is contained in:
salvadorpla 2020-01-13 13:47:23 +01:00
pare 10414f9dd4
commit 81bd44600b
S'han modificat 2 arxius amb 147 adicions i 1 eliminacions

144
delete_inactives.py Normal file
Veure arxiu

@ -0,0 +1,144 @@
#!/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
# i tancar la connexió amb la base de dades
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)

Veure arxiu

@ -22,11 +22,13 @@ def write_parameter( parameter, file_path ):
smtp_user_login = input("Enter SMTP user login, ex. user@" + smtp_host +"? ") smtp_user_login = input("Enter SMTP user login, ex. user@" + smtp_host +"? ")
smtp_user_password = getpass.getpass("SMTP user password? ") smtp_user_password = getpass.getpass("SMTP user password? ")
email_subject = input("Enter the subject of the email: ") email_subject = input("Enter the subject of the email: ")
mastodon_full_path = input("Enter Mastodon's live dir full path, ex. /home/mastodon/live : ")
with open(file_path, "w") as text_file: with open(file_path, "w") as text_file:
print("smtp_host: {}".format(smtp_host), file=text_file) print("smtp_host: {}".format(smtp_host), file=text_file)
print("smtp_user_login: {}".format(smtp_user_login), file=text_file) print("smtp_user_login: {}".format(smtp_user_login), file=text_file)
print("smtp_user_password: {}".format(smtp_user_password), file=text_file) print("smtp_user_password: {}".format(smtp_user_password), file=text_file)
print("email_subject: {}".format(email_subject), file=text_file) print("email_subject: {}".format(email_subject), file=text_file)
print("mastodon_full_path: {}".format(mastodon_full_path), file=text_file)
# Returns the parameter from the specified file # Returns the parameter from the specified file
def get_parameter( parameter, file_path ): def get_parameter( parameter, file_path ):
@ -56,5 +58,5 @@ smtp_host = get_parameter("smtp_host", secrets_filepath)
smtp_user_login = get_parameter("smtp_user_login", secrets_filepath) smtp_user_login = get_parameter("smtp_user_login", secrets_filepath)
smtp_user_pass = get_parameter("smtp_user_pass", secrets_filepath) smtp_user_pass = get_parameter("smtp_user_pass", secrets_filepath)
email_subject = get_parameter("email_subject", secrets_filepath) email_subject = get_parameter("email_subject", secrets_filepath)
mastodon_full_path = get_parameter("mastodon_full_path", secrets_filepath)