diff --git a/.gitignore b/.gitignore index da320eb..8eefd86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ config.py .idea cache.json +*.yaml __pycache__/ diff --git a/main.py b/main.py index ecfba74..8821643 100644 --- a/main.py +++ b/main.py @@ -1,27 +1,32 @@ -import json - import requests import telegram +import yaml from bs4 import BeautifulSoup -from config import telegram_token, telegram_chat_id +from config import telegram_token try: - with open("cache.json") as f: - cache = json.load(f) + with open("cache.yaml") as f: + cache = yaml.safe_load(f) except FileNotFoundError: cache = {} +with open("db.yaml") as f: + db = yaml.safe_load(f) + s = requests.Session() +pretty_daynames = {"heute": "Heute", "morgen": "Morgen", "uebermorgen": "Übermorgen"} -def notify(text): + +def notify(text, prettyday): bot = telegram.Bot(token=telegram_token) - message = "🌩️🌪️🌀\n" + text - bot.sendMessage(chat_id=telegram_chat_id, text=message) + message = f"🌩️🌪️🌀 ({prettyday})\n" + text + for chat_id in db["subscribed"].keys(): + bot.sendMessage(chat_id=chat_id, text=message) -for day in ["heute", "morgen", "uebermorgen"]: +for day in pretty_daynames.keys(): r = s.get(f'https://warnungen.zamg.at/html/de/{day}/wind/at/wien/wien_waehring/wien_waehring/') soup = BeautifulSoup(r.text, 'html.parser') @@ -29,8 +34,8 @@ for day in ["heute", "morgen", "uebermorgen"]: text = "\n".join(warnings) if day not in cache or text != cache[day]: if text: - notify(text) + notify(text, pretty_daynames[day]) cache[day] = text -with open("cache.json", "w") as f: - json.dump(cache, f) +with open("cache.yaml", "w") as f: + yaml.safe_dump(cache, f) diff --git a/poetry.lock b/poetry.lock index ea60ef2..f445a99 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,3 +1,18 @@ +[[package]] +category = "main" +description = "Screen-scraping library" +name = "beautifulsoup4" +optional = false +python-versions = "*" +version = "4.8.2" + +[package.dependencies] +soupsieve = ">=1.2" + +[package.extras] +html5lib = ["html5lib"] +lxml = ["lxml"] + [[package]] category = "main" description = "Python package for providing Mozilla's CA Bundle." @@ -74,7 +89,7 @@ description = "We have made you a wrapper you can't refuse" name = "python-telegram-bot" optional = false python-versions = "*" -version = "12.2.0" +version = "12.3.0" [package.dependencies] certifi = "*" @@ -86,6 +101,14 @@ tornado = ">=5.1" json = ["ujson"] socks = ["pysocks"] +[[package]] +category = "main" +description = "YAML parser and emitter for Python" +name = "pyyaml" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +version = "5.3" + [[package]] category = "main" description = "Python HTTP for Humans." @@ -109,8 +132,16 @@ category = "main" description = "Python 2 and 3 compatibility utilities" name = "six" optional = false -python-versions = ">=2.6, !=3.0.*, !=3.1.*" -version = "1.13.0" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" +version = "1.14.0" + +[[package]] +category = "main" +description = "A modern CSS selector implementation for Beautiful Soup." +name = "soupsieve" +optional = false +python-versions = "*" +version = "1.9.5" [[package]] category = "main" @@ -125,8 +156,8 @@ category = "main" description = "HTTP library with thread-safe connection pooling, file post, and more." name = "urllib3" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4" -version = "1.25.7" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" +version = "1.25.8" [package.extras] brotli = ["brotlipy (>=0.6.0)"] @@ -134,10 +165,15 @@ secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "cer socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7,<2.0)"] [metadata] -content-hash = "4c06f0e772ccc488e10354b62055ec9b898ce415272fcc601dda9ead2f91e37b" +content-hash = "4d93f8e8258611d71d491fc21569681adb9b7f39989a7eadd3a8f396bde1e1fc" python-versions = "^3.8" [metadata.files] +beautifulsoup4 = [ + {file = "beautifulsoup4-4.8.2-py2-none-any.whl", hash = "sha256:e1505eeed31b0f4ce2dbb3bc8eb256c04cc2b3b72af7d551a4ab6efd5cbe5dae"}, + {file = "beautifulsoup4-4.8.2-py3-none-any.whl", hash = "sha256:9fbb4d6e48ecd30bcacc5b63b94088192dcda178513b2ae3c394229f8911b887"}, + {file = "beautifulsoup4-4.8.2.tar.gz", hash = "sha256:05fd825eb01c290877657a56df4c6e4c311b3965bda790c613a3d6fb01a5462a"}, +] certifi = [ {file = "certifi-2019.11.28-py2.py3-none-any.whl", hash = "sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3"}, {file = "certifi-2019.11.28.tar.gz", hash = "sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f"}, @@ -215,16 +251,33 @@ pycparser = [ {file = "pycparser-2.19.tar.gz", hash = "sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3"}, ] python-telegram-bot = [ - {file = "python-telegram-bot-12.2.0.tar.gz", hash = "sha256:346d42771c2b23384c59f5f41e05bd7e801a0ce118d8dcb95209bb73d5f694c5"}, - {file = "python_telegram_bot-12.2.0-py2.py3-none-any.whl", hash = "sha256:3beee89cba3bc3217566c96199f04776dd25f541ac8992da27fd247b2d208a14"}, + {file = "python-telegram-bot-12.3.0.tar.gz", hash = "sha256:41608512a3025a04ff7472efcaae344ad7533a77ae207685bb10fc2fc8282f7b"}, + {file = "python_telegram_bot-12.3.0-py2.py3-none-any.whl", hash = "sha256:5e2156f829402e41bb5ea7196e450bf7f121c5689c5100ae180507d72f3777f5"}, +] +pyyaml = [ + {file = "PyYAML-5.3-cp27-cp27m-win32.whl", hash = "sha256:940532b111b1952befd7db542c370887a8611660d2b9becff75d39355303d82d"}, + {file = "PyYAML-5.3-cp27-cp27m-win_amd64.whl", hash = "sha256:059b2ee3194d718896c0ad077dd8c043e5e909d9180f387ce42012662a4946d6"}, + {file = "PyYAML-5.3-cp35-cp35m-win32.whl", hash = "sha256:4fee71aa5bc6ed9d5f116327c04273e25ae31a3020386916905767ec4fc5317e"}, + {file = "PyYAML-5.3-cp35-cp35m-win_amd64.whl", hash = "sha256:dbbb2379c19ed6042e8f11f2a2c66d39cceb8aeace421bfc29d085d93eda3689"}, + {file = "PyYAML-5.3-cp36-cp36m-win32.whl", hash = "sha256:e3a057b7a64f1222b56e47bcff5e4b94c4f61faac04c7c4ecb1985e18caa3994"}, + {file = "PyYAML-5.3-cp36-cp36m-win_amd64.whl", hash = "sha256:74782fbd4d4f87ff04159e986886931456a1894c61229be9eaf4de6f6e44b99e"}, + {file = "PyYAML-5.3-cp37-cp37m-win32.whl", hash = "sha256:24521fa2890642614558b492b473bee0ac1f8057a7263156b02e8b14c88ce6f5"}, + {file = "PyYAML-5.3-cp37-cp37m-win_amd64.whl", hash = "sha256:1cf708e2ac57f3aabc87405f04b86354f66799c8e62c28c5fc5f88b5521b2dbf"}, + {file = "PyYAML-5.3-cp38-cp38-win32.whl", hash = "sha256:70024e02197337533eef7b85b068212420f950319cc8c580261963aefc75f811"}, + {file = "PyYAML-5.3-cp38-cp38-win_amd64.whl", hash = "sha256:cb1f2f5e426dc9f07a7681419fe39cee823bb74f723f36f70399123f439e9b20"}, + {file = "PyYAML-5.3.tar.gz", hash = "sha256:e9f45bd5b92c7974e59bcd2dcc8631a6b6cc380a904725fce7bc08872e691615"}, ] requests = [ {file = "requests-2.22.0-py2.py3-none-any.whl", hash = "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31"}, {file = "requests-2.22.0.tar.gz", hash = "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4"}, ] six = [ - {file = "six-1.13.0-py2.py3-none-any.whl", hash = "sha256:1f1b7d42e254082a9db6279deae68afb421ceba6158efa6131de7b3003ee93fd"}, - {file = "six-1.13.0.tar.gz", hash = "sha256:30f610279e8b2578cab6db20741130331735c781b56053c59c4076da27f06b66"}, + {file = "six-1.14.0-py2.py3-none-any.whl", hash = "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c"}, + {file = "six-1.14.0.tar.gz", hash = "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a"}, +] +soupsieve = [ + {file = "soupsieve-1.9.5-py2.py3-none-any.whl", hash = "sha256:bdb0d917b03a1369ce964056fc195cfdff8819c40de04695a80bc813c3cfa1f5"}, + {file = "soupsieve-1.9.5.tar.gz", hash = "sha256:e2c1c5dee4a1c36bcb790e0fabd5492d874b8ebd4617622c4f6a731701060dda"}, ] tornado = [ {file = "tornado-6.0.3-cp35-cp35m-win32.whl", hash = "sha256:c9399267c926a4e7c418baa5cbe91c7d1cf362d505a1ef898fde44a07c9dd8a5"}, @@ -236,6 +289,6 @@ tornado = [ {file = "tornado-6.0.3.tar.gz", hash = "sha256:c845db36ba616912074c5b1ee897f8e0124df269468f25e4fe21fe72f6edd7a9"}, ] urllib3 = [ - {file = "urllib3-1.25.7-py2.py3-none-any.whl", hash = "sha256:a8a318824cc77d1fd4b2bec2ded92646630d7fe8619497b142c84a9e6f5a7293"}, - {file = "urllib3-1.25.7.tar.gz", hash = "sha256:f3c5fd51747d450d4dcf6f923c81f78f811aab8205fda64b0aba34a4e48b0745"}, + {file = "urllib3-1.25.8-py2.py3-none-any.whl", hash = "sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc"}, + {file = "urllib3-1.25.8.tar.gz", hash = "sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"}, ] diff --git a/pyproject.toml b/pyproject.toml index 8d01f99..3eac00b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,6 +8,8 @@ authors = ["Lukas Winkler "] python = "^3.8" requests = "^2.22.0" python-telegram-bot = "^12.2.0" +beautifulsoup4 = "^4.8.2" +pyyaml = "^5.3" [tool.poetry.dev-dependencies] diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 6f76ad8..0000000 --- a/requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -certifi==2019.11.28 -cffi==1.13.2 -chardet==3.0.4 -cryptography==2.8 -future==0.18.2 -idna==2.8 -pycparser==2.19 -python-telegram-bot==12.2.0 -requests==2.22.0 -six==1.13.0 -tornado==6.0.3 -urllib3==1.25.7 diff --git a/subscribe.py b/subscribe.py new file mode 100644 index 0000000..7eed130 --- /dev/null +++ b/subscribe.py @@ -0,0 +1,44 @@ +import telegram +import yaml +from telegram import Message, User, Chat + +from config import telegram_token + +try: + with open("db.yaml") as f: + db = yaml.safe_load(f) +except FileNotFoundError: + db = {"subscribed": {}, "last_update": 0} + +bot = telegram.Bot(token=telegram_token) +for update in bot.get_updates(offset=db["last_update"] + 1): + message: Message = update.message + text = message.text + user: User = update.effective_user + chat: Chat = update.effective_chat + if "/subscribe" in text: + if chat.id in db["subscribed"]: + bot.sendMessage(chat_id=chat.id, + text="Du bekommst bereits die Nachrichten. Verwende /unsubscribe zum Abmelden.") + else: + subscriber = { + "first_name": chat.first_name, + "last_name": chat.last_name, + "username": chat.username, + } + db["subscribed"][chat.id] = subscriber + bot.send_message(chat_id=chat.id, + text="Du bekommst nun regelmäßige Nachrichten. Verwende /unsubscribe zum Abmelden.") + elif "/unsubscribe" in text: + if chat.id in db["subscribed"]: + del db["subscribed"][chat.id] + bot.sendMessage(chat_id=chat.id, + text="Du bekommst keine Nachrichten mehr. Verwende /subscribe um sie wieder zu bekommen.") + else: + bot.sendMessage(chat_id=chat.id, + text="Du bekommst bereits keine Nachrichten. Verwende /subscribe um sie wieder zu bekommen.") + + db["last_update"] = update.update_id + +with open("db.yaml", "w") as f: + yaml.safe_dump(db, f)