Last active
February 23, 2018 19:49
-
-
Save feeeper/dc8f1c752406a5fcdfb6584e48fbaec1 to your computer and use it in GitHub Desktop.
Simple script for getting book title to email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
import requests | |
import argparse | |
from bs4 import BeautifulSoup | |
from email.message import EmailMessage | |
def main(): | |
parser = argparse.ArgumentParser(description='Send email with new book title to email') | |
parser.add_argument('-smtp_host', help='SMTP Host address') | |
parser.add_argument('-smtp_port', help='SMTP Host port', default=25) | |
parser.add_argument('-smtp_login', help='SMTP Login') | |
parser.add_argument('-smtp_pwd', help='SMTP Password') | |
parser.add_argument('-from_email', help='From email') | |
parser.add_argument('-to_email', help='To email') | |
args = parser.parse_args() | |
print(args) | |
html = requests.get('https://www.packtpub.com/packt/offers/free-learning', headers={'USER-AGENT': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'}) | |
soup = BeautifulSoup(str(html.content), 'html.parser') | |
title = soup.find('div', {'class': 'dotd-title'}).find('h2').text.replace('\\n', '').replace('\\t', '') | |
print('Title: {}'.format(title)) | |
msg = EmailMessage() | |
msg.set_content(title) | |
msg['Subject'] = 'New Book at Free Learning' | |
msg['From'] = args.from_email | |
msg['To'] = args.to_email | |
smtp = { | |
'host': args.smtp_host, | |
'port': args.smtp_port, | |
'login': args.smtp_login, | |
'password': args.smtp_pwd | |
} | |
s = smtplib.SMTP_SSL(smtp['host'], smtp['port']) | |
s.login(smtp['login'], smtp['password']) | |
s.send_message(msg) | |
s.quit() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment