BadProgrammer
BadProgrammer

Reputation: 371

Tweepy Search by a Country and not with coordinates

Is it possible to filter a Tweepy Search by a country code? I know it's possible to search by giving a countries coordinates and radius. However, most countries are not defined well by a circle. It seems there should be a way because you can access the tweets country code after the search is over.

Upvotes: 1

Views: 6176

Answers (2)

Yogesh
Yogesh

Reputation: 1432

This can be method to fetch from a particular country.

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)

Upvotes: 2

daemonsl
daemonsl

Reputation: 452

I worked on tweepy for a while to track down tweets from all countries. Refer to this : Streaming Parameters. What you can do if u want to track tweets from a single county is as follows:

  • Track all tweets from approximate bounding boxes of the coordinates surrounding the country (polygon coordinates).
  • If you want to further filter the particular country tweets because above method will give you some noisy tweets from neighbouring country, you can check each tweets which have geolocation parameter TRUE and check for Country Code you are looking for but that filters down ur tweets to 1%. Because approx. 1% of tweets are geotagged(people GPS info tweets).
  • So, finally you can choose next parameter to filter is using "users" "location" key, that is manually entered and can use geocoder APIs like geopy to resolve that string value to find location and extract country name.

I guess this will help you in narrowing down tweets to your target country. Below is geopy code to extract country code from string value (like "New york", "brisbane" etc).

from geopy.geocoders import Yandex
import json
geolocator = Yandex(lang='en_US')

location = geolocator.geocode("Paris", timeout=10)

if location != None:
    print json.dumps(location.raw, indent=4)
    print location.address
    print location.latitude, " -> ", location.longitude
else:
    print location

Refer to Geopy Documentation. And for further assistance My Project Presentation.

Upvotes: 4

Related Questions