Skip to content

Instantly share code, notes, and snippets.

@eirenik0
Last active December 19, 2015 03:19
Show Gist options
  • Save eirenik0/5889126 to your computer and use it in GitHub Desktop.
Save eirenik0/5889126 to your computer and use it in GitHub Desktop.
From friend-on-map
IP_URL = "http://api.hostip.info/?ip="
'
def location(self, user):
"""
:param user: user id(uid)
:return: address from ndb, coordinates from IP
"""
address = '%s, %s' % (user.country, user.city)
coords = self.get_coords(self.request.remote_addr) # receive location from id
if coords == None:
coords = "36.315125,-27.802738"
return address, coords
return address, coords
def get_coords(self, ip):
url = IP_URL + ip
content = None
try:
content = urllib2.urlopen(url).read()
except URLError:
return
if content:
d = minidom.parseString(content)
coords = d.getElementsByTagName("gml:coordinates")
if coords and coords[0].childNodes[0].nodeValue:
lon, lat = coords[0].childNodes[0].nodeValue.split(',')
return lon, lat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment