get city not country list
This commit is contained in:
parent
6217fb774f
commit
5cde8977ef
2 changed files with 13 additions and 7 deletions
|
@ -394,8 +394,7 @@ def update_static():
|
||||||
def update_geoip(force=False):
|
def update_geoip(force=False):
|
||||||
path = os.path.join(settings.GEOIP_PATH, 'GeoLite2-City.mmdb')
|
path = os.path.join(settings.GEOIP_PATH, 'GeoLite2-City.mmdb')
|
||||||
if not os.path.exists(path) or force:
|
if not os.path.exists(path) or force:
|
||||||
url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz'
|
index = ox.net.read_url('https://db-ip.com/db/download/ip-to-city-lite').decode()
|
||||||
index = ox.net.read_url('https://db-ip.com/db/download/ip-to-country-lite').decode()
|
|
||||||
match = re.compile('href=[\'"](http.*.mmdb.gz)').findall(index)
|
match = re.compile('href=[\'"](http.*.mmdb.gz)').findall(index)
|
||||||
if match:
|
if match:
|
||||||
url = match[0]
|
url = match[0]
|
||||||
|
@ -405,7 +404,7 @@ def update_geoip(force=False):
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
os.system('gunzip "%s.gz"' % path)
|
os.system('gunzip "%s.gz"' % path)
|
||||||
else:
|
else:
|
||||||
print('failed to download dbip-country-lite-2020-03.mmdb.gz')
|
print('failed to download GeoLite2-City.mmdb')
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
if not settings.RELOADER_RUNNING:
|
if not settings.RELOADER_RUNNING:
|
||||||
|
|
|
@ -17,15 +17,22 @@ def get_location(ip):
|
||||||
country = city = None
|
country = city = None
|
||||||
try:
|
try:
|
||||||
g = GeoIP2()
|
g = GeoIP2()
|
||||||
location = g.city(ip)
|
except:
|
||||||
|
country = city = None
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
location = g.city(ip)
|
||||||
|
except django.contrib.gis.geoip2.GeoIP2Exception:
|
||||||
|
try:
|
||||||
|
location = g.country(s.ip)
|
||||||
|
except:
|
||||||
|
location = None
|
||||||
if location:
|
if location:
|
||||||
country = ox.get_country_name(location['country_code'])
|
country = ox.get_country_name(location['country_code'])
|
||||||
if location['city']:
|
if location.get('city'):
|
||||||
city = location['city']
|
city = location['city']
|
||||||
if isinstance(city, bytes):
|
if isinstance(city, bytes):
|
||||||
city = city.decode('latin-1')
|
city = city.decode('latin-1')
|
||||||
except:
|
|
||||||
country = city = None
|
|
||||||
return city, country
|
return city, country
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue