From 98bdca71cd4cc47747f4493f78f56a2bbcee9526 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 14 Jan 2021 09:55:38 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ check.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + settings.dist.ini | 9 ++++++++ 4 files changed, 66 insertions(+) create mode 100644 .gitignore create mode 100644 check.py create mode 100644 requirements.txt create mode 100644 settings.dist.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..856d808 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +settings.ini +venv/ +.vscode \ No newline at end of file diff --git a/check.py b/check.py new file mode 100644 index 0000000..047f71e --- /dev/null +++ b/check.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import configparser +import urllib.request + +import telegram + +def check_availability(): + request = urllib.request.Request( + url="https://manage.edis.at/whmcs/cart.php?a=add&pid=976", + headers={"user-agent": "Edis Availability Check (https://kumig.it/kumitterer/edis-availability-check/)"} + ) + + response = urllib.request.urlopen(request).read().decode() + + if "Oops, there's a problem" in response: + return False + + return True + +def notify(text, token, recipients): + bot = telegram.Bot(token.split()[0]) + + for recipient in recipients.split(): + if recipient == "#": + break + + try: + bot.send_message(recipient, text) + print("Notified recipient %s" % recipient) + except: + print("Could not notify recipient %s" % recipient) + +def main(): + config = configparser.ConfigParser() + config.read("settings.ini") + + available = check_availability() + + if available and not config.getboolean("STATUS", "Notified"): + notify("HP DL320 Graz is available now!", config["TELEGRAM"]["Token"], config["TELEGRAM"]["Recipients"]) + config["STATUS"]["Notified"] = "yes" + elif config.getboolean("STATUS", "Notified") and not available: + notify("HP DL320 Graz is no longer available!", config["TELEGRAM"]["Token"], config["TELEGRAM"]["Recipients"]) + config["STATUS"]["Notified"] = "no" + else: + return + + with open("settings.ini", "w") as configfile: + config.write(configfile) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..11a50f9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +python-telegram-bot \ No newline at end of file diff --git a/settings.dist.ini b/settings.dist.ini new file mode 100644 index 0000000..9252b3f --- /dev/null +++ b/settings.dist.ini @@ -0,0 +1,9 @@ +[TELEGRAM] +token = INSERT_TOKEN_HERE # Your Telegram bot's token +recipients = 1234567 7654321 # Telegram chat IDs to notify, separated by spaces + +# No changes beyond this point + +[STATUS] +notified = no +