mailing/delete_inactives.py

176 líneas
4.5 KiB
Python
Original Vista normal Històric

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime, timezone, timedelta
import time
import os
import sys
import os.path
import psycopg2
2022-08-01 11:52:30 +02:00
def delete_inactives(deletion_accepted, query):
2022-08-01 11:52:30 +02:00
id_array = []
2022-08-01 11:52:30 +02:00
username_array = []
2022-08-01 11:52:30 +02:00
conn = None
2022-08-01 11:52:30 +02:00
try:
2022-08-01 11:52:30 +02:00
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2022-08-01 11:52:30 +02:00
cur = conn.cursor()
2022-08-01 11:52:30 +02:00
cur.execute(query)
2022-08-01 11:52:30 +02:00
for row in cur:
2020-03-05 12:04:08 +01:00
2022-08-01 11:52:30 +02:00
id_array.append(row[0])
2022-08-01 11:52:30 +02:00
username_array.append(row[1])
2022-08-01 11:52:30 +02:00
i = 0
2022-08-01 11:52:30 +02:00
if len(id_array) == 0:
2022-08-01 11:52:30 +02:00
if deletion_accepted == True:
2022-08-01 11:52:30 +02:00
print("None inactive users who accepted to be deleted found!")
2022-08-01 11:52:30 +02:00
elif deletion_accepted == False:
2022-08-01 11:52:30 +02:00
print("None inactive users to be deleted!")
2022-08-01 11:52:30 +02:00
return
2022-08-01 11:52:30 +02:00
while i < len(id_array):
if deletion_accepted == True:
print("Deleting inactive users who accepted to be deleted...")
elif deletion_accepted == False:
print("Deleting inactive users who do not reply our email...")
print(f"\nDeleting user {str(i)} of {str(len(id_array))} users")
print(f"Deleting user with id {str(id_array[i])}, username: {username_array[i]}")
os.system(f"RAILS_ENV=production {rvm_ruby} {mastodon_full_path}/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()
def delete_user(id, username):
2022-08-01 11:52:30 +02:00
conn = None
2022-08-01 11:52:30 +02:00
try:
2022-08-01 11:52:30 +02:00
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
2022-08-01 11:52:30 +02:00
cur = conn.cursor()
2022-08-01 11:52:30 +02:00
cur.execute("DELETE FROM " + mailing_db_table + " where account_id=(%s)", (str(id),))
2022-08-01 11:52:30 +02:00
print(f"Deleting user {str(id)}, username {str(username)}")
2022-08-01 11:52:30 +02:00
conn.commit()
2022-08-01 11:52:30 +02:00
cur.close()
2022-08-01 11:52:30 +02:00
except (Exception, psycopg2.DatabaseError) as error:
2022-08-01 11:52:30 +02:00
print(error)
2022-08-01 11:52:30 +02:00
finally:
2022-08-01 11:52:30 +02:00
if conn is not None:
2022-08-01 11:52:30 +02:00
conn.close()
def get_parameter( parameter, file_path ):
2022-08-01 11:52:30 +02:00
# Check if secrets file exists
if not os.path.isfile(file_path):
2022-08-01 11:52:30 +02:00
if file_path == "secrets/secrets.txt":
2022-08-01 11:52:30 +02:00
print("File %s not found, exiting. Run setup.py."%file_path)
elif file_path == "config.txt":
2022-08-01 11:52:30 +02:00
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:
2022-08-01 11:52:30 +02:00
for line in f:
2022-08-01 11:52:30 +02:00
if line.startswith( parameter ):
2022-08-01 11:52:30 +02:00
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
2022-08-01 11:52:30 +02:00
print(f"{file_path} Missing parameter {parameter}")
print("Run setup.py")
sys.exit(0)
2022-08-01 11:52:30 +02:00
def config():
2022-08-01 11:52:30 +02:00
secrets_filepath = "secrets/secrets.txt"
mastodon_full_path = get_parameter("mastodon_full_path", secrets_filepath)
2022-08-01 11:52:30 +02:00
# 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)
2022-08-01 11:52:30 +02:00
return (mastodon_full_path, mailing_db, mailing_db_user, mailing_db_table)
###############################################################################
2022-08-01 11:52:30 +02:00
# main
2022-08-01 11:52:30 +02:00
if __name__ == '__main__':
2022-08-01 11:52:30 +02:00
mastodon_full_path, mailing_db, mailing_db_user, mailing_db_table = config()
global rvm_ruby
rvm_ruby = os.environ['HOME'] + "/.rbenv/shims/ruby"
###############################################################################
# select and delete users who replied the warning email saying yes to deletion
2022-08-01 11:52:30 +02:00
deletion_accepted = True
query = "select account_id, username, email, to_be_deleted, feedback, recipient_error, elapsed_days from " + mailing_db_table + " where to_be_deleted and feedback"
2022-08-01 11:52:30 +02:00
delete_inactives(deletion_accepted, query)
###############################################################################
# select and delete users who don't replied the email after 31 days
2022-08-01 11:52:30 +02:00
deletion_accepted = False
query = "select account_id, username, email, to_be_deleted, feedback, recipient_error, elapsed_days from " + mailing_db_table + " where not feedback and elapsed_days = '31'"
2022-08-01 11:52:30 +02:00
delete_inactives(deletion_accepted, query)