From 5cde8977efc07c02029b01d71396d2d498efe997 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 30 May 2020 03:13:57 +0200 Subject: [PATCH] get city not country list --- pandora/app/config.py | 5 ++--- pandora/user/utils.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pandora/app/config.py b/pandora/app/config.py index 3bb7cc72..e950e5e9 100644 --- a/pandora/app/config.py +++ b/pandora/app/config.py @@ -394,8 +394,7 @@ def update_static(): def update_geoip(force=False): path = os.path.join(settings.GEOIP_PATH, 'GeoLite2-City.mmdb') 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-country-lite').decode() + index = ox.net.read_url('https://db-ip.com/db/download/ip-to-city-lite').decode() match = re.compile('href=[\'"](http.*.mmdb.gz)').findall(index) if match: url = match[0] @@ -405,7 +404,7 @@ def update_geoip(force=False): os.unlink(path) os.system('gunzip "%s.gz"' % path) else: - print('failed to download dbip-country-lite-2020-03.mmdb.gz') + print('failed to download GeoLite2-City.mmdb') def init(): if not settings.RELOADER_RUNNING: diff --git a/pandora/user/utils.py b/pandora/user/utils.py index 2cc82f1c..808aaa69 100644 --- a/pandora/user/utils.py +++ b/pandora/user/utils.py @@ -17,15 +17,22 @@ def get_location(ip): country = city = None try: 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: country = ox.get_country_name(location['country_code']) - if location['city']: + if location.get('city'): city = location['city'] if isinstance(city, bytes): city = city.decode('latin-1') - except: - country = city = None return city, country