Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import the required libraries [of code].
- require "rubygems"
- require "sequel"
- require "tweetstream"
- require "json"
- # Connect to the database.
- DB = Sequel.sqlite("tweets.sqlite")
- tweets = DB[:tweets]
- # Configure authorization.
- TweetStream.configure do |config|
- config.consumer_key = 'YOURCONSUMERKEY'
- config.consumer_secret = 'YOURCONSUMERSECRET'
- config.oauth_token = 'YOUROAUTHTOKEN'
- config.oauth_token_secret = 'YOURTOKENSECRET'
- config.auth_method = :oauth
- config.parser = :json_gem
- end
- # Start tracking tweets based on given criteria.
- TweetStream::Client.new.track('ows','occupyoakland','occupy') do |status|
- begin
- # Parse the tweets into the appropriate DB column.
- tweets.insert(
- :text => status.text,
- :username => status.user.screen_name,
- :name => status.user.name,
- :created_at => status.created_at,
- :lang => status.user.lang,
- :time_zone => status.user.time_zone,
- :guid => status[:id],
- :retweeted => status.retweeted,
- :in_reply_to_user_id => status.in_reply_to_user_id,
- :in_reply_to_status_id => status.in_reply_to_status_id,
- :location => status.user.location,
- :protected => status.user.protected,
- :verified => status.user.verified,
- :description => status.user.description,
- :followers_count => status.user.followers_count,
- :url => status.user.url,
- :statuses_count => status.user.statuses_count,
- :place => status.place,
- :retweet_count => status.retweet_count,
- :favourites_count => status.user.favourites_count,
- :geo_enabled => status.user.geo_enabled,
- :friends_count => status.user.friends_count,
- :entities => status.entities.to_s(),
- :source => status.source,
- :geo => status.geo.to_s(),
- :coordinates => status.coordinates.to_s()
- )
- # Print the tweets to the screen, to see what we're archiving.
- puts "[#{status.user.screen_name}] #{status.text}"
- # If something goes wrong, it's probably a db lock error.
- rescue
- puts "Couldnt insert tweet. Possibly db lock error"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment