Skip to content

Instantly share code, notes, and snippets.

@reddragon
Last active February 20, 2017 09:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddragon/4634169 to your computer and use it in GitHub Desktop.
Save reddragon/4634169 to your computer and use it in GitHub Desktop.
Getting Lat/Long Coordinates of my Twitter friends' locations
import urllib
import simplejson as json
def get_followers_url(handle):
return 'https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=' + handle
def get_followers(handle):
return json.load(urllib.urlopen(get_followers_url(handle)))['ids']
def get_friends_url(handle):
return 'https://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=' + handle
def get_friends(handle):
return json.load(urllib.urlopen(get_friends_url(handle)))['ids']
def get_peeps_id_list(handle):
return list(set(get_followers(handle)) | set(get_friends(handle)))
def get_user_info_url():
return 'https://api.twitter.com/1/users/lookup.json'
def get_peeps(handle):
peeps_id_list = get_peeps_id_list(handle)
peeps = []
start = 0
end = len(get_peeps_id_list(handle))
while start < end:
upto = end if (end - start + 1 <= 100) else start + 100
plist_cur_batch = peeps_id_list[start:upto]
cs_plist = reduce(lambda x, y: str(x) + "," + str(y), plist_cur_batch)
params = urllib.urlencode({'user_id' : cs_plist})
peeps += json.load(urllib.urlopen(get_user_info_url(), params))
start = upto
return peeps
def get_lat_long(addr):
try:
geo_url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + addr + '&sensor=false'
resultset = json.load(urllib.urlopen(geo_url))['results']
if resultset != []:
print "%f,%f" % (resultset[0]['geometry']['location']['lat'],resultset[0]['geometry']['location']['lng'])
except:
return
def get_names_and_locations_of_peeps(handle):
print 'lat,long'
peeps = get_peeps(handle)
peeps_list = []
for peep in peeps:
peep_dict = {}
peep_dict['name'] = peep['name']
peep_dict['location_name'] = peep['location']
# peep_dict['location_coords'] = get_location_coords(peep_dict['location_name'])
peeps_list.append(peep_dict)
# print peep_dict
get_lat_long(peep_dict['location_name'])
handle = 'gawruff'
get_names_and_locations_of_peeps(handle)
@reddragon
Copy link
Author

Writes lat-long pairs in CSV format to stdout. Redirect to a file if required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment