How can we get tweets from specific country
Asked Answered
G

1

15

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?

Generalization answered 13/7, 2013 at 19:14 Comment(0)
G
30

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:

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.

Gwenette answered 13/7, 2013 at 20:1 Comment(9)
is there a way to do the same thing but with python-twitter wrapper ?Generalization
Sure, I've added an example. Please, check.Gwenette
How would you include query for a specific set of hashtags in the above code?Ephemerality
Is this also doable for the stream API?Spaceless
@HammanSamuel unfortunately, it's being a while I've used twitter API, please try searching through SO. If there is not a question about it, don't hesistate to ask. Thanks!Gwenette
This will return tweets about a country. And not tweets from a country right?Dossal
@Gwenette I am also doing something like this, so tell me What is the value of place_id?Arezzini
link for geo search API is broken as of 20181206Sukkah
For some countries it does not work correclty. If I try United Kingdom or France it returns United States and Franch GuianaDeadradeadweight

© 2022 - 2024 — McMap. All rights reserved.