Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return 'GeoLite2 database not installed. Please install from the ' \
'<a href="settings?install_geoip=true">Settings</a> page.'
if not ip_address:
return 'No IP address provided.'
try:
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 maxminddb.InvalidDatabaseError as e:
return 'Invalid GeoLite2 database. Please reinstall from the ' \
'<a href="settings?reinstall_geoip=true">Settings</a> page.'
except geoip2.errors.AddressNotFoundError as e:
return '%s' % e
except Exception as e:
return 'Error: %s' % e
geo_info = {'continent': geo.continent.name,
'country': geo.country.name,
'region': geo.subdivisions.most_specific.name,
'city': geo.city.name,
'postal_code': geo.postal.code,
'timezone': geo.location.time_zone,
'latitude': geo.location.latitude,
'longitude': geo.location.longitude,
'accuracy': geo.location.accuracy_radius
"""
if not self.enabled:
return None
self._prune_lookup_cache()
cache = self._lookup_cache
country, timestamp = cache.get(addr, (None, 0))
now = time.time()
if country is not None and now - timestamp < self.max_cache_age:
return country
db = self._db
if db is not None:
try:
info = db.get(addr)
except maxminddb.InvalidDatabaseError as e:
log.debug('Invalid database: %r', e)
asyncio.ensure_future(self.load(force_update=True, ignore_errors=True))
except UnicodeDecodeError as e:
log.debug('Caught UnicodeDecodeError with address %r: %r', addr, e)
asyncio.ensure_future(self.load(force_update=True, ignore_errors=True))
else:
if isinstance(info, dict):
country = info.get('country')
if isinstance(country, dict):
iso_code = country.get('iso_code')
cache[addr] = (iso_code, now)
return iso_code
else:
log.debug('"country" key maps to non-dict: %r', country)
asyncio.ensure_future(self.load(force_update=True, ignore_errors=True))
else: