Last active
December 19, 2015 03:19
-
-
Save eirenik0/5889126 to your computer and use it in GitHub Desktop.
From friend-on-map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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