Archived
1
0
Fork 0
This repository has been archived on 2024-06-28. You can view files and clone it, but cannot push or open issues or pull requests.
storm/main.py

42 lines
1.1 KiB
Python
Raw Permalink Normal View History

2019-10-02 17:52:19 +02:00
import requests
import telegram
2020-02-03 14:27:06 +01:00
import yaml
2019-10-02 17:52:19 +02:00
from bs4 import BeautifulSoup
2020-02-03 14:27:06 +01:00
from config import telegram_token
2019-10-02 17:52:19 +02:00
try:
2020-02-03 14:27:06 +01:00
with open("cache.yaml") as f:
cache = yaml.safe_load(f)
2019-10-02 17:52:19 +02:00
except FileNotFoundError:
cache = {}
2020-02-03 14:27:06 +01:00
with open("db.yaml") as f:
db = yaml.safe_load(f)
2019-10-02 17:52:19 +02:00
s = requests.Session()
2020-02-03 14:27:06 +01:00
pretty_daynames = {"heute": "Heute", "morgen": "Morgen", "uebermorgen": "Übermorgen"}
2019-10-02 17:52:19 +02:00
2020-02-03 14:27:06 +01:00
def notify(text, prettyday):
2019-10-02 17:52:19 +02:00
bot = telegram.Bot(token=telegram_token)
2020-02-03 14:27:06 +01:00
message = f"🌩️🌪️🌀 ({prettyday})\n" + text
for chat_id in db["subscribed"].keys():
bot.sendMessage(chat_id=chat_id, text=message)
2019-10-02 17:52:19 +02:00
2020-02-03 14:27:06 +01:00
for day in pretty_daynames.keys():
2019-10-02 17:52:19 +02:00
r = s.get(f'https://warnungen.zamg.at/html/de/{day}/wind/at/wien/wien_waehring/wien_waehring/')
soup = BeautifulSoup(r.text, 'html.parser')
warnings = [tag.get_text() for tag in soup.find_all("p", class_="warnung_text")]
text = "\n".join(warnings)
if day not in cache or text != cache[day]:
2019-10-02 17:55:31 +02:00
if text:
2020-02-03 14:27:06 +01:00
notify(text, pretty_daynames[day])
2019-10-02 17:52:19 +02:00
cache[day] = text
2020-02-03 14:27:06 +01:00
with open("cache.yaml", "w") as f:
yaml.safe_dump(cache, f)