Guest User

Python Script to Check for K9 Masks In Stock

a guest
Jan 15th, 2025
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. # Make sure python and pip are already installed on the computer/server
  2. # Make sure you've also run 'pip install requests beautifulsoup4 schedule' because they're needed for this script
  3.  
  4. import requests
  5. from bs4 import BeautifulSoup
  6. import schedule
  7. import time
  8. import smtplib
  9. import sys
  10. from email.mime.text import MIMEText
  11. from email.mime.multipart import MIMEMultipart
  12.  
  13. SMTP_SERVER = "smtp.gmail.com"
  14. SMTP_PORT = 587
  15. SENDER_EMAIL = "[email protected]"
  16. # !! Not safe to secure a plaintext password in a script if AWS instance security group has inbound rule that is open to all of the internet
  17. SENDER_PASSWORD = "your app password"
  18. RECEIVER_EMAIL = "[email protected]"
  19. # archived in-stock version for testing
  20. # PRODUCT_URL = "https://web.archive.org/web/20240117232056/https://www.k9mask.com/products/k9-mask%C2%AE-for-dogs-with-3-extreme-breathe-n95-active-carbon-air-filters?variant=33008752623669"
  21. PRODUCT_URL = "https://www.k9mask.com/products/k9-mask%C2%AE-for-dogs-with-3-extreme-breathe-n95-active-carbon-air-filters"
  22.  
  23. def send_email():
  24.     subject = "Medium K9 masks in stock!"
  25.     body = f"Masks are in stock\n\n{PRODUCT_URL}"
  26.  
  27.     msg = MIMEMultipart()
  28.     msg["From"] = SENDER_EMAIL
  29.     msg["To"] = RECEIVER_EMAIL
  30.     msg["Subject"] = subject
  31.     msg.attach(MIMEText(body, "plain"))
  32.  
  33.     try:
  34.         with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
  35.             server.starttls()
  36.             server.login(SENDER_EMAIL, SENDER_PASSWORD)
  37.             server.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, msg.as_string())
  38.         print("Email sent")
  39.     except Exception as e:
  40.         print(f"Error sending email: {e}")
  41.  
  42. def check_stock():
  43.     headers = {
  44.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
  45.     }
  46.  
  47.     try:
  48.         response = requests.get(PRODUCT_URL, headers=headers)
  49.         response.raise_for_status()
  50.         soup = BeautifulSoup(response.text, 'html.parser')
  51.  
  52.         medium_option = soup.find("option", string=lambda text: "Medium" in text and "Sold Out" in text)
  53.         if medium_option:
  54.             return False
  55.         else:
  56.             return True
  57.  
  58.     except requests.exceptions.RequestException as e:
  59.         print(f"Error checking stock: {e}")
  60.  
  61. def email_if_in_stock():
  62.     if check_stock():
  63.         send_email()
  64.         sys.exit(0)
  65.     else:
  66.         print("Sold out")
  67.  
  68. schedule.every(1).minutes.do(email_if_in_stock)
  69.  
  70. while True:
  71.     schedule.run_pending()
  72.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment