-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweightbot.py
executable file
·214 lines (173 loc) · 6.25 KB
/
weightbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
"""Telegram Bot to collect weight measurements.
Stores the measurements and their timestamps into a CSV file for some very
simple statistical analysis and goal follow-up.
"""
# pylint: disable=wrong-import-position, ungrouped-imports
# matplotlib quirk
import configparser
import csv
import logging
import tempfile
from pathlib import Path
import matplotlib
import numpy as np
import pandas as pd
import pendulum
import telegram
from pandas.plotting import register_matplotlib_converters
from telegram.ext import (
Application,
CallbackContext,
CommandHandler,
MessageHandler,
Updater,
)
from telegram.ext.filters import MessageFilter
matplotlib.use("agg")
import matplotlib.pyplot as plt
register_matplotlib_converters()
BASEDIR = Path(__file__).parent
CONFIG = configparser.ConfigParser(inline_comment_prefixes="#")
CONFIG.read((BASEDIR / "config", BASEDIR / "config.local"))
CONFIG = CONFIG["weightbot"]
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
LOGGER = logging.getLogger(__name__)
class WeightFilter(MessageFilter):
"""Check if the given weight is acceptable or not."""
@staticmethod
def filter(message):
"""Check if the given weight is acceptable or not."""
try:
weight = float(message.text)
return 50 < weight < 150
except ValueError:
return False
# pylint: disable=unused-argument
# We just don't need it.
async def bot_start(update: telegram.Update, context: CallbackContext):
"""Send a welcome message."""
update.message.reply_text(
"Hi! Just type in your current weight and I'll store it for you!"
)
async def bot_error(update: telegram.Update, context: CallbackContext):
"""Log errors caused by updates."""
LOGGER.warning("Update %s caused error %s", update, context.error)
if update:
update.message.reply_text("[some error occurred; check the log]")
async def bot_weight(update: telegram.Update, context: CallbackContext):
"""Store the given weight (if found acceptable)."""
await context.bot.send_chat_action(
chat_id=update.message.chat_id,
action=telegram.constants.ChatAction.TYPING,
)
weight = update.message.text
store_weight(weight)
await update.message.reply_text(f"{weight}kg successfully stored!")
await bot_stats(update, context, last="100d", resample="10d", goal=False)
# pylint: disable=too-many-locals
# Sorry...
async def bot_stats(
update: telegram.Update,
context: CallbackContext,
last=None,
resample="W",
goal=True,
):
"""Generate more elaborate progress statistics."""
await context.bot.send_chat_action(
chat_id=update.message.chat_id,
action=telegram.constants.ChatAction.TYPING,
)
data = pd.read_csv(
CONFIG["csvfile"], parse_dates=["timestamp"], index_col="timestamp"
)
data.index = pd.to_datetime(data.index, utc=True).tz_convert(
"Europe/Brussels"
)
weekweight_mean_weight = data.last("7d").weight.mean()
if last:
data = data.last(last)
weight_min_weight = data.loc[data.weight.idxmin()].weight
weight_min_timestamp = pendulum.instance(
data.weight.idxmin()
).diff_for_humans()
weight_max_weight = data.loc[data.weight.idxmax()].weight
weight_max_timestamp = pendulum.instance(
data.weight.idxmax()
).diff_for_humans()
means = data.resample(resample, kind="period").mean()
weight_orig = means.weight[0]
weight_now = means.weight[-1]
weight_loss = weight_orig - weight_now
weight_loss_period = (
data.index.max() - data.index.min()
) / np.timedelta64(1, "D")
weight_goal = weight_orig + (
12 * float(CONFIG["goal"]) / 365 * weight_loss_period
)
fig, axes = plt.subplots()
axes.plot(data, "k.")
means.plot.line(ax=axes, style="g" if weight_now <= weight_goal else "r")
if goal:
axes.plot(
[means.index[0].start_time, means.index[-1].start_time],
[weight_orig, weight_goal],
"--",
color="orange",
)
axes.set_ylim(
[min(weight_goal, weight_min_weight) - 1, weight_max_weight + 1]
)
axes.yaxis.set_ticks_position("both")
axes.get_legend().remove()
axes.tick_params(labeltop=False, labelright=True)
plt.xlabel("")
plt.ylabel("kg")
fig.autofmt_xdate()
await update.message.reply_text(
f"Your weight mean the past week is {weekweight_mean_weight:.1f}kg. "
f"The minimum over the shown period was {weight_min_weight:.1f}kg "
f"({weight_min_timestamp}) and maximum was {weight_max_weight:.1f}kg "
f"({weight_max_timestamp})."
)
await context.bot.send_chat_action(
chat_id=update.message.chat_id,
action=telegram.constants.ChatAction.TYPING,
)
with tempfile.NamedTemporaryFile(suffix=".png") as figfile:
fig.savefig(figfile.name, bbox_inches="tight")
await update.message.reply_photo(figfile)
gainedlost = "lost" if weight_loss >= 0 else "gained"
await update.message.reply_text(
f"You have {gainedlost} {abs(weight_loss):.1f}kg "
f"in {weight_loss_period:.0f} days"
)
plt.close(fig)
def store_weight(weight):
"""Write the given weight to the CSV file with the current timestamp."""
with open(
CONFIG["csvfile"], mode="a", newline="", encoding="utf-8"
) as csvfile:
weightwriter = csv.writer(csvfile)
weightwriter.writerow([pendulum.now(), weight])
def main():
"""Run bot."""
csvfile_path = Path(CONFIG["csvfile"])
if not csvfile_path.is_file() or csvfile_path.stat().st_size == 0:
with csvfile_path.open(
mode="w", newline="", encoding="utf-8"
) as csvfile:
weightwriter = csv.writer(csvfile)
weightwriter.writerow(["timestamp", "weight"])
application = Application.builder().token(CONFIG["token"]).build()
application.add_handler(CommandHandler("start", bot_start))
application.add_handler(CommandHandler("stats", bot_stats))
application.add_handler(MessageHandler(WeightFilter(), bot_weight))
application.add_error_handler(bot_error)
application.run_polling()
if __name__ == "__main__":
main()