Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we get tweets from specific country

I've read a lot about this part and what I found is to write the geocode and search for tweets for example https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count=10

according to what i found in twitter website Returns tweets by users located within a given radius of the given latitude/longitude. A maximum of 1,000 distinct "sub-regions" will be considered when using the radius modifier. Example Values: 37.781157,-122.398720,1mi

The question!, how can we define or draw the latitude and longitude ? I've tried google map but I only get a point then i can add the miles around this point, but this is not enough, I want the whole country to be included, is that possible?

like image 927
user2490790 Avatar asked Jul 13 '13 19:07

user2490790


1 Answers

One way is to use twitter geo search API, get the place id and then perform regular search using place:place_id. Example, using tweepy:

import tweepy

auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)

api = tweepy.API(auth)
places = api.geo_search(query="USA", granularity="country")
place_id = places[0].id

tweets = api.search(q="place:%s" % place_id)
for tweet in tweets:
    print tweet.text + " | " + tweet.place.name if tweet.place else "Undefined place"

Also see these threads:

  • iOS Twitter API; How to retrieve the most recent tweets within a country?
  • How do I get top tweeps by country?

UPD (the same example using python-twitter):

from twitter import *


t = Twitter(auth=OAuth(..., ..., ..., ...))

result = t.geo.search(query="USA", granularity="country")
place_id = result['result']['places'][0]['id']

result = t.search.tweets(q="place:%s" % place_id)
for tweet in result['statuses']:
    print tweet['text'] + " | " + tweet['place']['name'] if tweet['place'] else "Undefined place"

Hope that helps.

like image 185
alecxe Avatar answered Sep 28 '22 08:09

alecxe