import telegram_send from sqlalchemy import create_engine, text import pandas as pd import datetime as dt import matplotlib.pyplot as plt # Get the relevant data engine = create_engine('sqlite:///history/data.sqlite') query = 'SELECT * FROM messages' data = pd.read_sql_query(sql=text(query), con=engine.connect()) data = data[data['content']=='Coffee'] grand_total = len(data) data['datetime'] = pd.to_datetime(data['date']) # Calculate weekdays and hours data['weekday'] = data['datetime'].dt.dayofweek data['hour'] = data['datetime'].dt.hour data['day'] = data['datetime'].dt.day lastmonth = (dt.datetime.now().month - 1) % 12 monthly = data[data['datetime'].dt.month == lastmonth].copy() # Make plots monthly['weekday'].plot.hist(bins=7) plt.savefig('files/monthly_weekdays.png') plt.close() monthly['hour'].plot.hist(bins=24) plt.savefig('files/monthly_hour.png') plt.close() data['weekday'].plot.hist(bins=7) plt.savefig('files/all_weekdays.png') plt.close() data['hour'].plot.hist(bins=24) plt.savefig('files/all_hour.png') plt.close() # Calculate on which day the most coffee was made monthly['count'] = 1 aggregated = monthly.groupby(['day']).count()['count'] message1 = 'Last month, coffee was made {amount} times.'.format(amount=len(monthly)) + ' Most coffee was made on day {day} of the month, with a total amount of {amount} times.'.format(day=aggregated.idxmax(),amount=aggregated.max()) + ' From the start of this chat, a grand total amount of {amount} times coffee was made.'.format(amount=grand_total) telegram_send.send(messages=[message1])