diff --git a/data/interfaces/default/ip_address_modal.html b/data/interfaces/default/ip_address_modal.html index 928d4cad..ed0e5eb8 100644 --- a/data/interfaces/default/ip_address_modal.html +++ b/data/interfaces/default/ip_address_modal.html @@ -24,7 +24,6 @@
@@ -82,11 +80,11 @@ error: function () { $('#ip_error').html(' Internal request failed.').show(); }, - success: function (data) { - if ('error' in data) { - $('#ip_error').html(' ' + data.error).show(); + success: function (result) { + if (result.results === 'error') { + $('#ip_error').html(' ' + result.message).show(); } else { - $('#continent').html(data.continent); + var data = result.data; $('#country').html(data.country); $('#region').html(data.region); $('#city').html(data.city); @@ -94,7 +92,6 @@ $('#timezone').html(data.timezone); $('#latitude').html(data.latitude); $('#longitude').html(data.longitude); - $('#accuracy').html(data.accuracy + ' km'); } } }); diff --git a/plexpy/plextv.py b/plexpy/plextv.py index e389cf07..5d1111d4 100644 --- a/plexpy/plextv.py +++ b/plexpy/plextv.py @@ -377,6 +377,14 @@ class PlexTV(object): return request + def get_plextv_geoip(self, ip_address='', output_format=''): + uri = '/api/v2/geoip?ip_address=%s' % ip_address + request = self.request_handler.make_request(uri=uri, + request_type='GET', + output_format=output_format) + + return request + def get_full_users_list(self): own_account = self.get_plextv_user_details(output_format='xml') friends_list = self.get_plextv_friends(output_format='xml') @@ -923,3 +931,35 @@ class PlexTV(object): "user_token": helpers.get_xml_attr(a, 'authToken') } return account_details + + def get_geoip_lookup(self, ip_address=''): + if not ip_address or not helpers.is_public_ip(ip_address): + return + + geoip_data = self.get_plextv_geoip(ip_address=ip_address, output_format='xml') + + try: + xml_head = geoip_data.getElementsByTagName('location') + except Exception as e: + logger.warn(u"Tautulli PlexTV :: Unable to parse XML for get_geoip_lookup: %s." % e) + return None + + for a in xml_head: + coordinates = helpers.get_xml_attr(a, 'coordinates').split(',') + latitude = longitude = None + if len(coordinates) == 2: + latitude, longitude = [helpers.cast_to_float(c) for c in coordinates] + + geo_info = {"code": helpers.get_xml_attr(a, 'code') or None, + "country": helpers.get_xml_attr(a, 'country') or None, + "region": helpers.get_xml_attr(a, 'subdivisions') or None, + "city": helpers.get_xml_attr(a, 'city') or None, + "postal_code": helpers.get_xml_attr(a, 'postal_code') or None, + "timezone": helpers.get_xml_attr(a, 'time_zone') or None, + "latitude": latitude, + "longitude": longitude, + "continent": None, # keep for backwards compatibility with GeoLite2 + "accuracy": None # keep for backwards compatibility with GeoLite2 + } + + return geo_info diff --git a/plexpy/webserve.py b/plexpy/webserve.py index f0e4fcd3..bcd37a4d 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -5751,10 +5751,22 @@ class WebInterface(object): } ``` """ - geo_info = helpers.geoip_lookup(ip_address) - if isinstance(geo_info, basestring): - return {'error': geo_info} - return geo_info + message = '' + if not ip_address: + message = 'No IP address provided.' + elif not helpers.is_valid_ip(ip_address): + message = 'Invalid IP address provided: %s' % ip_address + elif not helpers.is_public_ip(ip_address): + message = 'Non-public IP address provided: %s' % ip_address + + if message: + return {'result': 'error', 'message': message} + + plex_tv = plextv.PlexTV() + geo_info = plex_tv.get_geoip_lookup(ip_address) + if geo_info: + return {'result': 'success', 'data': geo_info} + return {'result': 'error', 'message': 'Failed to lookup GeoIP info for address: %s' % ip_address} @cherrypy.expose @cherrypy.tools.json_out()