spamcheck/spamcheck.py

135 lines
3.4 KiB
Python
Raw Normal View History

2021-07-21 14:46:27 +02:00
import datetime
from mastodon import Mastodon
import time
import os
import json
import sys
import os.path
import operator
import psycopg2
import pdb
def db_config():
# Load db configuration from config file
config_filepath = "config/db_config.txt"
mastodon_db = get_parameter("mastodon_db", config_filepath)
mastodon_db_user = get_parameter("mastodon_db_user", config_filepath)
spamcheck_db = get_parameter("spamcheck_db", config_filepath)
spamcheck_db_user = get_parameter("spamcheck_db_user", config_filepath)
return (mastodon_db, mastodon_db_user, spamcheck_db, spamcheck_db_user)
# 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):
print("File %s not found, exiting."%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)
sys.exit(0)
###############################################################################
# main
if __name__ == '__main__':
mastodon_db, mastodon_db_user, spamcheck_db, spamcheck_db_user = db_config()
###############################################################################
# check new registering
###############################################################################
created_at_lst = []
id_lst = []
email_lst = []
ip_lst = []
2021-07-26 15:43:02 +02:00
text_lst = []
2021-07-21 14:46:27 +02:00
try:
conn = None
conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
2021-07-26 15:43:02 +02:00
cur.execute("select users.created_at, users.id, users.email, users.sign_up_ip, user_invite_requests.text from users, user_invite_requests where not users.approved and users.id = user_invite_requests.user_id")
2021-07-21 14:46:27 +02:00
rows = cur.fetchall()
for row in rows:
if row != None:
created_at_lst.append(row[0])
id_lst.append(row[1])
email_lst.append(row[2])
ip_lst.append(row[3])
2021-07-26 15:43:02 +02:00
text_lst.append(row[4])
2021-07-21 14:46:27 +02:00
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print (error)
finally:
if conn is not None:
conn.close()
###############################################################################
2021-07-26 15:43:02 +02:00
insert_sql = 'INSERT INTO spamcheck(created_at, id, email, ip, text) VALUES(%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING'
2021-07-21 14:46:27 +02:00
i = 0
while i < len(id_lst):
conn = None
try:
conn = psycopg2.connect(database = spamcheck_db, user = spamcheck_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
2021-07-26 15:43:02 +02:00
cur.execute(insert_sql, (created_at_lst[i], id_lst[i], email_lst[i], ip_lst[i], text_lst[i]))
2021-07-21 14:46:27 +02:00
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
2021-07-26 15:43:02 +02:00
print(created_at_lst[i], id_lst[i], email_lst[i], ip_lst[i], text_lst[i])
2021-07-21 14:46:27 +02:00
i = i + 1