From f82b47ac8bee7defda32dc6fb33d975b561efda6 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 2 Dec 2023 15:52:43 -0500 Subject: [PATCH] Send Discord notifications when stock is available --- .gitignore | 2 ++ main.py | 30 +++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index bbb4974..c1c906f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ venv/ __pycache__/ *.py[cod] + +config.py diff --git a/main.py b/main.py index 07f766d..b62f695 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ import requests from bs4 import BeautifulSoup +from config import DISCORD_WEBHOOK + BASE_URL = 'https://my.frantech.ca/' URLS = [ 'https://my.frantech.ca/cart.php?gid=37', # Las Vegas @@ -11,6 +13,13 @@ URLS = [ ] +def send_notification(payload): + try: + requests.post(DISCORD_WEBHOOK, json=payload) + except requests.RequestException as e: + print(f'error sending notification: {str(e)}') + + def get_url(url): try: response = requests.get(url) @@ -34,7 +43,7 @@ def get_packages(html): package['name'] = package_name package_quantity = package_element.find('div', class_='package-qty').text.strip() - package['qty'] = package_quantity + package['qty'] = int(package_quantity.split()[0]) order_button = package_element.find('a', class_='btn-primary') if order_button: @@ -54,10 +63,21 @@ def main(): packages = get_packages(html) for package in packages: - print('Package Name:', package['name']) - print('Package Quantity:', package['qty']) - print('Order URL:', package['url']) - print('---------------------------') + if package['qty'] > 0: + send_notification({ + "username": "stockbot-buyvm", + "embeds": [ + { + "author": { + "name": "BuyVM", + }, + "title": package['name'], + "url": package['url'], + "description": f"{package['qty']} in stock now!" + } + ], + "content": "STOCK ALERT" + }) if __name__ == '__main__':