Created
September 13, 2010 22:19
-
-
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.
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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.