Alpha-Tango
Alpha-Tango

Reputation: 610

How to filter location based tweets in ruby?

I'm using a simple bot that favorite tweets based on keywords. I'd like to only favorite tweets from a specific location. Any idea how to do this in ruby? If not possible in ruby I'm open to other languages. Thanks

My code:

require 'twitter' #gem install twitter

while true
begin
    config = {
        consumer_key: 
        consumer_secret:
        access_token: 
        access_token_secret:
    }

    rClient = Twitter::REST::Client.new config
    sClient = Twitter::Streaming::Client.new(config)

    # topics to watch
    topics = ['star wars']
    sClient.filter(:track => topics.join(',')) do |tweet|
        if tweet.is_a?(Twitter::Tweet)
          puts tweet.text 
          rClient.fav tweet
        end
    end
rescue Exception => e
    puts 'error, waiting for 5s: ' + e.class.to_s
    sleep 5
end
end

I tried to add these lines but it doesn't work (for NYC lat/long):

    @Geo = Twitter::Geo.new(coordinates: [22.2798024,114.149884])
    sClient.filter(:track => topics.join(','), :Geo => Tweet.Geo('40.7033127,-73.979681')) do |tweet|

Upvotes: 0

Views: 465

Answers (1)

ams
ams

Reputation: 75

To filter based on locations you need the location parameter in the hash.

So, this is how your filter function call should look like

sClient.filter(:track => topics.join(','), :locations => "40.7033127,-73.979681") do |tweet|

Thats it and you are good to go. I found it in documentation and in this example.

Upvotes: 1

Related Questions