Skip to content

Instantly share code, notes, and snippets.

@losvedir
Created September 13, 2010 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save losvedir/578167 to your computer and use it in GitHub Desktop.
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.
#!/usr/bin/python
import os
import subprocess as sub
import urllib2
from BeautifulSoup import BeautifulSoup
TWITTERUSER = cupcakory # if you want to check twitter.com/cupcakory for updates
EMAILADDR = gabe@example.com
# get the most recent tweet from Twitter
page = urllib2.urlopen("http://twitter.com/" + 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')
try:
f = open(filename, 'r')
fileTweet = f.read();
if fileTweet != twitterTweet:
newTweet = True
f.close()
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:
f.write(twitterTweet)
# 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)
p.communicate(input=mailMsg)
@losvedir
Copy link
Author

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.

@losvedir
Copy link
Author

Brief walkthrough here: http://www.gabedurazo.com/blog/?p=39

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