how to configure ip2location (#296)

This commit is contained in:
Igor Chubin
2019-05-09 17:40:22 +02:00
parent 6b11f2893a
commit 8142e30e53
6 changed files with 35 additions and 14 deletions

View File

@ -95,15 +95,18 @@ def ip2location(ip_addr):
if os.path.exists(cached):
location = open(cached, 'r').read()
else:
try:
ip2location_response = requests\
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \
% (ip_addr, IP2LOCATION_KEY)).text
if ';' in ip2location_response:
open(cached, 'w').write(ip2location_response)
location = ip2location_response
except requests.exceptions.ConnectionError:
pass
# if IP2LOCATION_KEY is not set, do not the query,
# because the query wont be processed anyway
if IP2LOCATION_KEY:
try:
ip2location_response = requests\
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \
% (ip_addr, IP2LOCATION_KEY)).text
if ';' in ip2location_response:
open(cached, 'w').write(ip2location_response)
location = ip2location_response
except requests.exceptions.ConnectionError:
pass
if ';' in location:
location = location.split(';')[3], location.split(';')[1]