oyouareatubeo

Archive Da Tubes

Dec 11th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.03 KB | None | 0 0
  1. # Import the required libraries [of code].
  2. require "rubygems"
  3. require "sequel"
  4. require "tweetstream"
  5. require "json"
  6.  
  7.  
  8. # Connect to the database.
  9. DB = Sequel.sqlite("tweets.sqlite")
  10. tweets = DB[:tweets]
  11.  
  12. # Configure authorization.
  13. TweetStream.configure do |config|
  14.   config.consumer_key = 'YOURCONSUMERKEY'
  15.   config.consumer_secret = 'YOURCONSUMERSECRET'
  16.   config.oauth_token = 'YOUROAUTHTOKEN'
  17.   config.oauth_token_secret = 'YOURTOKENSECRET'
  18.   config.auth_method = :oauth
  19.   config.parser   = :json_gem
  20. end
  21.  
  22. # Start tracking tweets based on given criteria.
  23. TweetStream::Client.new.track('ows','occupyoakland','occupy') do |status|
  24.   begin
  25.    # Parse the tweets into the appropriate DB column.
  26.    tweets.insert(
  27.     :text => status.text,
  28.     :username => status.user.screen_name,
  29.     :name => status.user.name,
  30.     :created_at => status.created_at,
  31.     :lang => status.user.lang,
  32.     :time_zone => status.user.time_zone,
  33.     :guid => status[:id],
  34.     :retweeted => status.retweeted,
  35.     :in_reply_to_user_id => status.in_reply_to_user_id,
  36.     :in_reply_to_status_id => status.in_reply_to_status_id,
  37.     :location => status.user.location,
  38.     :protected => status.user.protected,
  39.     :verified => status.user.verified,
  40.     :description => status.user.description,
  41.     :followers_count => status.user.followers_count,
  42.     :url => status.user.url,
  43.     :statuses_count => status.user.statuses_count,
  44.     :place => status.place,
  45.     :retweet_count => status.retweet_count,
  46.     :favourites_count => status.user.favourites_count,
  47.     :geo_enabled => status.user.geo_enabled,
  48.     :friends_count => status.user.friends_count,
  49.     :entities => status.entities.to_s(),
  50.     :source => status.source,
  51.     :geo => status.geo.to_s(),
  52.     :coordinates => status.coordinates.to_s()
  53.     )
  54.    # Print the tweets to the screen, to see what we're archiving.
  55.    puts "[#{status.user.screen_name}] #{status.text}"
  56.   # If something goes wrong, it's probably a db lock error.
  57.   rescue
  58.    puts "Couldnt insert tweet. Possibly db lock error"
  59.   end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment