mirror of
https://github.com/chubin/wttr.in.git
synced 2025-08-13 15:37:03 +02:00
ip2location fixes
This commit is contained in:
@ -11,8 +11,8 @@ GEOLITE = os.path.join(MYDIR, 'data', "GeoLite2-City.mmdb")
|
|||||||
WEGO = "/home/igor/go/bin/we-lang"
|
WEGO = "/home/igor/go/bin/we-lang"
|
||||||
PYPHOON = "/home/igor/pyphoon/bin/pyphoon-lolcat"
|
PYPHOON = "/home/igor/pyphoon/bin/pyphoon-lolcat"
|
||||||
|
|
||||||
CACHEDIR = os.path.join(MYDIR, "cache")
|
CACHEDIR = os.path.join(MYDIR, "cache/wego/")
|
||||||
IP2LCACHE = os.path.join(MYDIR, "cache/ip2l")
|
IP2LCACHE = os.path.join(MYDIR, "cache/ip2l/")
|
||||||
|
|
||||||
ALIASES = os.path.join(MYDIR, "share/aliases")
|
ALIASES = os.path.join(MYDIR, "share/aliases")
|
||||||
ANSI2HTML = os.path.join(MYDIR, "share/ansi2html.sh")
|
ANSI2HTML = os.path.join(MYDIR, "share/ansi2html.sh")
|
||||||
@ -55,8 +55,9 @@ PLAIN_TEXT_AGENTS = [
|
|||||||
|
|
||||||
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation']
|
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation']
|
||||||
|
|
||||||
IP2LOCATION_KEY = ''
|
_IP2LOCATION_KEY_FILE = os.environ['HOME'] + '/.ip2location.key'
|
||||||
|
if os.path.exists(_IP2LOCATION_KEY_FILE):
|
||||||
|
IP2LOCATION_KEY = open(_IP2LOCATION_KEY_FILE, 'r').read().strip()
|
||||||
|
|
||||||
def error(text):
|
def error(text):
|
||||||
"log error `text` and raise a RuntimeError exception"
|
"log error `text` and raise a RuntimeError exception"
|
||||||
|
@ -89,21 +89,25 @@ def ip2location(ip_addr):
|
|||||||
if not os.path.exists(IP2LCACHE):
|
if not os.path.exists(IP2LCACHE):
|
||||||
os.makedirs(IP2LCACHE)
|
os.makedirs(IP2LCACHE)
|
||||||
|
|
||||||
|
location = None
|
||||||
|
|
||||||
if os.path.exists(cached):
|
if os.path.exists(cached):
|
||||||
location = open(cached, 'r').read()
|
location = open(cached, 'r').read()
|
||||||
return location
|
else:
|
||||||
|
try:
|
||||||
|
ip2location_response = requests\
|
||||||
|
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS10' \
|
||||||
|
% (ip_addr, IP2LOCATION_KEY)).text
|
||||||
|
if ';' in ip2location_response:
|
||||||
|
open(cached, 'w').write(ip2location_response)
|
||||||
|
location = ip2location_response
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
if ';' in location:
|
||||||
ip2location_response = requests\
|
location = "%s,%s" % (location.split(';')[3], location.split(';')[1])
|
||||||
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS10' \
|
|
||||||
% (IP2LOCATION_KEY, ip_addr)).text
|
return location
|
||||||
if ';' in ip2location_response:
|
|
||||||
location = ip2location_response.split(';')[3]
|
|
||||||
open(cached, 'w').write(location)
|
|
||||||
print "ip2location says: %s" % location
|
|
||||||
return location
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_location(ip_addr):
|
def get_location(ip_addr):
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user