Last active
March 28, 2020 15:22
-
-
Save yoonbae81/42a8b7d659b12c9606c1036f6b23e5b5 to your computer and use it in GitHub Desktop.
Send the first page of New York Times to Kindle
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
#!/usr/bin/env python3 | |
import urllib.request | |
import smtplib | |
from email.mime.application import MIMEApplication | |
from email.mime.multipart import MIMEMultipart | |
from datetime import datetime, timedelta | |
KINDLE_ID = '[email protected]' | |
GMAIL_ID = '[email protected]' | |
GMAIL_PW = '__________' # If you're using 2-step authentication, generate and use the App Password here | |
def download(): | |
url = 'https://static01.nyt.com/images/%Y/%m/%d/nytfrontpage/scan.pdf' | |
url = datetime.strftime(datetime.today() - timedelta(days=1), url) | |
filepath, _ = urllib.request.urlretrieve(url) | |
return filepath | |
def prepare(filepath): | |
email = MIMEMultipart() | |
email['To'] = KINDLE_ID | |
email['From'] = GMAIL_ID | |
email['Subject'] = 'Convert' | |
with open(filepath, 'rb') as file: | |
part = MIMEApplication(file.read(), | |
Name=f'NYTimes {datetime.today():%b %d}.pdf') | |
email.attach(part) | |
return email | |
def send(email): | |
smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465) | |
smtp.ehlo() | |
smtp.login(GMAIL_ID, GMAIL_PW) | |
smtp.sendmail(GMAIL_ID, KINDLE_ID, email.as_string()) | |
smtp.close() | |
return True | |
if __name__ == '__main__': | |
filepath = download() | |
email = prepare(filepath) | |
if send(email): | |
print("Sent") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment