Personal Tweet Mailer

I wanted to create a twitter feed of things my daughters were doing with the goal of easily updating the grandparents on what they were doing. Now, they aren't twitter users but I use twitter enough that it was an easy medium. I wrote a little python script that runs every few minutes and emails them the updates.

I know there are tools out there that turn RSS into email, but that would require that I know all the account credentials, which wasn't desired. Since this is a known audience, I don't have to worry about opt out so I can just use direct email method. Below is the source code so please use if you want and as always, feel free to comment.

  1. #!/usr/bin/python
  2.  
  3. ##########################################################################################################
  4. # tweetMail.py
  5. # File created to enable simple emailing of twitter feed. Good for emailing to people who don't use
  6. # Twitter and/or cannot use an RSS reader.
  7. # Author: Mike Dunphy
  8. # License: This is script is released into the public domain with no guarantee or waranty of any kind.
  9. # please use at your own discretion and/or peril
  10. ##########################################################################################################
  11.  
  12. import sys,smtplib
  13. sys.path.append('/home/miked/feedparse') # FeedParser module location for share hosting
  14. import feedparser
  15.  
  16. def send_mail(message):
  17. smtpObj = smtplib.SMTP('localhost')
  18. sender = 'email@address.com' #From address of emails
  19. receivers = ['email@address.com'] #Multiple addresses seperated by commas
  20. smtpObj.sendmail(sender,receivers,message)
  21. smtpObj.quit()
  22.  
  23. FILEPATH = '/path/to/file' #This file keeps track of last twitter uid
  24.  
  25. feed = feedparser.parse("Twitter URL goes here") #Remember that if the account is protected add credentials
  26.  
  27. f = open(FILEPATH)
  28. lastvalue = int(f.readline())
  29. f.close()
  30. for entry in feed.entries:
  31. if (int(entry.id[40:]) > int(lastvalue)): #This is ugly but 40 characters chops the URL leaving just
  32. #the twitter uid. Change this to fit your feed.
  33.  
  34. #The message headers repeat to section one address per line. Can add CC: entries too
  35. #Reply-To section defines a reply-to address if you don't want replies sent to sender address
  36. message1 = """From: From Address <email@address.com>
  37. To: To address <email@address.com>
  38. Reply-To: <email@address.com>
  39. Subject: Message Subject
  40. """
  41.  
  42. message2 = entry.summary[13:] #Again, this is ugly but the [13:] chops the username out of the summary. Change for your use
  43. message = message1 + '\n' + message2
  44. send_mail(message)
  45. f = open(FILEPATH,'w')
  46. f.write(feed.entries[0].id[40:])
  47. f.close()