5eem_q
Last Updated: February 25, 2016
·
531
· natural-affinity

Twitter Streaming API: Be Explicit

Rather than mining tweets based on individual terms,
we can also require that tweets contain ALL of our desired terms.
e.g. "apple" or "maps" VS "apple" and "maps"

Logical ORs are comma-delimited
e.g. "apple", "maps"

Logical ANDs are space-delimited:
e.g. "apple maps"

We can even combine the two:
e.g. "apple maps", "apple google"

require 'twitter'
require 'tweetstream'

# Tweets with the following terms
criteria = ['apple maps', 'google apple']

TweetStream.configure do |config|
  config.consumer_key = ENV['CONSUMER_KEY']
  config.consumer_secret = ENV['CONSUMER_SECRET']
  config.oauth_token = ENV['OAUTH_TOKEN']
  config.oauth_token_secret = ENV['OAUTH_SECRET']
  config.auth_method = :oauth
end

# Match Tweets with Apple AND (Google OR Maps)
TweetStream::Client.new.track(*criteria) do |status|
  tweet = status.text.downcase     
  term = tweet.include?('google') ? 'Google' : 'Maps'

  puts "#{status.user[:id]} tweeted about #{term}"
end

This gives us the ability to more explicitly capture only those tweets that we truly desire.