From df7312158d30b3c84a329f0bee4be587cd4342e5 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 10 Oct 2018 11:43:20 +0100 Subject: [PATCH] Use countries from iso3166 module Signed-off-by: Michael Tremer --- src/backend/countries.py | 276 +-------------------------------------- src/backend/geoip.py | 12 +- 2 files changed, 8 insertions(+), 280 deletions(-) diff --git a/src/backend/countries.py b/src/backend/countries.py index ec271558..edb63017 100644 --- a/src/backend/countries.py +++ b/src/backend/countries.py @@ -1,274 +1,12 @@ #!/usr/bin/python -# encoding: utf-8 -_ = lambda x: x +import iso3166 -country_list = { - "AD" : _("Andorra"), - "AE" : _("United Arab Emirates"), - "AF" : _("Afghanistan"), - "AG" : _("Antigua and Barbuda"), - "AI" : _("Aanguilla"), - "AL" : _("Albania"), - "AM" : _("Armenia"), - "AO" : _("Angola"), - "AQ" : _("Antarctica"), - "AR" : _("Argentina"), - "AS" : _("American Samoa"), - "AT" : _("Austria"), - "AU" : _("Australia"), - "AW" : _("Aruba"), - "AX" : _("Åland Islands"), - "AZ" : _("Azerbaijan"), - "BA" : _("Bosnia and Herzegovina"), - "BB" : _("Barbados"), - "BD" : _("Bangladesh"), - "BE" : _("Belgium"), - "BF" : _("Burkina Faso"), - "BG" : _("Bulgaria"), - "BH" : _("Bahrain"), - "BI" : _("Burundi"), - "BJ" : _("Benin"), - "BL" : _("Saint Barthélemy"), - "BM" : _("Bermuda"), - "BN" : _("Brunei Darussalam"), - "BO" : _("Plurinational State of Bolivia"), - "BQ" : _("Sint Eustatius and Saba Bonaire"), - "BR" : _("Brazil"), - "BS" : _("Bahamas"), - "BT" : _("Bhutan"), - "BV" : _("Bouvet Island"), - "BW" : _("Botswana"), - "BY" : _("Belarus"), - "BZ" : _("Belize"), - "CA" : _("Canada"), - "CC" : _("Cocos (Keeling) Islands"), - "CD" : _("The Democratic Republic of the Congo"), - "CF" : _("Central African Republic"), - "CG" : _("Congo"), - "CH" : _("Switzerland"), - "CI" : _("Côte d'Ivoire"), - "CK" : _("Cook Islands"), - "CL" : _("Chile"), - "CM" : _("Cameroon"), - "CN" : _("China"), - "CO" : _("Colombia"), - "CR" : _("Costa Rica"), - "CU" : _("Cuba"), - "CV" : _("Cape Verde"), - "CW" : _("Curaçao"), - "CX" : _("Chrismas Islands"), - "CY" : _("Cyprus"), - "CZ" : _("Czech Republic"), - "DE" : _("Germany"), - "DJ" : _("Djibouti"), - "DK" : _("Denmark"), - "DM" : _("Dominica"), - "DO" : _("Dominican Republic"), - "DZ" : _("Algeria"), - "EC" : _("Ecuador"), - "EE" : _("Estonia"), - "EG" : _("Egypt"), - "EH" : _("Western Sahara"), - "ER" : _("Eritrea"), - "ES" : _("Spain"), - "ET" : _("Ethiopia"), - "FI" : _("Finland"), - "FJ" : _("Fiji"), - "FK" : _("Falkland Islands (Malvinas)"), - "FM" : _("Federated States of Micronesia"), - "FO" : _("Faroe Islands"), - "FR" : _("France"), - "GA" : _("Gabon"), - "GB" : _("United Kingdom"), - "GD" : _("Grenada"), - "GE" : _("Georgia"), - "GF" : _("French Guiana"), - "GG" : _("Guersey"), - "GH" : _("Ghana"), - "GI" : _("Gibraltar"), - "GL" : _("Greenland"), - "GM" : _("Gambia"), - "GN" : _("Guinea"), - "GP" : _("Guadeloupe"), - "GQ" : _("Equatorial Guinea"), - "GR" : _("Greece"), - "GS" : _("South Georgia and the South Sandwich Islands"), - "GT" : _("Guatemala"), - "GU" : _("Guam"), - "GW" : _("Guinea-Bissau"), - "GY" : _("Guyana"), - "HK" : _("Hong Kong"), - "HM" : _("Heard Island and McDonald Islands"), - "HN" : _("Honduras"), - "HR" : _("Croatia"), - "HT" : _("Haiti"), - "HU" : _("Hungary"), - "ID" : _("Indonesia"), - "IE" : _("Ireland"), - "IL" : _("Israel"), - "IM" : _("Isle of Man"), - "IN" : _("India"), - "IO" : _("British Indian Ocean Territory"), - "IQ" : _("Iraq"), - "IR" : _("Islamic Republic of Iran"), - "IS" : _("Iceland"), - "IT" : _("Italy"), - "JE" : _("Jersey"), - "JM" : _("Jamaica"), - "JO" : _("Jordan"), - "JP" : _("Japan"), - "KE" : _("Kenya"), - "KG" : _("Kyrgyzstan"), - "KH" : _("Cambodia"), - "KI" : _("Kiribati"), - "KM" : _("Comoros"), - "KN" : _("Saint Kitts and Nevis"), - "KP" : _("Democratic People's Republic of Korea"), - "KR" : _("Republic of Korea"), - "KW" : _("Kuwait"), - "KY" : _("Cayman Islands"), - "KZ" : _("Kazakhstan"), - "LA" : _("Lao People's Democratic Public"), - "LB" : _("Lebanon"), - "LC" : _("Saint Lucia"), - "LI" : _("Liechtenstein"), - "LK" : _("Sri Lanka"), - "LR" : _("Liberia"), - "LS" : _("Lesotho"), - "LT" : _("Lithuania"), - "LU" : _("Luxembourg"), - "LV" : _("Latvia"), - "LY" : _("Libya"), - "MA" : _("Morocco"), - "MC" : _("Monaco"), - "MD" : _("Republic of Moldova"), - "ME" : _("Montenegro"), - "MF" : _("Saint Martin (French Part)"), - "MG" : _("Madagascar"), - "MH" : _("Marshall Islands"), - "MK" : _("The former Yugoslav Republic of Macedonia"), - "ML" : _("Mali"), - "MM" : _("Myanmar"), - "MN" : _("Mongolia"), - "MO" : _("Macao"), - "MP" : _("Northern Mariana Islands"), - "MQ" : _("Martinique"), - "MR" : _("Mauritania"), - "MS" : _("Montserrat"), - "MT" : _("Malta"), - "MU" : _("Mauritius"), - "MV" : _("Maldives"), - "MW" : _("Malawi"), - "MX" : _("Mexico"), - "MY" : _("Malaysia"), - "MZ" : _("Mozambique"), - "NA" : _("Namibia"), - "NC" : _("New Caledonia"), - "NE" : _("Niger"), - "NF" : _("Norfolk Island"), - "NG" : _("Nigeria"), - "NI" : _("Nicaragua"), - "NL" : _("Netherlands"), - "NO" : _("Norway"), - "NP" : _("Nepal"), - "NR" : _("Nauru"), - "NU" : _("Niue"), - "NZ" : _("New Zealand"), - "OM" : _("Oman"), - "PA" : _("Panama"), - "PE" : _("Peru"), - "PF" : _("French Polynesia"), - "PG" : _("Papua New Guinea"), - "PH" : _("Philipplines"), - "PK" : _("Pakistan"), - "PL" : _("Poland"), - "PM" : _("Saint Pierre and Miquelon"), - "PN" : _("Pitcairn"), - "PR" : _("Puerto Rico"), - "PS" : _("Palestinian Territory, occupied"), - "PT" : _("Portugal"), - "PW" : _("Palau"), - "PY" : _("Paraguay"), - "QA" : _("Qatar"), - "RE" : _("Réunion"), - "RO" : _("Romania"), - "RS" : _("Serbia"), - "RU" : _("Russian Federation"), - "RW" : _("Rwanda"), - "SA" : _("Saudi Arabia"), - "SB" : _("Solomon Islands"), - "SC" : _("Seychelles"), - "SD" : _("Sudan"), - "SE" : _("Sweden"), - "SG" : _("Singapore"), - "SH" : _("Saint Helena, Ascension and Tristan Da Cunha"), - "SI" : _("Slovenia"), - "SJ" : _("Svalbard and Jan Mayen"), - "SK" : _("Slovakia"), - "SL" : _("Sierra Leone"), - "SM" : _("San Marino"), - "SN" : _("Senegal"), - "SO" : _("Somalia"), - "SR" : _("Suriname"), - "SS" : _("South Sudan"), - "ST" : _("Sao Tome and Principe"), - "SV" : _("El Salvador"), - "SX" : _("Sint Maarten (Dutch Part)"), - "SY" : _("Syrian Arab Republic"), - "SZ" : _("Swaziland"), - "TC" : _("Turks and Caicos Islands"), - "TD" : _("Chad"), - "TF" : _("French Southern Territories"), - "TG" : _("Togo"), - "TH" : _("Thailand"), - "TJ" : _("Tajikistan"), - "TK" : _("Tokelau"), - "TL" : _("Timor-Leste"), - "TM" : _("Turkmenistan"), - "TN" : _("Tunisia"), - "TO" : _("Tonga"), - "TR" : _("Turkey"), - "TT" : _("Trinidad and Tobago"), - "TV" : _("Tuvalu"), - "TW" : _("Taiwan, Province of China"), - "TZ" : _("United Republic of Tanzania"), - "UA" : _("Ukraine"), - "UG" : _("Uganda"), - "UM" : _("United States minor outlying islands"), - "US" : _("United States"), - "UY" : _("Uruguay"), - "UZ" : _("Uzbekistan"), - "VA" : _("Vatican City State"), - "VC" : _("Saint Vincent and the Grenadines"), - "VE" : _("Bolivarian Republic of Venezuela"), - "VG" : _("Virgin Islands, British"), - "VI" : _("Virgin Islands, U.S."), - "VN" : _("Viet Nam"), - "VU" : _("Vanuatu"), - "WF" : _("Wallis and Futuna"), - "WS" : _("Samoa"), - "YE" : _("Yemen"), - "YT" : _("Mayotte"), - "ZA" : _("South Africa"), - "ZM" : _("Zambia"), - "ZW" : _("Zimbabwe"), -} - -def get_by_code(code): - return country_list.get(code, None) +def get_name(code): + try: + return iso3166.countries_by_alpha2[code].name + except KeyError: + return code def get_all(locale=None): - if locale is None: - l = list(country_list.keys()) - else: - l = [] - - for c in list(country_list.keys()): - c = locale.translate(c) - l.append(c) - - # Sort list in place. - l.sort() - - return l + return sorted(iso3166.countries, key=lambda c: c.name) diff --git a/src/backend/geoip.py b/src/backend/geoip.py index 3bb887d3..1f69ca94 100644 --- a/src/backend/geoip.py +++ b/src/backend/geoip.py @@ -51,14 +51,4 @@ class GeoIP(Object): } def get_country_name(self, code): - # Return description of some exceptional codes. - try: - return self._countries[code] - except KeyError: - pass - - country = countries.get_by_code(code) - if not country: - return code - - return country + return countries.get_name(code) -- 2.47.3