Python function to find the country of origin of a Tweet
August 2, 2014 Leave a comment
This is a rudimentary function to trace the origin from the tweet’s JSON structure. I am using a dictionary of states and abbreviations.
def checklocation(self,data):
location = data.get('user').get('location')
place = data.get('place')
if place:
country = data.get('place').get('country')
full_name = data.get('place').get('full_name')
for c in self.states.itervalues():
p = re.compile(c.lower(), re.IGNORECASE)
if country:
m = p.match(country.lower())
if m:
return m.group()
if full_name:
m = p.match(full_name.lower())
if m:
return m.group()
if location:
m = p.match(location.lower())
if m:
return m.group()