Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Location selects country based on IP

  • Loading branch information...
karasu committed Jun 3, 2018
1 parent bef924f commit f9edc90a2fd447b7e77170b64966344ba94db30e
Showing with 48 additions and 13 deletions.
  1. +17 −7 src/geoip.py
  2. +1 −1 src/info.py
  3. +29 −4 src/pages/location.py
  4. +1 −1 update.info
@@ -44,6 +44,8 @@ class GeoIP(object):
REPO_CITY_DATABASE = '/usr/share/GeoIP/GeoLite2-City.mmdb'
LOCAL_CITY_DATABASE = '/usr/share/cnchi/data/GeoLite2-City.mmdb'

SERVERS = ["moc.tsetnosj.pi", "pi/0003:kt.ateb-sogretna"]

def __init__(self):
self.record = None
self._maybe_wait_for_network()
@@ -82,9 +84,17 @@ def _load_data_and_ip(self):
@staticmethod
def _get_external_ip():
""" Get external IP """
json_text = requests.get("http://ip.jsontest.com/").text
data = json.loads(json_text)
return data['ip']
for srv in GeoIP.SERVERS:
srv = "http://" + srv[::-1]
try:
json_text = requests.get(srv).text
if not "503 Over Quota" in json_text:
data = json.loads(json_text)
return data['ip']
except (requests.ConnectionError, json.decoder.JSONDecodeError) as err:
logging.warning(
"Error getting external IP from %s: %s", srv, err)
return None

def _load_database(self, db_path, myip):
""" Loads cities database """
@@ -124,10 +134,10 @@ def get_location(self):
def test_module():
""" Test module """
geo = GeoIP()
print(geo.get_city())
print(geo.get_country())
print(geo.get_continent())
print(geo.get_location())
print("City:", geo.get_city())
print("Country:", geo.get_country())
print("Continent:", geo.get_continent())
print("Location:", geo.get_location())


if __name__ == "__main__":
@@ -29,7 +29,7 @@

""" Set some Cnchi global constants """

CNCHI_VERSION = "0.16.22"
CNCHI_VERSION = "0.16.23"
CNCHI_WEBSITE = "http://www.antergos.com"
CNCHI_RELEASE_STAGE = "production"

@@ -47,6 +47,7 @@
from pages.gtkbasebox import GtkBaseBox
from logging_utils import ContextFilter

import geoip

class Location(GtkBaseBox):
""" Location page """
@@ -66,6 +67,7 @@ def __init__(self, params, prev_page="check", next_page="timezone"):
self.locales = {}
self.load_locales()

self.geoip_country = None
self.selected_country = ""

self.show_all_locations = False
@@ -83,14 +85,16 @@ def on_show_all_locations_checkbox_toggled(self, button, _name):
""" Force to show all locations """
self.show_all_locations = button.get_active()
self.fill_listbox()
if not self.show_all_locations:
self.select_detected_country()

def translate_ui(self):
""" Translates all ui elements """
par1 = _("The location you select will be used to help determine the "
"system locale. It should normally be the country in which "
"you reside.")
"system locale. It should normally be the country in which "
"you reside.")
par2 = _("Please, note that your system language will be "
"determined from this selection.")
"determined from this selection.")
par3 = _("Here is a shortlist of locations based on the language you "
"selected for this installer (click on show all locations "
"to show them all).")
@@ -113,6 +117,27 @@ def select_first_listbox_item(self):
listbox_row = self.listbox.get_children()[0]
self.listbox.select_row(listbox_row)

def select_detected_country(self):
""" Selects listbox item that matches detected country using GeoIP database """
if not self.geoip_country:
logging.debug("Getting your country using GeoIP database")
self.geoip_country = geoip.GeoIP().get_country()
if self.geoip_country:
names = self.geoip_country.names
#logging.debug(names)
for listbox_row in self.listbox.get_children():
label = listbox_row.get_children()[0]
if label is not None:
label = label.get_text()
for name in names.values():
if name in label:
self.selected_country = label
self.listbox.select_row(listbox_row)
return
self.select_first_listbox_item()
else:
self.select_first_listbox_item()

def hide_all(self):
""" Hide all widgets """
names = [
@@ -128,7 +153,7 @@ def prepare(self, direction):
self.hide_all()

self.fill_listbox()
self.select_first_listbox_item()
self.select_detected_country()
self.translate_ui()
self.forward_button.set_sensitive(True)

@@ -1,2 +1,2 @@
{"version":"0.16.22","files":[
{"version":"0.16.23","files":[
]}

0 comments on commit f9edc90

Please sign in to comment.
You can’t perform that action at this time.