Guest User

Untitled

a guest
May 28th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # By @zcutlip
  4. # Get a twitter user's public timeline and email new tweets
  5. # to a configurable list of addresses.
  6. # Tweet ids are recorded in ~/.twitterchecker/ and are not re-downloaded
  7. # or emailed.
  8.  
  9. import os
  10. import twitter
  11. import HTMLParser
  12. import time
  13. import errno
  14. import smtplib
  15. from email.mime.text import MIMEText
  16.  
  17. class Twstatus(object):
  18.     FORMAT="%a %b %d %H:%M:%S %Y %Z"
  19.     hparser=HTMLParser.HTMLParser()
  20.     def __init__(self,status):
  21.         self.text=Twstatus.hparser.unescape(status.text)
  22.         ltime=time.localtime(status.created_at_in_seconds)
  23.         self.date=ltime
  24.    
  25.     def pretty_date(self,fmt=FORMAT):
  26.         return time.strftime(fmt,self.date)
  27.  
  28. USER=<'insert_twitter_username'>
  29. ADDRESSES=[<'insert_destination_addresses'>]
  30. EMAIL=False
  31.  
  32. twtr_state_dir=os.getenv('HOME')+'/.twitterchecker/'
  33. twtr_state_file=twtr_state_dir+"twitter_status_ids.state"
  34.  
  35. def check_state_dir(dn):
  36.     d = os.path.dirname(dn)
  37.     if not os.path.exists(d):
  38.         os.makedirs(d)
  39.  
  40.  
  41. def id_to_statefile(id):
  42.     statefile=None
  43.     try:
  44.         statefile=open(twtr_state_file,'a')
  45.     except Exception as e:
  46.         print("failed to open twitter state file: "+twtr_state_file)
  47.         print(str(e))
  48.         exit(1)
  49.     try:
  50.         statefile.write("%s\n" % str(id))
  51.     except Exception as e:
  52.         print("failed to write to state file: "+twtr_state_file)
  53.         print(str(e))
  54.         statefile.close()
  55.         exit(1)
  56.  
  57. def max_id_recorded():
  58.     statefile=None
  59.     status_ids=[]
  60.     try:
  61.         statefile=open(twtr_state_file,'r')
  62.     except IOError as ioe:
  63.         if ioe.errno!=errno.ENOENT:
  64.             print("Failed to open twitter state file for reading: "+twtr_state_file)
  65.             print(str(ioe))
  66.             exit(1)
  67.         else:
  68.             print("State file doesn't exist.")
  69.             return 1
  70.    
  71.     try:
  72.         for line in statefile:
  73.             identifier=long(line)
  74.             status_ids.append(identifier)
  75.     except Exception as ioe:
  76.         print("Failed to read line from state file: "+line)
  77.         print(str(ioe))
  78.         statefile.close()
  79.         exit(1)
  80.    
  81.     if len(status_ids)==0:
  82.         return 1
  83.    
  84.     status_ids.sort()
  85.     return status_ids[-1]
  86.  
  87. def email_tweet(tweet,addresses):
  88.     email=MIMEText(tweet)
  89.     email['Subject'] = "Tweet from "+USER
  90.     email['From'] = <'insert_from_address'>
  91.     email['To'] = ', '.join(addresses)
  92.  
  93.     #wherever your smtp is. I use my own MTA
  94.     server = smtplib.SMTP('localhost')
  95.     server.sendmail(email['From'],addresses,email.as_string())
  96.  
  97.  
  98. def main():
  99.     twiapi=twitter.Api()
  100.     max_id=max_id_recorded()
  101.    
  102.     try:
  103.         check_state_dir(twtr_state_dir)
  104.     except Exception as e:
  105.         print("Failed to create "+twtr_state_dir)
  106.         print str(e)
  107.         exit(1)
  108.     statuses=[]
  109.     timeline=twiapi.GetUserTimeline(USER,since_id=max_id,count=200)
  110.     for status in timeline:
  111.         statuses.append(Twstatus(status))
  112.         id_to_statefile(status.id)
  113.    
  114.     first=True
  115.     for status in statuses:
  116.         tweet=("%s\n%s\n" %(status.pretty_date(),status.text))
  117.         if EMAIL:
  118.             if first:
  119.                 first=False
  120.             else:
  121.                 time.sleep(20)
  122.            
  123.             print("emailing tweet: \n"+tweet)
  124.             email_tweet(tweet,ADDRESSES)
  125.         else:
  126.             print(tweet)
  127.  
  128.  
  129. if __name__=="__main__":
  130.     main()
Add Comment
Please, Sign In to add comment