Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Created May 9, 2022 00:47
Show Gist options
  • Save luckylittle/0a30e0cf2dee75ecb2a478d607474ed0 to your computer and use it in GitHub Desktop.
Save luckylittle/0a30e0cf2dee75ecb2a478d607474ed0 to your computer and use it in GitHub Desktop.
InformIT eBook Deal of the Day Mailinator
#!venv/bin/python3
import datetime
import smtplib
import time
import requests
import sys
import random
from bs4 import BeautifulSoup
# chmod x informit.py
# python3 -m venv venv
# source venv/bin/activate
# pip install --upgrade pip
# pip install requests beautifulsoup4
class bcolors:
SUCCESS = "\033[92m"
WARNING = "\033[93m"
ERROR = "\033[91m"
ENDC = "\033[0m"
URL = "https://www.informit.com/deals/"
smtp_username = "<INSERT_HERE>"
smtp_password = "<INSERT_HERE>"
email_address = "<INSERT_HERE>"
headers = {
"cache-control": "max-age=0",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"sec-fetch-dest": "document",
"accept": "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"accept-language": "en-US,en;q=0.9,cs;q=0.8",
}
today = datetime.date.today()
title = ""
threshold = 0
server = smtplib.SMTP("smtp.gmail.com", 587)
email = email_address
def check_price():
global title
global price
global discountPercentage
global buy_suffix
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, "html.parser")
title = soup.find("h2", {"class": "title gtm_title"}).text.strip()
price = soup.find("strong", {"class": "gtm_price"}).text.strip()
discountPercentage = soup.find("span", {"class": "discountPercentage"}).text.strip()
buy_suffix = soup.find("div", {"class": "buyOption"}).find(
"a", {"class": "productPurchase buy button"}
)["href"]
if len(title) == 0 or len(price) == 0:
print(f"{bcolors.ERROR}The item you were tracking was not found{bcolors.ENDC}")
send_warning_mail()
end_script()
print(
f"eBook Deal of the Day on {today} is {title} for USD {price} ({discountPercentage}). Buy it here: https://www.informit.com{buy_suffix}"
)
send_success_mail()
def setup_email_service():
server.ehlo()
server.starttls()
server.ehlo()
server.login(smtp_username, smtp_password)
def send_email(msg):
server.sendmail(smtp_username, email, msg)
print("Email has been sent!")
server.quit()
def send_success_mail():
setup_email_service()
subject = f"InformIT eBook Deal of the Day - {title}"
body = f"eBook Deal of the Day on {today} is {title} for USD {price} ({discountPercentage}). Buy it here: https://www.informit.com{buy_suffix}"
msg = f"Subject: {subject}\n\n{body}"
send_email(msg)
def send_warning_mail():
setup_email_service()
subject = "The item you were tracking was not found"
body = f"Something has changed and the link {URL} is probably no longer valid"
msg = f"Subject: {subject}\n\n{body}"
send_email(msg)
def end_script():
print("Exiting the script...")
sys.exit()
check_price()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment