diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index 3e503579..3695f215 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -1178,20 +1178,25 @@

- Enter your MaxMind License Key. You can register for a license key here. + Enter your MaxMind License Key to install the GeoLite2 database. You can register for a license key here.

- ${docker_msg | n} + ${docker_msg | n}
-
- -
- - +
+
+ + + + +
+

+ The GeoLite2 database is used to geolocate IP addresses. Database last updated never. +

@@ -2818,24 +2823,43 @@ $(document).ready(function() { openPlexXML('/api/resources', true, {includeHttps: 1}); }); - if ("${kwargs.get('install_geoip')}" == 'true') { + if ("${kwargs.get('install_geoip')}" === 'true') { gotoSetting('notifications', 'geoip_db') } + if ("${config['geoip_db_installed']}" > "0") { + $("#geoip_db_updated").text(moment("${config['geoip_db_installed']}", "X").fromNow()); + } + $("#install_geoip_db").click(function () { var msg = 'Are you sure you want to install the GeoLite2 database?

' + 'The database is used to lookup IP address geolocation info.
' + 'The database will be downloaded from MaxMind,
' + - 'and requires 100MB of free space to install in your Tautulli directory.
' + 'and requires 100MB of free space to install in your Tautulli directory.
'; var url = 'install_geoip_db'; - confirmAjaxCall(url, msg, null, 'Installing GeoLite2 database.', function () {$('#install_geoip_db').text('Update');}); + if ($(this).text() === 'Update') { + url += '?update=true'; + } + confirmAjaxCall(url, msg, null, 'Installing GeoLite2 database.', function (result) { + if (result.result === "success") { + $('#install_geoip_db').text('Update'); + $('#geoip_db_updated').text(moment(result.updated, "X").fromNow()); + } + getSchedulerTable(); + }); }); $("#uninstall_geoip_db").click(function () { var msg = 'Are you sure you want to uninstall the GeoLite2 database?

' + 'You will not be able to lookup IP address geolocation info.'; var url = 'uninstall_geoip_db'; - confirmAjaxCall(url, msg, null, 'Uninstalling GeoLite2 database.', function () {$('#install_geoip_db').text('Install');}); + confirmAjaxCall(url, msg, null, 'Uninstalling GeoLite2 database.', function (result) { + if (result.result === "success") { + $('#install_geoip_db').text('Install'); + $('#geoip_db_updated').text('never'); + } + getSchedulerTable(); + }); }); }); diff --git a/plexpy/__init__.py b/plexpy/__init__.py index c2e47ef7..70eaa19b 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -39,6 +39,7 @@ import activity_pinger import common import database import datafactory +import helpers import libraries import logger import mobile_app @@ -444,6 +445,8 @@ def initialize_scheduler(): hours=backup_hours, minutes=0, seconds=0, args=(True, True)) schedule_job(config.make_backup, 'Backup Tautulli config', hours=backup_hours, minutes=0, seconds=0, args=(True, True)) + schedule_job(helpers.update_geoip_db, 'Update GeoLite2 database', + hours=12 * bool(CONFIG.GEOIP_DB_INSTALLED > 1), minutes=0, seconds=0) if WS_CONNECTED and CONFIG.PMS_IP and CONFIG.PMS_TOKEN: schedule_job(plextv.get_server_resources, 'Refresh Plex server URLs', diff --git a/plexpy/common.py b/plexpy/common.py index fcd80536..cc4ca6a0 100644 --- a/plexpy/common.py +++ b/plexpy/common.py @@ -197,7 +197,8 @@ SCHEDULER_LIST = [ 'Refresh libraries list', 'Refresh Plex server URLs', 'Backup Tautulli database', - 'Backup Tautulli config' + 'Backup Tautulli config', + 'Update GeoLite2 database' ] DATE_TIME_FORMATS = [ diff --git a/plexpy/helpers.py b/plexpy/helpers.py index 0d7be965..099be9c4 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -584,7 +584,16 @@ def is_valid_ip(address): return False -def install_geoip_db(): +def update_geoip_db(): + if plexpy.CONFIG.GEOIP_DB_INSTALLED > 1: + logger.info(u"Tautulli Helpers :: Checking for GeoLite2 database updates.") + now = int(time.time()) + if now - plexpy.CONFIG.GEOIP_DB_INSTALLED >= 2592000: # 30 days + return install_geoip_db(update=True) + logger.info(u"Tautulli Helpers :: GeoLite2 database already updated within the last 30 days.") + + +def install_geoip_db(update=False): if not plexpy.CONFIG.MAXMIND_LICENSE_KEY: logger.error(u"Tautulli Helpers :: Failed to download GeoLite2 database file from MaxMind: Missing MaxMindLicense Key") return False @@ -655,25 +664,33 @@ def install_geoip_db(): except Exception as e: logger.warn(u"Tautulli Helpers :: Failed to remove temporary GeoLite2 gzip file: %s" % e) - logger.debug(u"Tautulli Helpers :: GeoLite2 database installed successfully.") plexpy.CONFIG.__setattr__('GEOIP_DB', geolite2_db_path) plexpy.CONFIG.__setattr__('GEOIP_DB_INSTALLED', int(time.time())) plexpy.CONFIG.write() - return True + logger.debug(u"Tautulli Helpers :: GeoLite2 database installed successfully.") + + if not update: + plexpy.schedule_job(update_geoip_db, 'Update GeoLite2 database', hours=12, minutes=0, seconds=0) + + return plexpy.CONFIG.GEOIP_DB_INSTALLED def uninstall_geoip_db(): logger.debug(u"Tautulli Helpers :: Uninstalling the GeoLite2 database...") try: os.remove(plexpy.CONFIG.GEOIP_DB) - plexpy.CONFIG.__setattr__('GEOIP_DB_INSTALLED', 0) - plexpy.CONFIG.write() except Exception as e: logger.error(u"Tautulli Helpers :: Failed to uninstall the GeoLite2 database: %s" % e) return False + plexpy.CONFIG.__setattr__('GEOIP_DB_INSTALLED', 0) + plexpy.CONFIG.write() + logger.debug(u"Tautulli Helpers :: GeoLite2 database uninstalled successfully.") + + plexpy.schedule_job(update_geoip_db, 'Update GeoLite2 database', hours=0, minutes=0, seconds=0) + return True diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 272da3bd..427b1025 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -2821,6 +2821,7 @@ class WebInterface(object): "newsletter_custom_dir": plexpy.CONFIG.NEWSLETTER_CUSTOM_DIR, "win_sys_tray": checked(plexpy.CONFIG.WIN_SYS_TRAY), "geoip_db": plexpy.CONFIG.GEOIP_DB, + "geoip_db_installed": plexpy.CONFIG.GEOIP_DB_INSTALLED, "maxmind_license_key": plexpy.CONFIG.MAXMIND_LICENSE_KEY } @@ -3057,15 +3058,17 @@ class WebInterface(object): @cherrypy.tools.json_out() @requireAuth(member_of("admin")) @addtoapi() - def install_geoip_db(self, **kwargs): + def install_geoip_db(self, update=False, **kwargs): """ Downloads and installs the GeoLite2 database """ - result = helpers.install_geoip_db() + update = True if update == 'true' else False + + result = helpers.install_geoip_db(update=update) if result: - return {'result': 'success', 'message': 'GeoLite2 database installed successful.'} + return {'result': 'success', 'message': 'GeoLite2 database installed successful.', 'updated': result} else: - return {'result': 'error', 'message': 'GeoLite2 database install failed.'} + return {'result': 'error', 'message': 'GeoLite2 database install failed.', 'updated': 0} @cherrypy.expose @cherrypy.tools.json_out()