Async ISP lookup

This commit is contained in:
JonnyWong16 2016-07-31 17:51:40 -07:00
parent 9b067a437c
commit ba8e4ff33c
5 changed files with 149 additions and 69 deletions

View file

@ -20,8 +20,7 @@ import geoip2.database, geoip2.errors
import gzip
import hashlib
import imghdr
from ipwhois import IPWhois
from ipwhois.utils import get_countries
import ipwhois, ipwhois.exceptions, ipwhois.utils
from IPy import IP
import json
import math
@ -604,12 +603,11 @@ def geoip_lookup(ip_address):
reader = geoip2.database.Reader(plexpy.CONFIG.GEOIP_DB)
geo = reader.city(ip_address)
reader.close()
except ValueError as e:
return 'Invalid IP address provided: %s.' % ip_address
except IOError as e:
return 'Missing GeoLite2 database. Please reinstall from the ' \
'<a href="settings?install_geoip=true">Settings</a> page.'
except ValueError as e:
return 'Unable to read GeoLite2 database. Please reinstall from the ' \
'<a href="settings?reinstall_geoip=true">Settings</a> page.'
except maxminddb.InvalidDatabaseError as e:
return 'Invalid GeoLite2 database. Please reinstall from the ' \
'<a href="settings?reinstall_geoip=true">Settings</a> page.'
@ -618,17 +616,6 @@ def geoip_lookup(ip_address):
except Exception as e:
return 'Error: %s' % e
nets = []
try:
whois = IPWhois(ip_address).lookup_whois()
countries = get_countries()
nets = whois['nets']
for net in nets:
net['country'] = countries[net['country']]
net['postal_code'] = net['postal_code'].replace('-', ' ')
except Exception as e:
logger.warn(u"PlexPy Helpers :: %s." % e)
geo_info = {'continent': geo.continent.name,
'country': geo.country.name,
'region': geo.subdivisions.most_specific.name,
@ -637,12 +624,33 @@ def geoip_lookup(ip_address):
'timezone': geo.location.time_zone,
'latitude': geo.location.latitude,
'longitude': geo.location.longitude,
'accuracy': geo.location.accuracy_radius,
'nets': nets
'accuracy': geo.location.accuracy_radius
}
return geo_info
def whois_lookup(ip_address):
nets = []
try:
whois = ipwhois.IPWhois(ip_address).lookup_whois(retry_count=0)
countries = ipwhois.utils.get_countries()
nets = whois['nets']
for net in nets:
net['country'] = countries[net['country']]
if net['postal_code']:
net['postal_code'] = net['postal_code'].replace('-', ' ')
except ValueError as e:
return 'Invalid IP address provided: %s.' % ip_address
except ipwhois.exceptions.IPDefinedError as e:
return '%s' % e
except ipwhois.exceptions.ASNRegistryError as e:
return '%s' % e
except Exception as e:
return 'Error: %s' % e
return nets
# Taken from SickRage
def anon_url(*url):
"""

View file

@ -4310,18 +4310,7 @@ class WebInterface(object):
"timezone": "America/Los_Angeles",
"latitude": 37.386,
"longitude": -122.0838,
"accuracy": 1000,
"net": [{"description": "Google Inc.",
"address": "1600 Amphitheatre Parkway",
"city": "Mountain View",
"state": "CA",
"postal_code": "94043",
"country": "United States",
...
},
{...}
]
"accuracy": 1000
}
json:
{"error": "The address 127.0.0.1 is not in the database."
@ -4332,3 +4321,39 @@ class WebInterface(object):
if isinstance(geo_info, basestring):
return {'error': geo_info}
return geo_info
@cherrypy.expose
@cherrypy.tools.json_out()
@requireAuth()
@addtoapi()
def get_whois_lookup(self, ip_address='', **kwargs):
""" Get the ISP info for an IP address.
```
Required parameters:
ip_address
Optional parameters:
None
Returns:
json:
[{"description": "Google Inc.",
"address": "1600 Amphitheatre Parkway",
"city": "Mountain View",
"state": "CA",
"postal_code": "94043",
"country": "United States",
...
},
{...}
]
json:
{"error": "The address 127.0.0.1 is not in the database."
}
```
"""
whois_info = helpers.whois_lookup(ip_address)
if isinstance(whois_info, basestring):
return {'error': whois_info}
return whois_info