Skip to content

Instantly share code, notes, and snippets.

@reddragon
Created August 20, 2012 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddragon/3400704 to your computer and use it in GitHub Desktop.
Save reddragon/3400704 to your computer and use it in GitHub Desktop.
Get Lat/Long coords of my Twitter Friends and Followers
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_yahoo_api_id():
return 'LXRODM62'
def get_geo_api_url(location_name):
return 'http://where.yahooapis.com/geocode?appid=' + get_yahoo_api_id() + '&flags=J&q=' + location_name.encode('ascii', 'ignore');
def get_location_coords(location_name):
location_coords = {}
if location_name == "":
return location_coords
resultset = json.load(urllib.urlopen(get_geo_api_url(location_name)))['ResultSet'];
if 'Results' not in resultset:
return location_coords
location_coords['lat'] = resultset['Results'][0]['latitude']
location_coords['lon'] = resultset['Results'][0]['longitude']
return location_coords
def get_names_and_locations_of_peeps(handle):
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
return peeps_list
handle = 'Gaurav_Menghani'
get_names_and_locations_of_peeps(handle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment