#!/usr/bin/python #-------------------------------------------------------------------# # twitter-2-mail # # # # Autor: Markus Becker / # # License: BSD # # Version: 0.4 / 2010-03-28 # #-------------------------------------------------------------------# ### Vaguely Customizable Options ### # TWitter Account + PWD: G_TW_ACC = "putYourAccountNameHere" G_TW_PWD = "putYourPasswordHere" G_FILE = "/home/markus/twitter2mail/twitter_time.txt" # Choose a path for a buffer-file # The email address messages are from by default: DEFAULT_FROM = "putYourFromEmailAdressHere" MAIL_TO = "putYourToEmailAdressHere" MAIL_SUBJECT = "[Twitter] Aktuelles vom" # To most correctly encode emails with international characters, we iterate through the list below and use the first character set that works # Eventually (and theoretically) ISO-8859-1 and UTF-8 are our catch-all failsafes CHARSET_LIST='US-ASCII', 'BIG5', 'ISO-2022-JP', 'ISO-8859-1', 'UTF-8' from email.MIMEText import MIMEText from email.Header import Header from email.Utils import parseaddr, formataddr import twitter import sys, os, string import time # Note: You can also override the send function. def send(sender, recipient, subject, body, contenttype, extraheaders=None, smtpserver=None): # Header class is smart enough to try US-ASCII, then the charset we # provide, then fall back to UTF-8. header_charset = 'ISO-8859-1' # We must choose the body charset manually for body_charset in CHARSET_LIST: try: body.encode(body_charset) except UnicodeError: pass else: break # Split real name (which is optional) and email address parts sender_name, sender_addr = parseaddr(sender) recipient_name, recipient_addr = parseaddr(recipient) # We must always pass Unicode strings to Header, otherwise it will # use RFC 2047 encoding even on plain ASCII strings. sender_name = str(Header(unicode(sender_name), header_charset)) recipient_name = str(Header(unicode(recipient_name), header_charset)) # Make sure email addresses do not contain non-ASCII characters sender_addr = sender_addr.encode('ascii') recipient_addr = recipient_addr.encode('ascii') # Create the message ('plain' stands for Content-Type: text/plain) msg = MIMEText(body.encode(body_charset), contenttype, body_charset) msg['To'] = formataddr((recipient_name, recipient_addr)) msg['Subject'] = Header(unicode(subject), header_charset) fromhdr = formataddr((sender_name, sender_addr)) msg['From'] = fromhdr msg_as_string = msg.as_string() i, o = os.popen2(["/usr/sbin/sendmail", recipient]) i.write(msg_as_string) i.close(); o.close() pid, status = os.wait() if status != 0: print >>warn, "" print >>warn, ('Fatal error: sendmail exited with code %s' % status) sys.exit(1) del i, o return None def readfile (src) : try : f = open (src, "r") lasttime = f.readline () f.close () except : lasttime = "0" return lasttime def writefile (src, last) : f = open (src, "w") f.write (last) f.close () if __name__ == '__main__': args = sys.argv twapi = twitter.Api (username = G_TW_ACC, password = G_TW_PWD) lasttime = string.atoi(readfile (G_FILE)) msg_list = twapi.GetFriendsTimeline(since_id=lasttime) content = "Aktuelle Tweets:" content = string.join ([content, "----------------------"], "\n") if len (msg_list) : for msg in msg_list : msgtxt = string.join ([msg._created_at, ": ", msg.user._screen_name, ": \n", msg.text], "") content = string.join ([content, msgtxt], "\n\n") if lasttime < msg.id : lasttime = msg.id writefile (G_FILE, str (lasttime)) contenttype = 'plain' sub = string.join ([MAIL_SUBJECT, time.strftime ("%Y-%m-%d %H:%M")]) send (DEFAULT_FROM, MAIL_TO,sub , content, contenttype)