From 7c159e97de8c42b15d8aa568e1791e7006f41808 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Sun, 31 Jul 2016 21:14:48 -0700 Subject: [PATCH] Add Host to IP lookup --- API.md | 27 +++++++++------- data/interfaces/default/ip_address_modal.html | 32 +++++++++++-------- plexpy/helpers.py | 24 +++++++++++--- plexpy/webserve.py | 29 +++++++++-------- 4 files changed, 68 insertions(+), 44 deletions(-) diff --git a/API.md b/API.md index 9e02e5cf..b0e77e2b 100644 --- a/API.md +++ b/API.md @@ -1706,7 +1706,7 @@ Returns: ### get_whois_lookup -Get the ISP info for an IP address. +Get the connection info for an IP address. ``` Required parameters: @@ -1717,18 +1717,21 @@ Optional parameters: Returns: json: - [{"description": "Google Inc.", - "address": "1600 Amphitheatre Parkway", - "city": "Mountain View", - "state": "CA", - "postal_code": "94043", - "country": "United States", - ... - }, - {...} - ] + {"host": "google-public-dns-a.google.com", + "nets": [{"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." + {"host": "Not available", + "nets": [], + "error": "IPv4 address 127.0.0.1 is already defined as Loopback via RFC 1122, Section 3.2.1.3." } ``` diff --git a/data/interfaces/default/ip_address_modal.html b/data/interfaces/default/ip_address_modal.html index 2f7e263a..cb407305 100644 --- a/data/interfaces/default/ip_address_modal.html +++ b/data/interfaces/default/ip_address_modal.html @@ -38,9 +38,14 @@

Connection Details

+
+ +
@@ -99,13 +104,12 @@ $('#isp_error').html(' Internal request failed.').show(); }, success: function (data) { + $('#isp_host').html(data.host); if ('error' in data) { $('#isp_error').html(' ' + data.error).show(); - } else if (!(data.length)) { - $('#isp_error').html(' Connection details not found.').show(); - } else { + } else if (data.nets.length) { $('#isp_instance').remove(); - $.each(data, function (index, net) { + $.each(data.nets, function (index, net) { var s = ''; if (net.city || net.state || net.postal_code) { s = (net.city && net.state) ? net.city + ', ' + net.state : net.city || net.state || ''; @@ -113,15 +117,17 @@ } s = (s) ? '' + s + '
' : s; $('#modal-text').append('
\ - \ -
') + \ + ') }); + } else { + $('#isp_name, #isp_address').html("Not available"); } } }); diff --git a/plexpy/helpers.py b/plexpy/helpers.py index 31b1393b..07a6fb13 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -632,6 +632,7 @@ def geoip_lookup(ip_address): def whois_lookup(ip_address): nets = [] + err = None try: whois = ipwhois.IPWhois(ip_address).lookup_whois(retry_count=0) countries = ipwhois.utils.get_countries() @@ -641,15 +642,28 @@ def whois_lookup(ip_address): if net['postal_code']: net['postal_code'] = net['postal_code'].replace('-', ' ') except ValueError as e: - return 'Invalid IP address provided: %s.' % ip_address + err = 'Invalid IP address provided: %s.' % ip_address except ipwhois.exceptions.IPDefinedError as e: - return '%s' % e + err = '%s' % e except ipwhois.exceptions.ASNRegistryError as e: - return '%s' % e + err = '%s' % e except Exception as e: - return 'Error: %s' % e + err = 'Error: %s' % e - return nets + host = '' + try: + host = ipwhois.Net(ip_address).get_host(retry_count=0)[0] + except Exception as e: + host = 'Not available' + + whois_info = {"host": host, + "nets": nets + } + + if err: + whois_info['error'] = err + + return whois_info # Taken from SickRage def anon_url(*url): diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 0dfe6d5c..f039db95 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -4327,7 +4327,7 @@ class WebInterface(object): @requireAuth() @addtoapi() def get_whois_lookup(self, ip_address='', **kwargs): - """ Get the ISP info for an IP address. + """ Get the connection info for an IP address. ``` Required parameters: @@ -4338,22 +4338,23 @@ class WebInterface(object): Returns: json: - [{"description": "Google Inc.", - "address": "1600 Amphitheatre Parkway", - "city": "Mountain View", - "state": "CA", - "postal_code": "94043", - "country": "United States", - ... - }, - {...} - ] + {"host": "google-public-dns-a.google.com", + "nets": [{"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." + {"host": "Not available", + "nets": [], + "error": "IPv4 address 127.0.0.1 is already defined as Loopback via RFC 1122, Section 3.2.1.3." } ``` """ whois_info = helpers.whois_lookup(ip_address) - if isinstance(whois_info, basestring): - return {'error': whois_info} return whois_info