Skip to content

Instantly share code, notes, and snippets.

@gelendir
Created September 11, 2011 22:12
Show Gist options
  • Save gelendir/1210200 to your computer and use it in GitHub Desktop.
Save gelendir/1210200 to your computer and use it in GitHub Desktop.
Is steve jobs dead ?
#!/usr/bin/env python2
import urllib
import smtplib
import time
from HTMLParser import HTMLParser
from email.mime.text import MIMEText
SERVER = "smtp.gmail.com"
PORT = 587
USERNAME = "username"
PASSWORD = "password"
EMAIL = USERNAME + "@gmail.com"
URL = "http://www.isstevejobsdead.com"
TIMEOUT = 5 * 60
class StatusScraper( HTMLParser ):
"""
Scrape the HTML on isstevejobsdead.com to find out
if Steve Jobs is alive or dead
"""
def __init__( self, *args, **kwargs ):
HTMLParser.__init__( self, *args, **kwargs )
self.in_block = False
self.dead = None
self.status_alive = "Nope."
def handle_starttag( self, tag, attrs ):
tag = tag.lower()
attrs = dict( attrs )
if tag == "div" and attrs['id'] == "main":
self.in_block = False
def handle_data( self, data ):
if self.in_block:
if data == self.status_alive:
self.dead = False
else:
self.dead = True
def handle_endtag( self, tag ):
if self.in_block and tag.lower == "div":
self.in_block = False
class SMTPSender(object):
"""
Simple wrapper around smtplib and MIMEText for sending emails
"""
def __init__(self, server, port=25, tls=False, user=None, password=None):
self.server = server
self.port = port
self.tls = tls
self.user = user
self.password = password
def send(self, addr_from, addr_to, subject, text):
msg = MIMEText(text.encode('utf-8'), _charset='utf-8')
msg['From'] = addr_from
msg['To'] = addr_to
msg['Subject'] = subject
conn = smtplib.SMTP(self.server, self.port)
if self.tls:
conn.starttls()
conn.ehlo_or_helo_if_needed()
if self.user and self.password:
conn.login( self.user, self.password )
conn.sendmail( addr_from, addr_to, msg.as_string() )
if __name__ == "__main__":
dead = False
while not dead:
html = urllib.urlopen( URL ).read()
status_scraper = StatusScraper()
status_scraper.feed( html )
dead = status_scraper.dead
if dead:
smtp = SMTPSender( SERVER, PORT, True, USERNAME, PASSWORD )
print "Steve Jobs is DEAD ! Sending email..."
smtp.send( EMAIL,
EMAIL,
"Steve Jobs is DEAD !",
"STEVE JOBS IS FINALLY DEAD !!!!!" )
else:
print "Steve jobs isn't dead yet. Checking again in %s seconds" % TIMEOUT
time.sleep( TIMEOUT )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment