From 6c33e418e2b642a00bd502b3efb98602f283074a Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 2 Dec 2023 14:49:46 -0500 Subject: [PATCH 1/3] Get package objects from HTML --- main.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 45 insertions(+) create mode 100644 main.py create mode 100644 requirements.txt diff --git a/main.py b/main.py new file mode 100644 index 0000000..384932f --- /dev/null +++ b/main.py @@ -0,0 +1,44 @@ +"""buyvm stock checker""" +from bs4 import BeautifulSoup + + +def get_packages(html): + soup = BeautifulSoup(html, 'html.parser') + packages = [] + + package_elements = soup.find_all('div', class_='package') + for package_element in package_elements: + package = {} + + package_name = package_element.find('h3', class_='package-name').text.strip() + package['name'] = package_name + + package_quantity = package_element.find('div', class_='package-qty').text.strip() + package['qty'] = package_quantity + + order_button = package_element.find('a', class_='btn-primary') + if order_button: + order_url = order_button['href'] + package['url'] = order_url + else: + package['url'] = '' + + packages.append(package) + + return packages + + +def main(): + with open('tests/data/stock.html', 'r', encoding='utf-8') as file: + html = file.read() + + 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 __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c1f5f71 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +beautifulsoup4 From f3990bc4d20b549eecee3cd3e21fef8cd9d92bb0 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 2 Dec 2023 15:33:07 -0500 Subject: [PATCH 2/3] Scrape all locations --- main.py | 38 +++++++++++++++++++++++++++++--------- requirements.txt | 1 + 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 384932f..07f766d 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,26 @@ """buyvm stock checker""" +import requests from bs4 import BeautifulSoup +BASE_URL = 'https://my.frantech.ca/' +URLS = [ + 'https://my.frantech.ca/cart.php?gid=37', # Las Vegas + 'https://my.frantech.ca/cart.php?gid=38', # New York + 'https://my.frantech.ca/cart.php?gid=48', # Miami + 'https://my.frantech.ca/cart.php?gid=39', # Luxembourg +] + + +def get_url(url): + try: + response = requests.get(url) + response.raise_for_status() + except requests.RequestException as e: + print(f'error fetching {url}: {str(e)}') + return None + + return response.text + def get_packages(html): soup = BeautifulSoup(html, 'html.parser') @@ -19,7 +39,7 @@ def get_packages(html): order_button = package_element.find('a', class_='btn-primary') if order_button: order_url = order_button['href'] - package['url'] = order_url + package['url'] = BASE_URL + order_url else: package['url'] = '' @@ -29,15 +49,15 @@ def get_packages(html): def main(): - with open('tests/data/stock.html', 'r', encoding='utf-8') as file: - html = file.read() + for url in URLS: + html = get_url(url) - packages = get_packages(html) - for package in packages: - print('Package Name:', package['name']) - print('Package Quantity:', package['qty']) - print('Order URL:', package['url']) - print('---------------------------') + 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 __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt index c1f5f71..1f3e778 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ beautifulsoup4 +requests From f82b47ac8bee7defda32dc6fb33d975b561efda6 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 2 Dec 2023 15:52:43 -0500 Subject: [PATCH 3/3] 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__':