twit icon indicating copy to clipboard operation
twit copied to clipboard

Is there a way of getting tweets by location?

Open webegguk opened this issue 4 years ago • 2 comments

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.

webegguk avatar Jun 10 '20 00:06 webegguk

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
})

angelsalazar-zz avatar Sep 26 '20 03:09 angelsalazar-zz

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 });

neville-bot avatar May 08 '21 05:05 neville-bot