11import logging
22import sys
3+ import typing
34from datetime import datetime
45
56from telebot import TeleBot
@@ -46,6 +47,23 @@ def format_expense_row(row):
4647 row = f"{ row } " .replace ("(" , "" ).replace (")" , "" ).replace ("'" , "" )
4748 return f"{ row } \n "
4849
50+ @staticmethod
51+ def extract_month_from_message (message_text : str ) -> str :
52+ """
53+ Extract the month from the message, if no month is found return current month
54+ :param message_text:
55+ :return:
56+ """
57+ current_month_notations = (datetime .now ().strftime ('%b' ), datetime .now ().strftime ('%B' ),
58+ datetime .now ().strftime ('%m' ), datetime .now ().strftime ('%-m' ))
59+ selected_month : typing .Union [str , int ] = message_text .split (' ' )[1 ]
60+ try :
61+ selected_month = int (selected_month )
62+ except ValueError :
63+ selected_month = selected_month .capitalize ()
64+
65+ return selected_month if selected_month in current_month_notations else datetime .now ().strftime ("%m" )
66+
4967 @_bot .message_handler (commands = ['start' , 'help' ])
5068 def welcome_message (self , message : Message ):
5169 """Handles 'start' and 'help' commands"""
@@ -68,15 +86,14 @@ def add_outcome(self, message: Message):
6886 @_bot .message_handler (commands = ['listincome' ])
6987 def list_income (self , message : Message ):
7088 """List all the income for the current month"""
71- # TODO: add a method to list the income for a specific month
72- current_month = datetime .now ().strftime ("%m" )
73- rows = self ._db .get_income (current_month )
89+ selected_month = self .extract_month_from_message (message .text )
90+ rows = self ._db .get_income (selected_month )
7491 LOGGER .debug (f'Fetched entries: { rows } ' )
7592 if rows :
7693 income_list : str = ""
7794 for row in rows :
7895 income_list += self .format_expense_row (row )
79- month_total_income = self ._db .get_total_income (current_month )
96+ month_total_income = self ._db .get_total_income (selected_month )
8097 LOGGER .debug (f'Month total: { month_total_income } , formatted income list: { income_list } ' )
8198 self ._bot .reply_to (message , f'{ self ._replies .list_income } \n \n { income_list } \n \n '
8299 f'Total income: { month_total_income } €' )
@@ -86,15 +103,14 @@ def list_income(self, message: Message):
86103 @_bot .message_handler (commands = ['listoutcome' ])
87104 def list_outcome (self , message : Message ):
88105 """List all the outcome for the current month"""
89- # TODO: add a method to list the outcome for a specific month
90- current_month = datetime .now ().strftime ("%m" )
91- rows = self ._db .get_outcome (current_month )
106+ selected_month = self .extract_month_from_message (message .text )
107+ rows = self ._db .get_outcome (selected_month )
92108 LOGGER .debug (f'Fetched entries: { rows } ' )
93109 if rows :
94110 outcome_list : str = ""
95111 for row in rows :
96112 outcome_list += self .format_expense_row (row )
97- month_total_outcome = self ._db .get_total_outcome (current_month )
113+ month_total_outcome = self ._db .get_total_outcome (selected_month )
98114 LOGGER .debug (f'Month total: { month_total_outcome } , formatted income list: { outcome_list } ' )
99115 self ._bot .reply_to (message , f'{ self ._replies .list_income } \n \n { outcome_list } \n \n '
100116 f'Total income: { month_total_outcome } €' )
@@ -103,9 +119,9 @@ def list_outcome(self, message: Message):
103119
104120 @_bot .message_handler (commands = ['balance' ])
105121 def balance (self , message : Message ):
106- current_month = datetime . now (). strftime ( "%m" )
107- month_total_outcome = self ._db .get_total_outcome (current_month )
108- month_total_income = self ._db .get_total_income (current_month )
122+ selected_month = self . extract_month_from_message ( message . text )
123+ month_total_outcome = self ._db .get_total_outcome (selected_month )
124+ month_total_income = self ._db .get_total_income (selected_month )
109125 if month_total_income is None :
110126 month_total_income = 0.0
111127 elif month_total_outcome is None :
0 commit comments