From b9819edd5e0e5e68c33a2a7a6283cb1e2acc14c7 Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 15 Nov 2022 15:48:20 +0100 Subject: [PATCH] Added current month donations --- publisher.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/publisher.py b/publisher.py index 02b3d04..1098678 100644 --- a/publisher.py +++ b/publisher.py @@ -5,6 +5,7 @@ import re from datetime import datetime, timedelta from mastodon import Mastodon import psycopg2 +import pdb def get_bills(): @@ -42,7 +43,7 @@ def get_bills(): conn.close() -def get_donations(): +def year_donations(): current_year = now.year @@ -82,6 +83,58 @@ def get_donations(): conn.close() +def month_donations(): + + current_month = now.month + + donations = 0 + + try: + + conn = None + + conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute("select count(donations) from incomes where date_part('month', datetime) = (%s)", (current_month,)) + + row = cur.fetchone() + + if row[0] != None: + + n_donations = row[0] + + else: + + n_donations = 0 + + cur.execute("select sum (donations) + sum(owner) from incomes where date_part('month', datetime) = (%s)", (current_month,)) + + row = cur.fetchone() + + if row[0] != None: + + donations = row[0] + + else: + + donations = 0 + + cur.close() + + return (n_donations, donations) + + except (Exception, psycopg2.DatabaseError) as error: + + sys.exit(error) + + finally: + + if conn is not None: + + conn.close() + def get_fixed_fees(): fees_desc_lst = [] @@ -186,31 +239,37 @@ if __name__ == '__main__': now = datetime.now() - donations = get_donations() + donations, sum_donations = month_donations() + + year_donations = year_donations() bills = get_bills() fees_desc_lst, fees_types_lst, fees_lst = get_fixed_fees() - toot_text = 'Finances of ' + mastodon_hostname + '\n\n' + toot_text = 'Finances de ' + mastodon_hostname + '\n\n' - toot_text += 'Year: ' + str(now.year) + '\n' + toot_text += 'Any: ' + str(now.year) + '\n' toot_text += '\n' - toot_text += 'Server bills: ' + str(bills) + '€' + '\n' + toot_text += 'Pagaments: ' + str(bills) + '€' + '\n' - toot_text += 'Donations: ' + str(donations) + '€' + '\n\n' + toot_text += 'Donacions: ' + str(year_donations) + '€' + '\n' - if donations != 0 and bills != 0: + toot_text += f'Balanç: {str(year_donations - bills)}€\n' - toot_text += '%: ' + str(round((donations * 100)/bills,2)) + '\n\n' + if year_donations != 0 and bills != 0: + + toot_text += 'Objectiu assolit: ' + str(round((year_donations * 100)/bills,2)) + '%\n\n' i = 0 + toot_text += f'Donacions rebudes (mes actual): {str(donations)} / {sum_donations}€\n\n' + while i < len(fees_lst): - toot_text += '\n' + str(fees_desc_lst[i]) + ': ' + str(fees_lst[i]) + ' (' + str(fees_types_lst[i]) + ')' + toot_text += 'Cost del ' + str(fees_desc_lst[i]) + ': ' + str(fees_lst[i]) + '€ (' + str(fees_types_lst[i]) + ')\n' i += 1