aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hommaexceli_py/parser.py45
-rw-r--r--hommaexceli_py/telegram_client.py8
2 files changed, 52 insertions, 1 deletions
diff --git a/hommaexceli_py/parser.py b/hommaexceli_py/parser.py
new file mode 100644
index 0000000..b4abb46
--- /dev/null
+++ b/hommaexceli_py/parser.py
@@ -0,0 +1,45 @@
+import datetime
+import logging
+import re
+from sheets_client import SheetsRow
+
+INTERVAL_ABBREVS = {
+ "pv": datetime.timedelta(days=1),
+ "vk": datetime.timedelta(days=7),
+ "kk": datetime.timedelta(days=30),
+ "v": datetime.timedelta(days=365),
+}
+
+
+def _calculate_interval(interval_str: str):
+ match = re.search(r"(\d+)(\w+)", interval_str)
+ number = int(match[1])
+ abbrev = match[2]
+
+ try:
+ interval = INTERVAL_ABBREVS[abbrev]
+ except KeyError:
+ raise Exception(f'"{interval_str}" is an invalid interval string!')
+
+ total_interval = datetime.timedelta(seconds=interval.total_seconds() * number)
+ return total_interval
+
+
+def _timestamp_to_date(timestamp: str):
+ return datetime.datetime.strptime(timestamp, "%Y-%m-%d")
+
+
+def _date_to_timestamp(date):
+ return datetime.datetime.strftime(date, "%Y-%m-%d")
+
+
+def _process_row(row: SheetsRow):
+ interval = _calculate_interval(row.interval)
+ last_done_date = _timestamp_to_date(row.last_done)
+ next_date = last_done_date + interval
+ next_date_str = _date_to_timestamp(next_date)
+ return (row.last_done, row.interval, next_date_str, row.name)
+
+
+def process_rows(rows: list[SheetsRow]):
+ return map(_process_row, rows)
diff --git a/hommaexceli_py/telegram_client.py b/hommaexceli_py/telegram_client.py
index 5e845a5..2d30b01 100644
--- a/hommaexceli_py/telegram_client.py
+++ b/hommaexceli_py/telegram_client.py
@@ -6,6 +6,8 @@ from os import getenv
from telegram.ext import Updater, CommandHandler, Job
from telegram import Update
from sheets_client import authenticate_and_fetch_sheets_data
+from parser import process_rows
+
chats_mem_cache = set()
@@ -25,8 +27,11 @@ def _callback_alarm(context):
logging.info(f"Authenticating and fetching data from Sheets...")
rows = authenticate_and_fetch_sheets_data()
+ logging.info(f"Processing data...")
+ processed_data = process_rows(rows)
+
logging.info(f"Sending sheet data to chat id {chat_id}...")
- message = "\n".join(map(str, rows))
+ message = "\n".join(map(str, processed_data))
context.bot.send_message(chat_id=chat_id, text=message)
@@ -40,6 +45,7 @@ def _callback_timer(update: Update, context):
context.job_queue.run_repeating(
_callback_alarm,
datetime.timedelta(weeks=1),
+ 1, # run immediately
context=chat_id,
)