[#2861] [Core] Add support for python-geoip

This commit is contained in:
Calum Lind 2016-10-20 15:25:16 +01:00
parent bd80ad62a0
commit ffb1316f09
5 changed files with 26 additions and 21 deletions

View File

@ -12,6 +12,7 @@
* geoip-database (optional) * geoip-database (optional)
* setproctitle (optional) * setproctitle (optional)
* pillow (optional) * pillow (optional)
* python-geoip (optional)
* libtorrent (rasterbar) >= 0.14 * libtorrent (rasterbar) >= 0.14

View File

@ -138,6 +138,9 @@ class Core(component.Component):
# New release check information # New release check information
self.new_release = None self.new_release = None
# GeoIP instance with db loaded
self.geoip_instance = None
# Get the core config # Get the core config
self.config = deluge.configmanager.ConfigManager("core.conf") self.config = deluge.configmanager.ConfigManager("core.conf")

View File

@ -49,6 +49,12 @@ import deluge.common
import deluge.component as component import deluge.component as component
from deluge.log import LOG as log from deluge.log import LOG as log
try:
import GeoIP
except ImportError:
GeoIP = None
DEFAULT_PREFS = { DEFAULT_PREFS = {
"send_info": False, "send_info": False,
"info_sent": 0.0, "info_sent": 0.0,
@ -489,23 +495,19 @@ class PreferencesManager(component.Component):
def _on_geoip_db_location(self, key, value): def _on_geoip_db_location(self, key, value):
log.debug("%s: %s", key, value) log.debug("%s: %s", key, value)
# Load the GeoIP DB for country look-ups if available # Load the GeoIP DB for country look-ups if available
geoip_db = ""
if os.path.exists(value): if os.path.exists(value):
geoip_db = value try:
elif os.path.exists(pkg_resources.resource_filename("deluge", os.path.join("data", "GeoIP.dat"))): self.core.geoip_instance = GeoIP.open(value, GeoIP.GEOIP_STANDARD)
geoip_db = pkg_resources.resource_filename("deluge", os.path.join("data", "GeoIP.dat")) 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: else:
log.warning("Unable to find GeoIP database file!") 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): def _on_cache_size(self, key, value):
log.debug("%s: %s", key, value) log.debug("%s: %s", key, value)
self.session_set_setting("cache_size", value) self.session_set_setting("cache_size", value)

View File

@ -556,13 +556,12 @@ class Torrent(object):
except UnicodeDecodeError: except UnicodeDecodeError:
client = str(peer.client).decode("latin-1") client = str(peer.client).decode("latin-1")
# Make country a proper string try:
country = str() country = component.get("Core").geoip_instance.country_code_by_addr(peer.ip[0])
for c in peer.country: except AttributeError:
if not c.isalpha(): country = peer.country
country += " "
else: country = "".join([char if char.isalpha() else " " for char in country])
country += c
ret.append({ ret.append({
"client": client, "client": client,

View File

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