From a7113472a6ac240f991205c20e959cfe1055675b Mon Sep 17 00:00:00 2001 From: spla Date: Mon, 24 Jan 2022 14:40:21 +0100 Subject: [PATCH] Fill today's totals with zero if any attempt --- spamcheck.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/spamcheck.py b/spamcheck.py index 3f0ee84..d6ce90e 100644 --- a/spamcheck.py +++ b/spamcheck.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import date, datetime, timedelta from mastodon import Mastodon import time import os @@ -17,6 +17,8 @@ def write_totals(spamcheck_datetime_lst, spamcheck_registers_lst): first_date = spamcheck_datetime_lst[0] + last_date = spamcheck_datetime_lst[len(spamcheck_datetime_lst)-1] + i = 0 while i < len(spamcheck_datetime_lst): @@ -55,6 +57,34 @@ def write_totals(spamcheck_datetime_lst, spamcheck_registers_lst): first_date = first_date + timedelta(days=1) + if date.today() == last_date + timedelta(days=1): + + insert_sql = 'INSERT INTO totals(datetime, registers) VALUES(%s,%s) ON CONFLICT (datetime) DO UPDATE SET (datetime, registers) = (EXCLUDED.datetime, EXCLUDED.registers)' + + conn = None + + try: + + conn = psycopg2.connect(database = spamcheck_db, user = spamcheck_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute(insert_sql, (date.today(), '0')) + + conn.commit() + + cur.close() + + except (Exception, psycopg2.DatabaseError) as error: + + print(error) + + finally: + + if conn is not None: + + conn.close() + def get_totals(): spamcheck_datetime_lst = [] @@ -285,7 +315,7 @@ if __name__ == '__main__': while i < len(id_lst): - if detect(text_lst[i]) != 'ca': + if detect(text_lst[i]) != 'ca' or len(text_lst) == 1: is_tor_exit_node = check_ip(ip_lst[i])