Search by location in Twitter using nodejs

534 views Asked by At

I am trying to search tweets based on location

I have written the below code for search by hashtag

Twitter.get('search/tweets', {  q: '#javascript since:7 days', count: 100 }, function (err, data, response) {
      if(!err) { res.send(data.statuses); }
      })

In the same way I am trying to search by location in the below data. I didn't find the proper keyword to search by location.

 { statuses:
  [ { created_at: 'Mon Aug 19 03:01:56 +0000 2019',
   id: 1163284936125759500,
   id_str: '1163284936125759490',
   text:
    'THE <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7019343f3c3d3023243522">',
   truncated: true,
   entities: [Object],
   metadata: [Object],
   source:
    '<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>',
   in_reply_to_status_id: null,
   in_reply_to_status_id_str: null,
   in_reply_to_user_id: null,
   in_reply_to_user_id_str: null,
   in_reply_to_screen_name: null,
   user: [Object],
   geo: null,
   coordinates: null,
   place: null,
   contributors: null,
   is_quote_status: false,
   retweet_count: 0,
   favorite_count: 1,
   favorited: false,
   retweeted: false,
   lang: 'en' }]}

How can we search. Please help

1

There are 1 answers

0
Terry Lennox On

The Twitter API lets you specify a geocode parameter to search for tweets by users located with a given radius of the given latitude / longitude. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either " mi " (miles) or " km " (kilometers).

There's more detail in the Twitter API docs

// Look for tweets from users within 100km of London
let loc = "51.5074,0.1278,100km";
Twitter.get('search/tweets', {  q: '#javascript since:7 days', geocode: loc, count: 10 }, function (err, data, response) {
    if(!err) { res.send(data.statuses); }
})

You can also look at the user.location property of the tweet object returned (e.g. tweet.user.location).