Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # By @zcutlip
- # Get a twitter user's public timeline and email new tweets
- # to a configurable list of addresses.
- # Tweet ids are recorded in ~/.twitterchecker/ and are not re-downloaded
- # or emailed.
- import os
- import twitter
- import HTMLParser
- import time
- import errno
- import smtplib
- from email.mime.text import MIMEText
- class Twstatus(object):
- FORMAT="%a %b %d %H:%M:%S %Y %Z"
- hparser=HTMLParser.HTMLParser()
- def __init__(self,status):
- self.text=Twstatus.hparser.unescape(status.text)
- ltime=time.localtime(status.created_at_in_seconds)
- self.date=ltime
- def pretty_date(self,fmt=FORMAT):
- return time.strftime(fmt,self.date)
- USER=<'insert_twitter_username'>
- ADDRESSES=[<'insert_destination_addresses'>]
- EMAIL=False
- twtr_state_dir=os.getenv('HOME')+'/.twitterchecker/'
- twtr_state_file=twtr_state_dir+"twitter_status_ids.state"
- def check_state_dir(dn):
- d = os.path.dirname(dn)
- if not os.path.exists(d):
- os.makedirs(d)
- def id_to_statefile(id):
- statefile=None
- try:
- statefile=open(twtr_state_file,'a')
- except Exception as e:
- print("failed to open twitter state file: "+twtr_state_file)
- print(str(e))
- exit(1)
- try:
- statefile.write("%s\n" % str(id))
- except Exception as e:
- print("failed to write to state file: "+twtr_state_file)
- print(str(e))
- statefile.close()
- exit(1)
- def max_id_recorded():
- statefile=None
- status_ids=[]
- try:
- statefile=open(twtr_state_file,'r')
- except IOError as ioe:
- if ioe.errno!=errno.ENOENT:
- print("Failed to open twitter state file for reading: "+twtr_state_file)
- print(str(ioe))
- exit(1)
- else:
- print("State file doesn't exist.")
- return 1
- try:
- for line in statefile:
- identifier=long(line)
- status_ids.append(identifier)
- except Exception as ioe:
- print("Failed to read line from state file: "+line)
- print(str(ioe))
- statefile.close()
- exit(1)
- if len(status_ids)==0:
- return 1
- status_ids.sort()
- return status_ids[-1]
- def email_tweet(tweet,addresses):
- email=MIMEText(tweet)
- email['Subject'] = "Tweet from "+USER
- email['From'] = <'insert_from_address'>
- email['To'] = ', '.join(addresses)
- #wherever your smtp is. I use my own MTA
- server = smtplib.SMTP('localhost')
- server.sendmail(email['From'],addresses,email.as_string())
- def main():
- twiapi=twitter.Api()
- max_id=max_id_recorded()
- try:
- check_state_dir(twtr_state_dir)
- except Exception as e:
- print("Failed to create "+twtr_state_dir)
- print str(e)
- exit(1)
- statuses=[]
- timeline=twiapi.GetUserTimeline(USER,since_id=max_id,count=200)
- for status in timeline:
- statuses.append(Twstatus(status))
- id_to_statefile(status.id)
- first=True
- for status in statuses:
- tweet=("%s\n%s\n" %(status.pretty_date(),status.text))
- if EMAIL:
- if first:
- first=False
- else:
- time.sleep(20)
- print("emailing tweet: \n"+tweet)
- email_tweet(tweet,ADDRESSES)
- else:
- print(tweet)
- if __name__=="__main__":
- main()
Add Comment
Please, Sign In to add comment