diff --git a/DEPENDS b/DEPENDS index 49083335a..093f5ddf6 100644 --- a/DEPENDS +++ b/DEPENDS @@ -12,6 +12,7 @@ * geoip-database (optional) * setproctitle (optional) * pillow (optional) + * python-geoip (optional) * libtorrent (rasterbar) >= 0.14 diff --git a/deluge/core/core.py b/deluge/core/core.py index 84db25eee..def2aa70b 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -138,6 +138,9 @@ class Core(component.Component): # New release check information self.new_release = None + # GeoIP instance with db loaded + self.geoip_instance = None + # Get the core config self.config = deluge.configmanager.ConfigManager("core.conf") diff --git a/deluge/core/preferencesmanager.py b/deluge/core/preferencesmanager.py index e921f6a48..4d73cf2d2 100644 --- a/deluge/core/preferencesmanager.py +++ b/deluge/core/preferencesmanager.py @@ -49,6 +49,12 @@ import deluge.common import deluge.component as component from deluge.log import LOG as log +try: + import GeoIP +except ImportError: + GeoIP = None + + DEFAULT_PREFS = { "send_info": False, "info_sent": 0.0, @@ -489,23 +495,19 @@ class PreferencesManager(component.Component): def _on_geoip_db_location(self, key, value): log.debug("%s: %s", key, value) # Load the GeoIP DB for country look-ups if available - geoip_db = "" if os.path.exists(value): - geoip_db = value - elif os.path.exists(pkg_resources.resource_filename("deluge", os.path.join("data", "GeoIP.dat"))): - geoip_db = pkg_resources.resource_filename("deluge", os.path.join("data", "GeoIP.dat")) + try: + self.core.geoip_instance = GeoIP.open(value, GeoIP.GEOIP_STANDARD) + except AttributeError: + try: + self.session.load_country_db(value) + except RuntimeError, ex: + log.error("Unable to load geoip database: %s", ex) + except AttributeError: + log.warning("GeoIP Unavailable") else: log.warning("Unable to find GeoIP database file!") - if geoip_db: - try: - self.session.load_country_db(str(geoip_db)) - except RuntimeError, e: - log.error("Unable to load geoip database!") - log.exception(e) - except AttributeError: - log.warning("GeoIP Unavailable") - def _on_cache_size(self, key, value): log.debug("%s: %s", key, value) self.session_set_setting("cache_size", value) diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index 9410901be..bf36e3e6a 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -556,13 +556,12 @@ class Torrent(object): except UnicodeDecodeError: client = str(peer.client).decode("latin-1") - # Make country a proper string - country = str() - for c in peer.country: - if not c.isalpha(): - country += " " - else: - country += c + try: + country = component.get("Core").geoip_instance.country_code_by_addr(peer.ip[0]) + except AttributeError: + country = peer.country + + 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 1df379e90..7a31ce32e 100644 --- a/deluge/ui/gtkui/peers_tab.py +++ b/deluge/ui/gtkui/peers_tab.py @@ -262,7 +262,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 not self.cached_flag_pixbufs.has_key(country):