Advertisement
Guest User

Untitled

a guest
Feb 24th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import sys
  2. import tweepy
  3. import webbrowser
  4. import sqlite3 as lite
  5.  
  6. # Query terms
  7.  
  8. Q = sys.argv[1:]
  9.  
  10. sqlite3file='/var/www/twitter.lbox.com/html/stream5_log.sqlite'
  11.  
  12. CONSUMER_KEY = 'HfYi7CoZoH3DvzHkfmau1g'
  13. CONSUMER_SECRET = 'CfrPYXhk1q6cT9KdcnHQyJcALg6Ba0xnD3U2MRQ8k4U'
  14. ACCESS_TOKEN = '501088042-y380jKOWFtpZ3RmrCQRW70dOrqrKW8YDrNbiWnJI'
  15. ACCESS_TOKEN_SECRET = 'syUpRCKzdkmNnxbMkqDHXHgab1ow36MPzB9FVm793fY'
  16.  
  17. auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  18. auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
  19.  
  20. con = lite.connect(sqlite3file)
  21. cur = con.cursor()
  22. cur.execute("CREATE TABLE TWEETS(txt text, author text, created int, source text)")
  23.  
  24. class CustomStreamListener(tweepy.StreamListener):
  25.  
  26.     def on_status(self, status):
  27.        
  28.         try:
  29.             print "%s\t%s\t%s\t%s" % (status.text,
  30.                                       status.author.screen_name,
  31.                                       status.created_at,
  32.                                       status.source,)
  33.            
  34.             cur.executemany("INSERT INTO TWEETS(?, ?, ?)", (status.text,
  35.                                       status.author.screen_name,
  36.                                       status.created_at,
  37.                                       status.source)
  38.                          
  39.         except Exception, e:
  40.             print >> sys.stderr, 'Encountered Exception:', e
  41.             pass
  42.  
  43.     def on_error(self, status_code):
  44.         print >> sys.stderr, 'Encountered error with status code:', status_code
  45.         return True # Don't kill the stream
  46.  
  47.     def on_timeout(self):
  48.         print >> sys.stderr, 'Timeout...'
  49.         return True # Don't kill the stream
  50.  
  51. streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
  52.  
  53. print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)
  54.  
  55. streaming_api.filter(follow=None, track=Q)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement