diff --git a/DEPENDS b/DEPENDS index 56076132c..b467900d7 100644 --- a/DEPENDS +++ b/DEPENDS @@ -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) diff --git a/deluge/core/core.py b/deluge/core/core.py index 3c0a2a896..7499698b0 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -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() diff --git a/deluge/core/preferencesmanager.py b/deluge/core/preferencesmanager.py index 4372bd4ac..d7aa3e8aa 100644 --- a/deluge/core/preferencesmanager.py +++ b/deluge/core/preferencesmanager.py @@ -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) diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index 7fa3d0f0b..93e67c97b 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -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, diff --git a/deluge/ui/gtkui/peers_tab.py b/deluge/ui/gtkui/peers_tab.py index 943254126..82ea2e1bf 100644 --- a/deluge/ui/gtkui/peers_tab.py +++ b/deluge/ui/gtkui/peers_tab.py @@ -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: