[#2861] [Core] Add support for python-geoip
This commit is contained in:
parent
bd80ad62a0
commit
ffb1316f09
1
DEPENDS
1
DEPENDS
|
@ -12,6 +12,7 @@
|
|||
* geoip-database (optional)
|
||||
* setproctitle (optional)
|
||||
* pillow (optional)
|
||||
* python-geoip (optional)
|
||||
|
||||
* libtorrent (rasterbar) >= 0.14
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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,22 +495,18 @@ 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"))
|
||||
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)
|
||||
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!")
|
||||
|
||||
def _on_cache_size(self, key, value):
|
||||
log.debug("%s: %s", key, value)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue