ttezel / twit

Twitter API Client for node (REST & Streaming API)
4.31k stars 568 forks source link

Is there a way of getting tweets by location? #535

Open webegguk opened 4 years ago

webegguk commented 4 years ago

Great package and just what I was looking for. I've trawled through the docs I could find and wondered if you could do something like:

t.get('search/tweets', { q: '"my phrase" -filter:retweets -filter:replies', count: 100, location:["United Kingdom", "UK", "England"] }, function (err, data, response) { console.log(data.statuses) })

to get only tweets matching those locations (location:["United Kingdom", "UK", "England"] bit doesn't work BTW). It doesn't seem obvious from what I've read so far.

angelsalazar-zz commented 4 years ago

the easiest way to accomplish it is to first perform an HTTP request to the geo/search endpoint in order to find out the place id and use the place id as part of your query when searching for tweets

Retrieving the place id

const T = require('twit');
const fs = require('fs');

const querParams = {
  query: 'PLACE_YOU_WANT_TO_SEARCH',
  granularity: 'city' // this can be neighborhood,city, admin, country
}

T.get('geo/search', querParams, (err, data, response) => {
  if (err) {
    console.log('error')
    console.log(err)
    return;
  }
  // save result to scan the result easily
   fs.writeFileSync('./places.json', JSON.stringify(data.result, undefined, 2), { encoding: 'utf8' });
})

Search for tweets for a specific place

const query = {
      q: '(word1 OR word2) place:THE_PLACE_ID'
 }

T.get('search/tweets', query, function(err, data, response) {
 // Your code
})
neville-bot commented 3 years ago

I used a bounded box of lon/lat and it worked fine:

const chicago = [-87.941313, 41.643179, -87.522772, 42.023758];

const stream = T.stream("statuses/filter", { count: 10, locations: chicago });