Last active
August 30, 2016 16:51
-
-
Save matthoendorf/8f1247a1c61f4d95803de66ab1ce8a5d to your computer and use it in GitHub Desktop.
AlertMeh - send yourself an email every day when a Meh.com deal goes live
This file contains 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 json | |
import urllib.request | |
import smtplib | |
from email.mime.text import MIMEText | |
import schedule | |
import time | |
pwd = # your google app-specific password | |
api_key = # your meh.com api key | |
url = "https://api.meh.com/1/current.json?apikey=" + api_key | |
def job(t): | |
print("Initializing...") | |
data = urllib.request.urlopen( | |
url).read().decode('utf-8') | |
output = json.loads(data) | |
deal_title = output['deal']['title'] | |
deal_price = output['deal']['items'][0]['price'] | |
deal_features = output['deal']['features'] | |
deal_specs = output['deal']['specifications'] | |
deal_photo = output['deal']['items'][0]['photo'] | |
text1 = "<h3>" + deal_title + " - $" + str(deal_price) + ".00</h3>" | |
text2 = "<p>" + deal_features + "</p><p>" + deal_specs + "</p>" | |
text1 = text1.replace('\n', '<br/>') | |
text2 = text2.replace('\n', '<br/>') | |
html = """\ | |
<html> | |
<head>""" + text1 + """</head> | |
<body> | |
<img src='""" + deal_photo + """' width='200px'>""" + text2 + """ | |
</body> | |
</html> | |
""" | |
# Create a text/plain message | |
msg = MIMEText(html, 'html') | |
me = # your email address | |
msg['Subject'] = deal_title + " - $" + str(deal_price) | |
msg['From'] = me | |
msg['To'] = me | |
# Send the message via our own SMTP server, but don't include the | |
# envelope header. | |
s = smtplib.SMTP('smtp.gmail.com:587') | |
s.ehlo() | |
s.starttls() | |
s.login(me, pwd) | |
s.sendmail(me, [me], msg.as_string()) | |
s.quit() | |
print("Message sent!") | |
schedule.every().day.at("00:05").do(job, 'It is 12:05AM.') | |
while True: | |
schedule.run_pending() | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment