From 9b11fd4f182ced3a5c8d079c33c4002347e76bad Mon Sep 17 00:00:00 2001
From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com>
Date: Mon, 27 Apr 2020 17:43:00 -0700
Subject: [PATCH] Remove all MaxMind/GeoLite2 settings
---
API.md | 10 +-
data/interfaces/default/ip_address_modal.html | 2 -
data/interfaces/default/settings.html | 100 +----------
plexpy/__init__.py | 2 -
plexpy/common.py | 3 +-
plexpy/config.py | 6 -
plexpy/helpers.py | 164 ------------------
plexpy/webserve.py | 38 +---
8 files changed, 5 insertions(+), 320 deletions(-)
diff --git a/API.md b/API.md
index 64ffd813..67df2254 100644
--- a/API.md
+++ b/API.md
@@ -672,7 +672,7 @@ Returns:
### get_geoip_lookup
-Get the geolocation info for an IP address. The GeoLite2 database must be installed.
+Get the geolocation info for an IP address.
```
Required parameters:
@@ -2571,10 +2571,6 @@ Returns:
```
-### install_geoip_db
-Downloads and installs the GeoLite2 database
-
-
### notify
Send a notification using Tautulli.
@@ -2851,10 +2847,6 @@ Returns:
```
-### uninstall_geoip_db
-Uninstalls the GeoLite2 database
-
-
### update
Update Tautulli.
diff --git a/data/interfaces/default/ip_address_modal.html b/data/interfaces/default/ip_address_modal.html
index ed0e5eb8..2874cf45 100644
--- a/data/interfaces/default/ip_address_modal.html
+++ b/data/interfaces/default/ip_address_modal.html
@@ -59,8 +59,6 @@
diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html
index 281dc49d..20fe51a6 100644
--- a/data/interfaces/default/settings.html
+++ b/data/interfaces/default/settings.html
@@ -1268,7 +1268,7 @@
Enable to lookup links to MusicBrainz for music when available.
@@ -2942,56 +2894,6 @@ $(document).ready(function() {
$('#resources-xml').on('tripleclick', function () {
openPlexXML('/api/resources', true, {includeHttps: 1});
});
-
- if ("${kwargs.get('install_geoip')}" === 'true') {
- gotoSetting('3rd_party_apis', '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 maxmind_license_key = $("#maxmind_license_key");
- maxmind_license_key.val($.trim(maxmind_license_key.val()));
- if (maxmind_license_key.val() === "") {
- maxmind_license_key.focus();
- showMsg(' Maxmind License Key is required.', false, true, 5000, true);
- return false;
- } else if (!(saveSettings())) {
- return false;
- }
- 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.
';
- var url = 'install_geoip_db';
- 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');
- $('#uninstall_geoip_db').prop('disabled', false);
- $('#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 (result) {
- if (result.result === "success") {
- $('#install_geoip_db').text('Install');
- $('#uninstall_geoip_db').prop('disabled', true);
- $('#geoip_db_updated').text('never');
- }
- getSchedulerTable();
- });
- });
});
%def>
diff --git a/plexpy/__init__.py b/plexpy/__init__.py
index 0b5e3735..2cbba532 100644
--- a/plexpy/__init__.py
+++ b/plexpy/__init__.py
@@ -450,8 +450,6 @@ 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), 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 f897e584..cf8d836c 100644
--- a/plexpy/common.py
+++ b/plexpy/common.py
@@ -214,8 +214,7 @@ SCHEDULER_LIST = [
'Refresh libraries list',
'Refresh Plex server URLs',
'Backup Tautulli database',
- 'Backup Tautulli config',
- 'Update GeoLite2 database'
+ 'Backup Tautulli config'
]
DATE_TIME_FORMATS = [
diff --git a/plexpy/config.py b/plexpy/config.py
index 704bb171..ae6c965b 100644
--- a/plexpy/config.py
+++ b/plexpy/config.py
@@ -175,9 +175,6 @@ _CONFIG_DEFINITIONS = {
'FACEBOOK_ON_NEWDEVICE': (int, 'Facebook', 0),
'FIRST_RUN_COMPLETE': (int, 'General', 0),
'FREEZE_DB': (int, 'General', 0),
- 'GEOIP_DB': (str, 'General', ''),
- 'GEOIP_DB_INSTALLED': (int, 'General', 0),
- 'GEOIP_DB_UPDATE_DAYS': (int, 'General', 30),
'GET_FILE_SIZES': (int, 'General', 0),
'GET_FILE_SIZES_HOLD': (dict, 'General', {'section_ids': [], 'rating_keys': []}),
'GIT_BRANCH': (str, 'General', 'master'),
@@ -292,7 +289,6 @@ _CONFIG_DEFINITIONS = {
'LOG_BLACKLIST': (int, 'General', 1),
'LOG_DIR': (str, 'General', ''),
'LOGGING_IGNORE_INTERVAL': (int, 'Monitoring', 120),
- 'MAXMIND_LICENSE_KEY': (str, 'General', ''),
'METADATA_CACHE_SECONDS': (int, 'Advanced', 1800),
'MOVIE_LOGGING_ENABLE': (int, 'Monitoring', 1),
'MOVIE_NOTIFY_ENABLE': (int, 'Monitoring', 0),
@@ -932,8 +928,6 @@ class Config(object):
self.CONFIG_VERSION = 13
if self.CONFIG_VERSION == 13:
- if not self.GEOIP_DB:
- self.GEOIP_DB = os.path.join(plexpy.DATA_DIR, 'GeoLite2-City.mmdb')
self.CONFIG_VERSION = 14
diff --git a/plexpy/helpers.py b/plexpy/helpers.py
index a2adb592..33b4f3ef 100644
--- a/plexpy/helpers.py
+++ b/plexpy/helpers.py
@@ -15,14 +15,12 @@
import arrow
import base64
-import certifi
import cloudinary
from cloudinary.api import delete_resources_by_tag
from cloudinary.uploader import upload
from cloudinary.utils import cloudinary_url
import datetime
from functools import wraps
-import geoip2.database, geoip2.errors
import hashlib
import imghdr
from itertools import izip_longest
@@ -30,19 +28,15 @@ import ipwhois, ipwhois.exceptions, ipwhois.utils
from IPy import IP
import json
import math
-import maxminddb
from operator import itemgetter
import os
import re
import shlex
-import shutil
import socket
import sys
-import tarfile
import time
import unicodedata
import urllib
-import urllib3
from xml.dom import minidom
import xmltodict
@@ -607,164 +601,6 @@ def is_valid_ip(address):
return False
-def update_geoip_db():
- if plexpy.CONFIG.GEOIP_DB_INSTALLED:
- logger.info(u"Tautulli Helpers :: Checking for GeoLite2 database updates.")
- now = int(time.time())
- if now - plexpy.CONFIG.GEOIP_DB_INSTALLED >= plexpy.CONFIG.GEOIP_DB_UPDATE_DAYS * 24 * 60 * 60:
- return install_geoip_db(update=True)
- logger.info(u"Tautulli Helpers :: GeoLite2 database already updated within the last %s days."
- % plexpy.CONFIG.GEOIP_DB_UPDATE_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
-
- maxmind_db = 'GeoLite2-City'
- maxmind_url = 'https://download.maxmind.com/app/geoip_download?edition_id={db}&suffix={{suffix}}&license_key={key}'.format(
- db=maxmind_db, key=plexpy.CONFIG.MAXMIND_LICENSE_KEY)
- geolite2_db_url = maxmind_url.format(suffix='tar.gz')
- geolite2_md5_url = maxmind_url.format(suffix='tar.gz.md5')
- geolite2_gz = maxmind_db + '.tar.gz'
- geolite2_md5 = geolite2_gz + '.md5'
- geolite2_db = maxmind_db + '.mmdb'
- geolite2_db_path = plexpy.CONFIG.GEOIP_DB or os.path.join(plexpy.DATA_DIR, geolite2_db)
-
- # Check path ends with .mmdb
- if os.path.splitext(geolite2_db_path)[1] != os.path.splitext(geolite2_db)[1]:
- geolite2_db_path = os.path.join(geolite2_db_path, geolite2_db)
-
- temp_gz = os.path.join(plexpy.CONFIG.CACHE_DIR, geolite2_gz)
- temp_md5 = os.path.join(plexpy.CONFIG.CACHE_DIR, geolite2_md5)
-
- # Retrieve the GeoLite2 gzip file
- logger.debug(u"Tautulli Helpers :: Downloading GeoLite2 gzip file from MaxMind...")
- try:
- maxmind = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
- with maxmind.request('GET', geolite2_db_url, preload_content=False) as r_db, open(temp_gz, 'wb') as f_db:
- shutil.copyfileobj(r_db, f_db)
- with maxmind.request('GET', geolite2_md5_url, preload_content=False) as r_md5, open(temp_md5, 'wb') as f_md5:
- shutil.copyfileobj(r_md5, f_md5)
- except Exception as e:
- logger.error(u"Tautulli Helpers :: Failed to download GeoLite2 gzip file from MaxMind: %s" % e)
- return False
-
- # Check MD5 hash for GeoLite2 tar.gz file
- logger.debug(u"Tautulli Helpers :: Checking MD5 checksum for GeoLite2 gzip file...")
- try:
- hash_md5 = hashlib.md5()
- with open(temp_gz, 'rb') as f:
- for chunk in iter(lambda: f.read(4096), b""):
- hash_md5.update(chunk)
- md5_hash = hash_md5.hexdigest()
-
- with open(temp_md5, 'r') as f:
- md5_checksum = f.read()
-
- if md5_hash != md5_checksum:
- logger.error(u"Tautulli Helpers :: MD5 checksum doesn't match for GeoLite2 database. "
- "Checksum: %s, file hash: %s" % (md5_checksum, md5_hash))
- return False
- except Exception as e:
- logger.error(u"Tautulli Helpers :: Failed to generate MD5 checksum for GeoLite2 gzip file: %s" % e)
- return False
-
- # Extract the GeoLite2 database file
- logger.debug(u"Tautulli Helpers :: Extracting GeoLite2 database...")
- try:
- mmdb = None
- with tarfile.open(temp_gz, 'r:gz') as tar:
- for member in tar.getmembers():
- if geolite2_db in member.name:
- member.name = os.path.basename(member.name)
- tar.extractall(path=os.path.dirname(geolite2_db_path), members=[member])
- mmdb = True
- break
- if not mmdb:
- raise Exception("{} not found in gzip file.".format(geolite2_db))
- except Exception as e:
- logger.error(u"Tautulli Helpers :: Failed to extract the GeoLite2 database: %s" % e)
- return False
-
- # Delete temportary GeoLite2 gzip file
- logger.debug(u"Tautulli Helpers :: Deleting temporary GeoLite2 gzip file...")
- try:
- os.remove(temp_gz)
- os.remove(temp_md5)
- except Exception as e:
- logger.warn(u"Tautulli Helpers :: Failed to remove temporary GeoLite2 gzip file: %s" % e)
-
- plexpy.CONFIG.__setattr__('GEOIP_DB', geolite2_db_path)
- plexpy.CONFIG.__setattr__('GEOIP_DB_INSTALLED', int(time.time()))
- plexpy.CONFIG.write()
-
- 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)
- 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
-
-
-def geoip_lookup(ip_address):
- if not plexpy.CONFIG.GEOIP_DB_INSTALLED:
- return 'GeoLite2 database not installed. Please install from the ' \
- 'Settings 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 ' \
- 'Settings page.'
- except maxminddb.InvalidDatabaseError as e:
- return 'Invalid GeoLite2 database. Please reinstall from the ' \
- 'Settings 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
- }
-
- return geo_info
-
-
def whois_lookup(ip_address):
nets = []
diff --git a/plexpy/webserve.py b/plexpy/webserve.py
index 870c5187..addafb13 100644
--- a/plexpy/webserve.py
+++ b/plexpy/webserve.py
@@ -2942,11 +2942,7 @@ class WebInterface(object):
"newsletter_password": plexpy.CONFIG.NEWSLETTER_PASSWORD,
"newsletter_inline_styles": checked(plexpy.CONFIG.NEWSLETTER_INLINE_STYLES),
"newsletter_custom_dir": plexpy.CONFIG.NEWSLETTER_CUSTOM_DIR,
- "win_sys_tray": checked(plexpy.CONFIG.WIN_SYS_TRAY),
- "maxmind_license_key": plexpy.CONFIG.MAXMIND_LICENSE_KEY,
- "geoip_db": plexpy.CONFIG.GEOIP_DB,
- "geoip_db_installed": plexpy.CONFIG.GEOIP_DB_INSTALLED,
- "geoip_db_update_days": plexpy.CONFIG.GEOIP_DB_UPDATE_DAYS
+ "win_sys_tray": checked(plexpy.CONFIG.WIN_SYS_TRAY)
}
return serve_template(templatename="settings.html", title="Settings", config=config, kwargs=kwargs)
@@ -3178,36 +3174,6 @@ class WebInterface(object):
else:
return {'result': 'error', 'message': 'Database backup failed.'}
- @cherrypy.expose
- @cherrypy.tools.json_out()
- @requireAuth(member_of("admin"))
- @addtoapi()
- def install_geoip_db(self, update=False, **kwargs):
- """ Downloads and installs the GeoLite2 database """
-
- update = helpers.bool_true(update)
-
- result = helpers.install_geoip_db(update=update)
-
- if result:
- return {'result': 'success', 'message': 'GeoLite2 database installed successful.', 'updated': result}
- else:
- return {'result': 'error', 'message': 'GeoLite2 database install failed.', 'updated': 0}
-
- @cherrypy.expose
- @cherrypy.tools.json_out()
- @requireAuth(member_of("admin"))
- @addtoapi()
- def uninstall_geoip_db(self, **kwargs):
- """ Uninstalls the GeoLite2 database """
-
- result = helpers.uninstall_geoip_db()
-
- if result:
- return {'result': 'success', 'message': 'GeoLite2 database uninstalled successfully.'}
- else:
- return {'result': 'error', 'message': 'GeoLite2 database uninstall failed.'}
-
@cherrypy.expose
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
@@ -5725,7 +5691,7 @@ class WebInterface(object):
@requireAuth()
@addtoapi()
def get_geoip_lookup(self, ip_address='', **kwargs):
- """ Get the geolocation info for an IP address. The GeoLite2 database must be installed.
+ """ Get the geolocation info for an IP address.
```
Required parameters: