cmart
cmart

Reputation: 91

How Do I search Twitter for a word with Ruby?

I have written code in Ruby that will display the timeline for a specific user. I would like to write code to be able to just search twitter to just find every user that has mentioned a word. My code is currently:

require 'rubygems'
require 'oauth'
require 'json'

# Now you will fetch /1.1/statuses/user_timeline.json,
# returns a list of public Tweets from the specified
# account.


  baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/user_timeline.json"
query   = URI.encode_www_form(
    "q" => "Obama"
    )
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

# Print data about a list of Tweets
def print_timeline(tweets)
  tweets.each do |tweet|
  require 'date'
    d = DateTime.parse(tweet['created_at'])
    puts " #{tweet['text'].delete ","} , #{d.strftime('%d.%m.%y')} , #{tweet['user']['name']}, #{tweet['id']}"
  end
end

# Set up HTTP.
http             = Net::HTTP.new address.host, address.port
http.use_ssl     = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

# If you entered your credentials in the first
# exercise, no need to enter them again here. The
# ||= operator will only assign these values if
# they are not already set.
consumer_key = OAuth::Consumer.new(
    "")
access_token = OAuth::Token.new(
    "")

# Issue the request.
request.oauth! http, consumer_key, access_token
http.start
response = http.request request

# Parse and print the Tweet if the response code was 200
tweets = nil
puts "Text,Date,Name,id"
if response.code == '200' then
  tweets = JSON.parse(response.body)
  print_timeline(tweets)
end
nil

How would I possibly change this code to search all of twitter for a specific word?

Upvotes: 0

Views: 2246

Answers (2)

Som Poddar
Som Poddar

Reputation: 1451

The easiest approach would be to use 'Twitter' gem. Refer to this Link for more information and the result type of the search results. Once you have all the correct authorization attribute in place (oAuth-Token,oAuth-secret, etc) you should be able to search as

Twitter.search('Obama') 

or

Twitter.search('Obama', options = {}) 

Let us know, if that worked for you or not.

p.s. - Please mark the post as answered if it helped you. Else put a comment back with what is missing.

Upvotes: 3

Kashyap
Kashyap

Reputation: 4796

The Twitter API suggests the URI your should be using for global search is https://api.twitter.com/1.1/search/tweets.json and this means:

  • Your base_url component would be https://api.twitter.com
  • Your path component would be /1.1/search/tweets.json
  • Your query component would be the text you are searching for.

The query part takes a lot of values depending upon the API spec. Refer to the specification and you can change it as per your requirement.

Tip: Try to use irb (I'd recommend pry) REPL which makes it a lot easier to explore APIs. Also, checkout the Faraday gem which can be easier to use than the default HTTP library in Ruby IMO.

Upvotes: 0

Related Questions