From e6b25bf07b2cd3a69dfe81dfd9def18e5384e590 Mon Sep 17 00:00:00 2001 From: Yuriy Novostavskiy Date: Tue, 26 Mar 2024 01:28:36 +0200 Subject: [PATCH] use Europe/Kiev timezone instead of UTC --- .env.exmple | 2 ++ Dockerfile | 2 +- bot/main.py | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.env.exmple b/.env.exmple index b2b1820..63a5028 100644 --- a/.env.exmple +++ b/.env.exmple @@ -2,6 +2,8 @@ TOKEN= CHAT_ID= # URL of API with compatiable format with http://alerts.net.ua/explosives_statuses_v2.json, optional # URL=http://alerts.net.ua/explosives_statuses_v2.json +# Timezone, optional, default Europe/Kiev +# TIMEZONE=Europe/Kiev # List of regions to filter, don't use spaces, single line, optional # REGION_LIST="Одеська область","Київська область","Житомирська область","м. Київ","Львівська область" # Full list of regions: diff --git a/Dockerfile b/Dockerfile index 749a1ca..2247377 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ LABEL org.opencontainers.image.authors="Yuriy Novostavskiy" \ org.opencontainers.image.license="MIT" \ org.opencontainers.image.description="A simple bot that sends the update of alarms and exlosions" -RUN python -m pip install requests~=2.31.0 python-dotenv~=1.0.1 +RUN python -m pip install requests~=2.31.0 python-dotenv~=1.0.1 pytz==2024.1 WORKDIR /bot diff --git a/bot/main.py b/bot/main.py index 8d35dd8..430719a 100644 --- a/bot/main.py +++ b/bot/main.py @@ -6,7 +6,8 @@ import os from dotenv import load_dotenv import time -from datetime import datetime +from datetime import datetime, timedelta +from pytz import timezone """ This is a simple telegram bot that every 30 secound get API http://alerts.net.ua/explosives_statuses_v2.json and send @@ -45,6 +46,7 @@ CHAT_ID = os.getenv("CHAT_ID") URL = os.getenv("URL") REGION_LIST = os.getenv("REGION_LIST").split(",") if os.getenv("REGION_LIST") else None +TIMEZONE = os.getenv("TIMEZONE") """ Full list of regions: @@ -112,9 +114,16 @@ else: REGION_LIST = [region.strip('"') for region in REGION_LIST] +if not TIMEZONE: + logger.warning( + "TIMEZONE is not defined in .env file, using a default timezone Europe/Kiev" + ) + TIMEZONE = "Europe/Kiev" + logger.info(f"Bot started with CHAT_ID: {CHAT_ID}") logger.info(f"Following regions will be monitored: {REGION_LIST}") + def get_data(): try: response = requests.get(URL) @@ -162,7 +171,10 @@ def main(): region ): logger.info(f"Explosion in region: {region}, date: {date}") - message += f"{regions_gram_case.get(region, region)} о {datetime.strptime(date,'%Y-%m-%dT%H:%M:%S+00:00').strftime('%H:%M')}\n" + + message += f"{regions_gram_case.get(region, region)} о {datetime\ + .strptime(date,'%Y-%m-%dT%H:%M:%S+00:00')\ + .replace(tzinfo=timezone.utc).astimezone(TIMEZONE).strftime('%H:%M')}\n" if message != MESSAGE: message += "\nЙобанарусня!"