Send Discord notifications when stock is available

This commit is contained in:
agatha 2023-12-02 15:52:43 -05:00
parent f3990bc4d2
commit f82b47ac8b
2 changed files with 27 additions and 5 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ venv/
__pycache__/
*.py[cod]
config.py

30
main.py
View File

@ -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__':