Skip to content

Instantly share code, notes, and snippets.

Created September 13, 2010 22:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save losvedir/578167 to your computer and use it in GitHub Desktop.
Checks a twitter account's most recent tweet. Sends an email if new.
import os
import subprocess as sub
import urllib2
from BeautifulSoup import BeautifulSoup
TWITTERUSER = cupcakory # if you want to check for updates
# get the most recent tweet from Twitter
page = urllib2.urlopen("" + TWITTERUSER)
soup = BeautifulSoup(page)
contentTags = soup.findAll('span', attrs={"class" : "entry-content"})
twitterTweet = contentTags[0].string
# Have we seen that Tweet before, and saved it?
newTweet = False
filename = os.path.join(os.path.dirname(__file__), 'recentest.txt')
f = open(filename, 'r')
fileTweet =;
if fileTweet != twitterTweet:
newTweet = True
except IOError:
newTweet = True # file not present, make new file with current tweet
# if new, overwrite old file with new tweet and send email alert
if newTweet:
with open(filename, 'w') as f:
# prepare mail messages:
mailMsg = "to:" + EMAILADDR + "\n"
mailMsg += "subject:Update from " + TWITTERUSER + "\n\n"
mailMsg += twitterTweet
mailMsg += "\n"
# send via sendmail subprocess
p = sub.Popen(['/usr/sbin/sendmail -t'], shell=True, stdin=sub.PIPE)
Copy link

Full disclosure: Only as I was pasting it into this gist box did I pull the TWITTERUSER and EMAILADDR out and up to the top -- haven't tested it. It works if you just hardcode that info into the script, though, as I did.

Copy link

Brief walkthrough here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment