diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-11-06 10:16:03 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-11-06 10:34:26 +0100 |
| commit | 8983a77010ee97f3f402098175afafdad5fee5e5 (patch) | |
| tree | ee87d09fba4900de48a386e42a15d4f49cf0ddd0 | |
| parent | 08f50826ec76442d2a010bb23fda0ce66efad99d (diff) | |
| download | geldschieberbot-8983a77010ee97f3f402098175afafdad5fee5e5.tar.gz geldschieberbot-8983a77010ee97f3f402098175afafdad5fee5e5.zip | |
export the plot command
| -rw-r--r-- | geldschieberbot.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py index 51571c0..e6b0d75 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -19,11 +19,14 @@ from datetime import date, datetime, timedelta import json import os +import subprocess import typing as T +from tempfile import NamedTemporaryFile from models import MessageContext, Modification, Change, GeldschieberbotJSONEncoder, Reply from utils import to_euro, to_cent from minimize import minimize +from plot import plot class Geldschieberbot: @@ -284,6 +287,7 @@ class Geldschieberbot: list-changes [n] [first] - list the last n changes starting at first export-state - send the state file + plot - generate a plotting of the balance graph weekly name cmd - repeat cmd each week monthly name cmd - repeat cmd each month @@ -895,6 +899,24 @@ class Geldschieberbot: out = f'State from {datetime.now().date().isoformat()}' return {'msg': out, 'attachment': self.state_path} + def plot(self, msg: MessageContext) -> dict[str, str]: + """Send the balance graph plot as attachment""" + if not msg.sender: + return {'err': 'you must register first'} + + dot = plot(self.balance) + # Create a temporary image file and keep it + with NamedTemporaryFile(delete=False) as image: + try: + subprocess.run(['dot', '-Tpng'], + input=dot.encode('utf-8'), + stdout=image, + check=True) + except subprocess.CalledProcessError as e: + return {'err': f'{e}'} + + return {'msg': '', 'attachment': image.name} + def schedule(self, msg: MessageContext) -> dict[str, str]: """Schedule a command for periodic execution""" if not msg.sender: @@ -1064,6 +1086,7 @@ class Geldschieberbot: 'undo': self.fuck, 'list-changes': self.list_changes, 'export-state': self.export_state, + 'plot': self.plot, 'weekly': self.schedule, 'monthly': self.schedule, 'yearly': self.schedule, |
