[#2861] [Core] Switch to using python-geoip for geoip lookups

* libtorrent >= 1.1 dropped support for GeoIP so this adds support
   again using MaxMind GeoIP Legacy Python Extension API.
   For reference it is known by the following package names:
       * Maxmind: geoip-api-python
       * Linux: python-geoip
       * PyPi: GeoIP
This commit is contained in:
Calum Lind 2016-10-19 12:42:57 +01:00
parent d77666cd3e
commit ca7cbd291f
5 changed files with 30 additions and 29 deletions

View File

@ -1,5 +1,5 @@
=== Core ===
* libtorrent (rasterbar) >= 0.16.7
* libtorrent (rasterbar) >= 1.0.7
* python >= 2.6
* setuptools
* twisted >= 11.1
@ -7,6 +7,7 @@
* pyxdg
* chardet
* gettext
* python-geoip (optional)
* geoip-database (optional)
* setproctitle (optional)
* pillow (optional)

View File

@ -96,6 +96,9 @@ class Core(component.Component):
self.external_ip = None
self.eventmanager.register_event_handler("ExternalIPEvent", self._on_external_ip_event)
# GeoIP instance with db loaded
self.geoip_instance = None
# Get the core config
self.config = ConfigManager("core.conf")
self.config.save()

View File

@ -21,6 +21,11 @@ import deluge.configmanager
from deluge._libtorrent import lt
from deluge.event import ConfigValueChangedEvent
try:
import GeoIP
except ImportError:
GeoIP = None
log = logging.getLogger(__name__)
DEFAULT_PREFS = {
@ -436,27 +441,19 @@ class PreferencesManager(component.Component):
log.debug("%s: %s", key, value)
self.session_set_setting("anonymous_mode", value)
def _on_set_geoip_db_location(self, key, value):
log.debug("%s: %s", key, value)
def _on_set_geoip_db_location(self, key, geoip_db):
log.debug("%s: %s", key, geoip_db)
# Load the GeoIP DB for country look-ups if available
geoip_db = ""
if os.path.exists(value):
geoip_db = value
elif os.path.exists(deluge.common.resource_filename("deluge", os.path.join("data", "GeoIP.dat"))):
geoip_db = deluge.common.resource_filename(
"deluge", os.path.join("data", "GeoIP.dat")
)
deluge_geoip_db = deluge.common.resource_filename("deluge", os.path.join("data", "GeoIP.dat"))
for geoip_path in (geoip_db, deluge_geoip_db):
if os.path.exists(geoip_path):
try:
self.core.geoip_instance = GeoIP.open(geoip_path, GeoIP.GEOIP_STANDARD)
except AttributeError:
log.warning("GeoIP Unavailable")
break
else:
log.warning("Unable to find GeoIP database file!")
if geoip_db:
try:
self.session.load_country_db(str(geoip_db))
except RuntimeError as ex:
log.error("Unable to load geoip database!")
log.exception(ex)
except AttributeError:
log.warning("GeoIP Unavailable")
log.warning("Unable to find GeoIP database file: %s")
def _on_set_cache_size(self, key, value):
log.debug("%s: %s", key, value)

View File

@ -791,14 +791,14 @@ class Torrent(object):
if peer.flags & peer.connecting or peer.flags & peer.handshake:
continue
client = decode_string(str(peer.client))
# Make country a proper string
country = str()
for char in peer.country:
if not char.isalpha():
country += " "
else:
country += char
client = decode_string(peer.client)
try:
country = component.get("Core").geoip_instance.country_code_by_addr(peer.ip[0])
except AttributeError:
country = ""
else:
country = "".join([char if char.isalpha() else " " for char in country])
ret.append({
"client": client,

View File

@ -214,7 +214,7 @@ class PeersTab(Tab):
component.get("SessionProxy").get_torrent_status(torrent_id, ["peers"]).addCallback(self._on_get_torrent_status)
def get_flag_pixbuf(self, country):
if country == " ":
if not country.strip():
return None
if country not in self.cached_flag_pixbufs: