diff --git a/app/location/__init__.py b/app/location/__init__.py index 1da5e9e5..c3d729cd 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -1,8 +1,6 @@ """app.location""" from ..coordinates import Coordinates -from ..utils import countries -from ..utils.populations import country_population - +from ..utils import Country # pylint: disable=redefined-builtin,invalid-name class Location: # pylint: disable=too-many-instance-attributes @@ -11,12 +9,12 @@ class Location: # pylint: disable=too-many-instance-attributes """ def __init__( - self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered, + self, id, country, coordinates, last_updated, confirmed, deaths, recovered, ): # pylint: disable=too-many-arguments # General info. self.id = id - self.country = country.strip() - self.province = province.strip() + self.country = country.name.strip() + self.province = country.province.strip() self.coordinates = coordinates # Last update. @@ -27,26 +25,6 @@ def __init__( self.deaths = deaths self.recovered = recovered - @property - def country_code(self): - """ - Gets the alpha-2 code represention of the country. Returns 'XX' if none is found. - - :returns: The country code. - :rtype: str - """ - return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper() - - @property - def country_population(self): - """ - Gets the population of this location. - - :returns: The population. - :rtype: int - """ - return country_population(self.country_code) - def serialize(self): """ Serializes the location into a dict. @@ -57,10 +35,10 @@ def serialize(self): return { # General info. "id": self.id, - "country": self.country, - "country_code": self.country_code, - "country_population": self.country_population, - "province": self.province, + "country": self.country.name, + "country_code": self.country.country_code, + "country_population": self.country.country_population, + "province": self.country.province, # Coordinates. "coordinates": self.coordinates.serialize(), # Last updated. @@ -80,12 +58,12 @@ class TimelinedLocation(Location): """ # pylint: disable=too-many-arguments - def __init__(self, id, country, province, coordinates, last_updated, timelines): + def __init__(self, id, country, coordinates, last_updated, timelines): super().__init__( # General info. id, - country, - province, + country.name, + country.province, coordinates, last_updated, # Statistics (retrieve latest from timelines). diff --git a/app/location/csbs.py b/app/location/csbs.py index 649e8b22..75a00f94 100644 --- a/app/location/csbs.py +++ b/app/location/csbs.py @@ -1,6 +1,6 @@ """app.locations.csbs.py""" from . import Location - +from ..utils import Country class CSBSLocation(Location): """ @@ -8,12 +8,11 @@ class CSBSLocation(Location): """ # pylint: disable=too-many-arguments,redefined-builtin - def __init__(self, id, state, county, coordinates, last_updated, confirmed, deaths): + def __init__(self, id, country, coordinates, last_updated, confirmed, deaths): super().__init__( # General info. id, - "US", - state, + country, coordinates, last_updated, # Statistics. @@ -22,8 +21,8 @@ def __init__(self, id, state, county, coordinates, last_updated, confirmed, deat recovered=0, ) - self.state = state - self.county = county + self.state = country.state + self.county = country.county def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument """ @@ -36,7 +35,7 @@ def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused # Update with new fields. serialized.update( - {"state": self.state, "county": self.county,} + {"state": self.country.state, "county": self.country.county,} ) # Return the serialized location. diff --git a/app/location/nyt.py b/app/location/nyt.py index ad92212e..32c03389 100644 --- a/app/location/nyt.py +++ b/app/location/nyt.py @@ -1,6 +1,6 @@ """app.locations.nyt.py""" from . import TimelinedLocation - +from ..utils import Country class NYTLocation(TimelinedLocation): """ @@ -8,11 +8,11 @@ class NYTLocation(TimelinedLocation): """ # pylint: disable=too-many-arguments,redefined-builtin - def __init__(self, id, state, county, coordinates, last_updated, timelines): - super().__init__(id, "US", state, coordinates, last_updated, timelines) + def __init__(self, id, country, coordinates, last_updated, timelines): + super().__init__(id, country, coordinates, last_updated, timelines) - self.state = state - self.county = county + self.state = country.state + self.county = country.county def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument """ @@ -25,7 +25,7 @@ def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused # Update with new fields. serialized.update( - {"state": self.state, "county": self.county,} + {"state": self.country.state, "county": self.country.county,} ) # Return the serialized location. diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..ab66836d 100644 --- a/app/services/location/csbs.py +++ b/app/services/location/csbs.py @@ -10,6 +10,7 @@ from ...coordinates import Coordinates from ...location.csbs import CSBSLocation from ...utils import httputils +from ...utils import Country from . import LocationService LOGGER = logging.getLogger("services.location.csbs") @@ -79,8 +80,7 @@ async def get_locations(): CSBSLocation( # General info. i, - state, - county, + Country("","",state,county), # Coordinates. Coordinates(item["Latitude"], item["Longitude"]), # Last update (parse as ISO). diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..2aaa8c3c 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -12,7 +12,7 @@ from ...coordinates import Coordinates from ...location import TimelinedLocation from ...models import Timeline -from ...utils import countries +from ...utils import Country from ...utils import date as date_util from ...utils import httputils from . import LocationService @@ -98,7 +98,7 @@ async def get_category(category): { # General info. "country": country, - "country_code": countries.country_code(country), + "country_code": Country().country_code(country), "province": item["Province/State"], # Coordinates. "coordinates": {"lat": item["Lat"], "long": item["Long"],}, diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..216a0edb 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -12,6 +12,7 @@ from ...models import Timeline from ...utils import httputils from . import LocationService +from ...utils import Country LOGGER = logging.getLogger("services.location.nyt") @@ -113,8 +114,7 @@ async def get_locations(): locations.append( NYTLocation( id=idx, - state=county_state[1], - county=county_state[0], + country=Country("","",county_state[1],county=county_state[0]), coordinates=Coordinates(None, None), # NYT does not provide coordinates last_updated=datetime.utcnow().isoformat() + "Z", # since last request timelines={ diff --git a/app/utils/countries.py b/app/utils/countries.py index 9fb4f98a..4aacbd4d 100644 --- a/app/utils/countries.py +++ b/app/utils/countries.py @@ -1,380 +1,404 @@ """app.utils.countries.py""" import logging +from ..utils.populations import Population LOGGER = logging.getLogger(__name__) -# Default country code. -DEFAULT_COUNTRY_CODE = "XX" +class Country: + # Default country code. + DEFAULT_COUNTRY_CODE = "XX" -# Mapping of country names to alpha-2 codes according to -# https://en.wikipedia.org/wiki/ISO_3166-1. -# As a reference see also https://github.com/TakahikoKawasaki/nv-i18n (in Java) -# fmt: off -COUNTRY_NAME__COUNTRY_CODE = { - "Afghanistan" : "AF", - "Åland Islands" : "AX", - "Albania" : "AL", - "Algeria" : "DZ", - "American Samoa" : "AS", - "Andorra" : "AD", - "Angola" : "AO", - "Anguilla" : "AI", - "Antarctica" : "AQ", - "Antigua and Barbuda" : "AG", - "Argentina" : "AR", - "Armenia" : "AM", - "Aruba" : "AW", - "Australia" : "AU", - "Austria" : "AT", - "Azerbaijan" : "AZ", - " Azerbaijan" : "AZ", - "Bahamas" : "BS", - "The Bahamas" : "BS", - "Bahamas, The" : "BS", - "Bahrain" : "BH", - "Bangladesh" : "BD", - "Barbados" : "BB", - "Belarus" : "BY", - "Belgium" : "BE", - "Belize" : "BZ", - "Benin" : "BJ", - "Bermuda" : "BM", - "Bhutan" : "BT", - "Bolivia, Plurinational State of" : "BO", - "Bolivia" : "BO", - "Bonaire, Sint Eustatius and Saba" : "BQ", - "Caribbean Netherlands" : "BQ", - "Bosnia and Herzegovina" : "BA", - # "Bosnia–Herzegovina" : "BA", - "Bosnia" : "BA", - "Botswana" : "BW", - "Bouvet Island" : "BV", - "Brazil" : "BR", - "British Indian Ocean Territory" : "IO", - "Brunei Darussalam" : "BN", - "Brunei" : "BN", - "Bulgaria" : "BG", - "Burkina Faso" : "BF", - "Burundi" : "BI", - "Cambodia" : "KH", - "Cameroon" : "CM", - "Canada" : "CA", - "Cape Verde" : "CV", - "Cabo Verde" : "CV", - "Cayman Islands" : "KY", - "Central African Republic" : "CF", - "Chad" : "TD", - "Chile" : "CL", - "China" : "CN", - "Mainland China" : "CN", - "Christmas Island" : "CX", - "Cocos (Keeling) Islands" : "CC", - "Colombia" : "CO", - "Comoros" : "KM", - "Congo" : "CG", - "Congo (Brazzaville)" : "CG", - "Republic of the Congo" : "CG", - "Congo, the Democratic Republic of the" : "CD", - "Congo (Kinshasa)" : "CD", - "DR Congo" : "CD", - "Cook Islands" : "CK", - "Costa Rica" : "CR", - "Côte d'Ivoire" : "CI", - "Cote d'Ivoire" : "CI", - "Ivory Coast" : "CI", - "Croatia" : "HR", - "Cuba" : "CU", - "Curaçao" : "CW", - "Curacao" : "CW", - "Cyprus" : "CY", - "Czech Republic" : "CZ", - "Czechia" : "CZ", - "Denmark" : "DK", - "Djibouti" : "DJ", - "Dominica" : "DM", - "Dominican Republic" : "DO", - "Dominican Rep" : "DO", - "Ecuador" : "EC", - "Egypt" : "EG", - "El Salvador" : "SV", - "Equatorial Guinea" : "GQ", - "Eritrea" : "ER", - "Estonia" : "EE", - "Ethiopia" : "ET", - "Falkland Islands (Malvinas)" : "FK", - "Falkland Islands" : "FK", - "Faroe Islands" : "FO", - "Faeroe Islands" : "FO", - "Fiji" : "FJ", - "Finland" : "FI", - "France" : "FR", - "French Guiana" : "GF", - "French Polynesia" : "PF", - "French Southern Territories" : "TF", - "Gabon" : "GA", - "Gambia" : "GM", - "The Gambia" : "GM", - "Gambia, The" : "GM", - "Georgia" : "GE", - "Germany" : "DE", - "Deutschland" : "DE", - "Ghana" : "GH", - "Gibraltar" : "GI", - "Greece" : "GR", - "Greenland" : "GL", - "Grenada" : "GD", - "Guadeloupe" : "GP", - "Guam" : "GU", - "Guatemala" : "GT", - "Guernsey" : "GG", - "Guinea" : "GN", - "Guinea-Bissau" : "GW", - "Guyana" : "GY", - "Haiti" : "HT", - "Heard Island and McDonald Islands" : "HM", - "Holy See (Vatican City State)" : "VA", - "Holy See" : "VA", - "Vatican City" : "VA", - "Honduras" : "HN", - "Hong Kong" : "HK", - "Hong Kong SAR" : "HK", - "Hungary" : "HU", - "Iceland" : "IS", - "India" : "IN", - "Indonesia" : "ID", - "Iran, Islamic Republic of" : "IR", - "Iran" : "IR", - "Iran (Islamic Republic of)" : "IR", - "Iraq" : "IQ", - "Ireland" : "IE", - "Republic of Ireland" : "IE", - "Isle of Man" : "IM", - "Israel" : "IL", - "Italy" : "IT", - "Jamaica" : "JM", - "Japan" : "JP", - "Jersey" : "JE", - # Guernsey and Jersey form Channel Islands. Conjoin Guernsey on Jersey. - # Jersey has higher population. - # https://en.wikipedia.org/wiki/Channel_Islands - "Guernsey and Jersey" : "JE", - "Channel Islands" : "JE", - # "Channel Islands" : "GB", - "Jordan" : "JO", - "Kazakhstan" : "KZ", - "Kenya" : "KE", - "Kiribati" : "KI", - "Korea, Democratic People's Republic of" : "KP", - "North Korea" : "KP", - "Korea, Republic of" : "KR", - "Korea, South" : "KR", - "South Korea" : "KR", - "Republic of Korea" : "KR", - "Kosovo, Republic of" : "XK", - "Kosovo" : "XK", - "Kuwait" : "KW", - "Kyrgyzstan" : "KG", - "Lao People's Democratic Republic" : "LA", - "Laos" : "LA", - "Latvia" : "LV", - "Lebanon" : "LB", - "Lesotho" : "LS", - "Liberia" : "LR", - "Libya" : "LY", - "Liechtenstein" : "LI", - "Lithuania" : "LT", - "Luxembourg" : "LU", - "Macao" : "MO", - # TODO Macau is probably a typo. Report it to CSSEGISandData/COVID-19 - "Macau" : "MO", - "Macao SAR" : "MO", - "North Macedonia" : "MK", - "Macedonia" : "MK", - "Madagascar" : "MG", - "Malawi" : "MW", - "Malaysia" : "MY", - "Maldives" : "MV", - "Mali" : "ML", - "Malta" : "MT", - "Marshall Islands" : "MH", - "Martinique" : "MQ", - "Mauritania" : "MR", - "Mauritius" : "MU", - "Mayotte" : "YT", - "Mexico" : "MX", - "Micronesia, Federated States of" : "FM", - "F.S. Micronesia" : "FM", - "Micronesia" : "FM", - "Moldova, Republic of" : "MD", - "Republic of Moldova" : "MD", - "Moldova" : "MD", - "Monaco" : "MC", - "Mongolia" : "MN", - "Montenegro" : "ME", - "Montserrat" : "MS", - "Morocco" : "MA", - "Mozambique" : "MZ", - "Myanmar" : "MM", - "Burma" : "MM", - "Namibia" : "NA", - "Nauru" : "NR", - "Nepal" : "NP", - "Netherlands" : "NL", - "New Caledonia" : "NC", - "New Zealand" : "NZ", - "Nicaragua" : "NI", - "Niger" : "NE", - "Nigeria" : "NG", - "Niue" : "NU", - "Norfolk Island" : "NF", - "Northern Mariana Islands" : "MP", - "Norway" : "NO", - "Oman" : "OM", - "Pakistan" : "PK", - "Palau" : "PW", - "Palestine, State of" : "PS", - "Palestine" : "PS", - "occupied Palestinian territory" : "PS", - "State of Palestine" : "PS", - "The West Bank and Gaza" : "PS", - "West Bank and Gaza" : "PS", - "Panama" : "PA", - "Papua New Guinea" : "PG", - "Paraguay" : "PY", - "Peru" : "PE", - "Philippines" : "PH", - "Pitcairn" : "PN", - "Poland" : "PL", - "Portugal" : "PT", - "Puerto Rico" : "PR", - "Qatar" : "QA", - "Réunion" : "RE", - "Reunion" : "RE", - "Romania" : "RO", - "Russian Federation" : "RU", - "Russia" : "RU", - "Rwanda" : "RW", - "Saint Barthélemy" : "BL", - "Saint Barthelemy" : "BL", - "Saint Helena, Ascension and Tristan da Cunha" : "SH", - "Saint Helena" : "SH", - "Saint Kitts and Nevis" : "KN", - "Saint Kitts & Nevis" : "KN", - "Saint Lucia" : "LC", - "Saint Martin (French part)" : "MF", - "Saint Martin" : "MF", - "St. Martin" : "MF", - "Saint Pierre and Miquelon" : "PM", - "Saint Pierre & Miquelon" : "PM", - "Saint Vincent and the Grenadines" : "VC", - "St. Vincent & Grenadines" : "VC", - "Samoa" : "WS", - "San Marino" : "SM", - "Sao Tome and Principe" : "ST", - "São Tomé and Príncipe" : "ST", - "Sao Tome & Principe" : "ST", - "Saudi Arabia" : "SA", - "Senegal" : "SN", - "Serbia" : "RS", - "Seychelles" : "SC", - "Sierra Leone" : "SL", - "Singapore" : "SG", - "Sint Maarten (Dutch part)" : "SX", - "Sint Maarten" : "SX", - "Slovakia" : "SK", - "Slovenia" : "SI", - "Solomon Islands" : "SB", - "Somalia" : "SO", - "South Africa" : "ZA", - "South Georgia and the South Sandwich Islands" : "GS", - "South Sudan" : "SS", - "Spain" : "ES", - "Sri Lanka" : "LK", - "Sudan" : "SD", - "Suriname" : "SR", - "Svalbard and Jan Mayen" : "SJ", - "Eswatini" : "SZ", # previous name "Swaziland" - "Swaziland" : "SZ", - "Sweden" : "SE", - "Switzerland" : "CH", - "Syrian Arab Republic" : "SY", - "Syria" : "SY", - "Taiwan, Province of China" : "TW", - "Taiwan*" : "TW", - "Taipei and environs" : "TW", - "Taiwan" : "TW", - "Tajikistan" : "TJ", - "Tanzania, United Republic of" : "TZ", - "Tanzania" : "TZ", - "Thailand" : "TH", - "Timor-Leste" : "TL", - "East Timor" : "TL", - "Togo" : "TG", - "Tokelau" : "TK", - "Tonga" : "TO", - "Trinidad and Tobago" : "TT", - "Tunisia" : "TN", - "Turkey" : "TR", - "Turkmenistan" : "TM", - "Turks and Caicos Islands" : "TC", - "Turks and Caicos" : "TC", - "Tuvalu" : "TV", - "Uganda" : "UG", - "Ukraine" : "UA", - "United Arab Emirates" : "AE", - "Emirates" : "AE", - "United Kingdom" : "GB", - "UK" : "GB", - # Conjoin North Ireland on United Kingdom - "North Ireland" : "GB", - "United States" : "US", - "US" : "US", - "United States Minor Outlying Islands" : "UM", - "Uruguay" : "UY", - "Uzbekistan" : "UZ", - "Vanuatu" : "VU", - "Venezuela, Bolivarian Republic of" : "VE", - "Venezuela" : "VE", - "Viet Nam" : "VN", - "Vietnam" : "VN", - "Virgin Islands, British" : "VG", - "British Virgin Islands" : "VG", - "Virgin Islands, U.S." : "VI", - "U.S. Virgin Islands" : "VI", - "Wallis and Futuna" : "WF", - "Wallis & Futuna" : "WF", - "Western Sahara" : "EH", - "Yemen" : "YE", - "Zambia" : "ZM", - "Zimbabwe" : "ZW", + # Mapping of country names to alpha-2 codes according to + # https://en.wikipedia.org/wiki/ISO_3166-1. + # As a reference see also https://github.com/TakahikoKawasaki/nv-i18n (in Java) + # fmt: off + COUNTRY_NAME__COUNTRY_CODE = { + "Afghanistan" : "AF", + "Åland Islands" : "AX", + "Albania" : "AL", + "Algeria" : "DZ", + "American Samoa" : "AS", + "Andorra" : "AD", + "Angola" : "AO", + "Anguilla" : "AI", + "Antarctica" : "AQ", + "Antigua and Barbuda" : "AG", + "Argentina" : "AR", + "Armenia" : "AM", + "Aruba" : "AW", + "Australia" : "AU", + "Austria" : "AT", + "Azerbaijan" : "AZ", + " Azerbaijan" : "AZ", + "Bahamas" : "BS", + "The Bahamas" : "BS", + "Bahamas, The" : "BS", + "Bahrain" : "BH", + "Bangladesh" : "BD", + "Barbados" : "BB", + "Belarus" : "BY", + "Belgium" : "BE", + "Belize" : "BZ", + "Benin" : "BJ", + "Bermuda" : "BM", + "Bhutan" : "BT", + "Bolivia, Plurinational State of" : "BO", + "Bolivia" : "BO", + "Bonaire, Sint Eustatius and Saba" : "BQ", + "Caribbean Netherlands" : "BQ", + "Bosnia and Herzegovina" : "BA", + # "Bosnia–Herzegovina" : "BA", + "Bosnia" : "BA", + "Botswana" : "BW", + "Bouvet Island" : "BV", + "Brazil" : "BR", + "British Indian Ocean Territory" : "IO", + "Brunei Darussalam" : "BN", + "Brunei" : "BN", + "Bulgaria" : "BG", + "Burkina Faso" : "BF", + "Burundi" : "BI", + "Cambodia" : "KH", + "Cameroon" : "CM", + "Canada" : "CA", + "Cape Verde" : "CV", + "Cabo Verde" : "CV", + "Cayman Islands" : "KY", + "Central African Republic" : "CF", + "Chad" : "TD", + "Chile" : "CL", + "China" : "CN", + "Mainland China" : "CN", + "Christmas Island" : "CX", + "Cocos (Keeling) Islands" : "CC", + "Colombia" : "CO", + "Comoros" : "KM", + "Congo" : "CG", + "Congo (Brazzaville)" : "CG", + "Republic of the Congo" : "CG", + "Congo, the Democratic Republic of the" : "CD", + "Congo (Kinshasa)" : "CD", + "DR Congo" : "CD", + "Cook Islands" : "CK", + "Costa Rica" : "CR", + "Côte d'Ivoire" : "CI", + "Cote d'Ivoire" : "CI", + "Ivory Coast" : "CI", + "Croatia" : "HR", + "Cuba" : "CU", + "Curaçao" : "CW", + "Curacao" : "CW", + "Cyprus" : "CY", + "Czech Republic" : "CZ", + "Czechia" : "CZ", + "Denmark" : "DK", + "Djibouti" : "DJ", + "Dominica" : "DM", + "Dominican Republic" : "DO", + "Dominican Rep" : "DO", + "Ecuador" : "EC", + "Egypt" : "EG", + "El Salvador" : "SV", + "Equatorial Guinea" : "GQ", + "Eritrea" : "ER", + "Estonia" : "EE", + "Ethiopia" : "ET", + "Falkland Islands (Malvinas)" : "FK", + "Falkland Islands" : "FK", + "Faroe Islands" : "FO", + "Faeroe Islands" : "FO", + "Fiji" : "FJ", + "Finland" : "FI", + "France" : "FR", + "French Guiana" : "GF", + "French Polynesia" : "PF", + "French Southern Territories" : "TF", + "Gabon" : "GA", + "Gambia" : "GM", + "The Gambia" : "GM", + "Gambia, The" : "GM", + "Georgia" : "GE", + "Germany" : "DE", + "Deutschland" : "DE", + "Ghana" : "GH", + "Gibraltar" : "GI", + "Greece" : "GR", + "Greenland" : "GL", + "Grenada" : "GD", + "Guadeloupe" : "GP", + "Guam" : "GU", + "Guatemala" : "GT", + "Guernsey" : "GG", + "Guinea" : "GN", + "Guinea-Bissau" : "GW", + "Guyana" : "GY", + "Haiti" : "HT", + "Heard Island and McDonald Islands" : "HM", + "Holy See (Vatican City State)" : "VA", + "Holy See" : "VA", + "Vatican City" : "VA", + "Honduras" : "HN", + "Hong Kong" : "HK", + "Hong Kong SAR" : "HK", + "Hungary" : "HU", + "Iceland" : "IS", + "India" : "IN", + "Indonesia" : "ID", + "Iran, Islamic Republic of" : "IR", + "Iran" : "IR", + "Iran (Islamic Republic of)" : "IR", + "Iraq" : "IQ", + "Ireland" : "IE", + "Republic of Ireland" : "IE", + "Isle of Man" : "IM", + "Israel" : "IL", + "Italy" : "IT", + "Jamaica" : "JM", + "Japan" : "JP", + "Jersey" : "JE", + # Guernsey and Jersey form Channel Islands. Conjoin Guernsey on Jersey. + # Jersey has higher population. + # https://en.wikipedia.org/wiki/Channel_Islands + "Guernsey and Jersey" : "JE", + "Channel Islands" : "JE", + # "Channel Islands" : "GB", + "Jordan" : "JO", + "Kazakhstan" : "KZ", + "Kenya" : "KE", + "Kiribati" : "KI", + "Korea, Democratic People's Republic of" : "KP", + "North Korea" : "KP", + "Korea, Republic of" : "KR", + "Korea, South" : "KR", + "South Korea" : "KR", + "Republic of Korea" : "KR", + "Kosovo, Republic of" : "XK", + "Kosovo" : "XK", + "Kuwait" : "KW", + "Kyrgyzstan" : "KG", + "Lao People's Democratic Republic" : "LA", + "Laos" : "LA", + "Latvia" : "LV", + "Lebanon" : "LB", + "Lesotho" : "LS", + "Liberia" : "LR", + "Libya" : "LY", + "Liechtenstein" : "LI", + "Lithuania" : "LT", + "Luxembourg" : "LU", + "Macao" : "MO", + # TODO Macau is probably a typo. Report it to CSSEGISandData/COVID-19 + "Macau" : "MO", + "Macao SAR" : "MO", + "North Macedonia" : "MK", + "Macedonia" : "MK", + "Madagascar" : "MG", + "Malawi" : "MW", + "Malaysia" : "MY", + "Maldives" : "MV", + "Mali" : "ML", + "Malta" : "MT", + "Marshall Islands" : "MH", + "Martinique" : "MQ", + "Mauritania" : "MR", + "Mauritius" : "MU", + "Mayotte" : "YT", + "Mexico" : "MX", + "Micronesia, Federated States of" : "FM", + "F.S. Micronesia" : "FM", + "Micronesia" : "FM", + "Moldova, Republic of" : "MD", + "Republic of Moldova" : "MD", + "Moldova" : "MD", + "Monaco" : "MC", + "Mongolia" : "MN", + "Montenegro" : "ME", + "Montserrat" : "MS", + "Morocco" : "MA", + "Mozambique" : "MZ", + "Myanmar" : "MM", + "Burma" : "MM", + "Namibia" : "NA", + "Nauru" : "NR", + "Nepal" : "NP", + "Netherlands" : "NL", + "New Caledonia" : "NC", + "New Zealand" : "NZ", + "Nicaragua" : "NI", + "Niger" : "NE", + "Nigeria" : "NG", + "Niue" : "NU", + "Norfolk Island" : "NF", + "Northern Mariana Islands" : "MP", + "Norway" : "NO", + "Oman" : "OM", + "Pakistan" : "PK", + "Palau" : "PW", + "Palestine, State of" : "PS", + "Palestine" : "PS", + "occupied Palestinian territory" : "PS", + "State of Palestine" : "PS", + "The West Bank and Gaza" : "PS", + "West Bank and Gaza" : "PS", + "Panama" : "PA", + "Papua New Guinea" : "PG", + "Paraguay" : "PY", + "Peru" : "PE", + "Philippines" : "PH", + "Pitcairn" : "PN", + "Poland" : "PL", + "Portugal" : "PT", + "Puerto Rico" : "PR", + "Qatar" : "QA", + "Réunion" : "RE", + "Reunion" : "RE", + "Romania" : "RO", + "Russian Federation" : "RU", + "Russia" : "RU", + "Rwanda" : "RW", + "Saint Barthélemy" : "BL", + "Saint Barthelemy" : "BL", + "Saint Helena, Ascension and Tristan da Cunha" : "SH", + "Saint Helena" : "SH", + "Saint Kitts and Nevis" : "KN", + "Saint Kitts & Nevis" : "KN", + "Saint Lucia" : "LC", + "Saint Martin (French part)" : "MF", + "Saint Martin" : "MF", + "St. Martin" : "MF", + "Saint Pierre and Miquelon" : "PM", + "Saint Pierre & Miquelon" : "PM", + "Saint Vincent and the Grenadines" : "VC", + "St. Vincent & Grenadines" : "VC", + "Samoa" : "WS", + "San Marino" : "SM", + "Sao Tome and Principe" : "ST", + "São Tomé and Príncipe" : "ST", + "Sao Tome & Principe" : "ST", + "Saudi Arabia" : "SA", + "Senegal" : "SN", + "Serbia" : "RS", + "Seychelles" : "SC", + "Sierra Leone" : "SL", + "Singapore" : "SG", + "Sint Maarten (Dutch part)" : "SX", + "Sint Maarten" : "SX", + "Slovakia" : "SK", + "Slovenia" : "SI", + "Solomon Islands" : "SB", + "Somalia" : "SO", + "South Africa" : "ZA", + "South Georgia and the South Sandwich Islands" : "GS", + "South Sudan" : "SS", + "Spain" : "ES", + "Sri Lanka" : "LK", + "Sudan" : "SD", + "Suriname" : "SR", + "Svalbard and Jan Mayen" : "SJ", + "Eswatini" : "SZ", # previous name "Swaziland" + "Swaziland" : "SZ", + "Sweden" : "SE", + "Switzerland" : "CH", + "Syrian Arab Republic" : "SY", + "Syria" : "SY", + "Taiwan, Province of China" : "TW", + "Taiwan*" : "TW", + "Taipei and environs" : "TW", + "Taiwan" : "TW", + "Tajikistan" : "TJ", + "Tanzania, United Republic of" : "TZ", + "Tanzania" : "TZ", + "Thailand" : "TH", + "Timor-Leste" : "TL", + "East Timor" : "TL", + "Togo" : "TG", + "Tokelau" : "TK", + "Tonga" : "TO", + "Trinidad and Tobago" : "TT", + "Tunisia" : "TN", + "Turkey" : "TR", + "Turkmenistan" : "TM", + "Turks and Caicos Islands" : "TC", + "Turks and Caicos" : "TC", + "Tuvalu" : "TV", + "Uganda" : "UG", + "Ukraine" : "UA", + "United Arab Emirates" : "AE", + "Emirates" : "AE", + "United Kingdom" : "GB", + "UK" : "GB", + # Conjoin North Ireland on United Kingdom + "North Ireland" : "GB", + "United States" : "US", + "US" : "US", + "United States Minor Outlying Islands" : "UM", + "Uruguay" : "UY", + "Uzbekistan" : "UZ", + "Vanuatu" : "VU", + "Venezuela, Bolivarian Republic of" : "VE", + "Venezuela" : "VE", + "Viet Nam" : "VN", + "Vietnam" : "VN", + "Virgin Islands, British" : "VG", + "British Virgin Islands" : "VG", + "Virgin Islands, U.S." : "VI", + "U.S. Virgin Islands" : "VI", + "Wallis and Futuna" : "WF", + "Wallis & Futuna" : "WF", + "Western Sahara" : "EH", + "Yemen" : "YE", + "Zambia" : "ZM", + "Zimbabwe" : "ZW", - # see also - # https://en.wikipedia.org/wiki/List_of_sovereign_states_and_dependent_territories_by_continent_(data_file)#Data_file - # https://en.wikipedia.org/wiki/List_of_sovereign_states_and_dependent_territories_by_continent - "United Nations Neutral Zone" : "XD", - "Iraq-Saudi Arabia Neutral Zone" : "XE", - "Spratly Islands" : "XS", + # see also + # https://en.wikipedia.org/wiki/List_of_sovereign_states_and_dependent_territories_by_continent_(data_file)#Data_file + # https://en.wikipedia.org/wiki/List_of_sovereign_states_and_dependent_territories_by_continent + "United Nations Neutral Zone" : "XD", + "Iraq-Saudi Arabia Neutral Zone" : "XE", + "Spratly Islands" : "XS", - # "Diamond Princess" : default_country_code, - # TODO "Disputed Territory" conflicts with `default_country_code` - # "Disputed Territory" : "XX", + # "Diamond Princess" : default_country_code, + # TODO "Disputed Territory" conflicts with `default_country_code` + # "Disputed Territory" : "XX", - # "Others" has no mapping, i.e. the default val is used + # "Others" has no mapping, i.e. the default val is used + + # ships: + # "Cruise Ship" + # "MS Zaandam" + } + + def __init__(self, name, province, state, county): + self.name = name + self.province = province + self.state = state + self.county = county + + # fmt: on + @property + def country_code(self, value): + """ + Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1 + Defaults to "XX". + Gets the alpha-2 code represention of the country. Returns 'XX' if none is found. + """ + code = self.COUNTRY_NAME__COUNTRY_CODE.get(value, self.DEFAULT_COUNTRY_CODE) + if code == self.DEFAULT_COUNTRY_CODE: + # log at sub DEBUG level + LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!") + + return code.upper() - # ships: - # "Cruise Ship" - # "MS Zaandam" -} -# fmt: on -def country_code(value): - """ - Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1 - Defaults to "XX". """ - code = COUNTRY_NAME__COUNTRY_CODE.get(value, DEFAULT_COUNTRY_CODE) - if code == DEFAULT_COUNTRY_CODE: - # log at sub DEBUG level - LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!") + Gets the population of this location. + + :returns: The population. + :rtype: int + """ + @property + def country_population(self): + return Population().country_population(self.country_code) + + - return code diff --git a/app/utils/populations.py b/app/utils/populations.py index c02f15a9..6a8eeaf0 100644 --- a/app/utils/populations.py +++ b/app/utils/populations.py @@ -10,51 +10,52 @@ GEONAMES_URL = "http://api.geonames.org/countryInfoJSON" GEONAMES_BACKUP_PATH = "geonames_population_mappings.json" -# Fetching of the populations. -def fetch_populations(save=False): - """ - Returns a dictionary containing the population of each country fetched from the GeoNames. - https://www.geonames.org/ - - TODO: only skip writing to the filesystem when deployed with gunicorn, or handle concurent access, or use DB. - - :returns: The mapping of populations. - :rtype: dict - """ - LOGGER.info("Fetching populations...") - - # Mapping of populations - mappings = {} - - # Fetch the countries. - try: - countries = requests.get(GEONAMES_URL, params={"username": "dperic"}, timeout=1.25).json()[ - "geonames" - ] - # Go through all the countries and perform the mapping. - for country in countries: - mappings.update({country["countryCode"]: int(country["population"]) or None}) - - if mappings and save: - LOGGER.info(f"Saving population data to {app.io.save(GEONAMES_BACKUP_PATH, mappings)}") - except (json.JSONDecodeError, KeyError, requests.exceptions.Timeout) as err: - LOGGER.warning(f"Error pulling population data. {err.__class__.__name__}: {err}") - mappings = app.io.load(GEONAMES_BACKUP_PATH) - LOGGER.info(f"Using backup data from {GEONAMES_BACKUP_PATH}") - # Finally, return the mappings. - LOGGER.info("Fetched populations") - return mappings - - -# Mapping of alpha-2 codes country codes to population. -POPULATIONS = fetch_populations() - -# Retrieving. -def country_population(country_code, default=None): - """ - Fetches the population of the country with the provided country code. - - :returns: The population. - :rtype: int - """ - return POPULATIONS.get(country_code, default) +class Population: + # Fetching of the populations. + def fetch_populations(save=False): + """ + Returns a dictionary containing the population of each country fetched from the GeoNames. + https://www.geonames.org/ + + TODO: only skip writing to the filesystem when deployed with gunicorn, or handle concurent access, or use DB. + + :returns: The mapping of populations. + :rtype: dict + """ + LOGGER.info("Fetching populations...") + + # Mapping of populations + mappings = {} + + # Fetch the countries. + try: + countries = requests.get(GEONAMES_URL, params={"username": "dperic"}, timeout=1.25).json()[ + "geonames" + ] + # Go through all the countries and perform the mapping. + for country in countries: + mappings.update({country["countryCode"]: int(country["population"]) or None}) + + if mappings and save: + LOGGER.info(f"Saving population data to {app.io.save(GEONAMES_BACKUP_PATH, mappings)}") + except (json.JSONDecodeError, KeyError, requests.exceptions.Timeout) as err: + LOGGER.warning(f"Error pulling population data. {err.__class__.__name__}: {err}") + mappings = app.io.load(GEONAMES_BACKUP_PATH) + LOGGER.info(f"Using backup data from {GEONAMES_BACKUP_PATH}") + # Finally, return the mappings. + LOGGER.info("Fetched populations") + return mappings + + + # Mapping of alpha-2 codes country codes to population. + POPULATIONS = fetch_populations() + + # Retrieving. + def country_population(self,country_code, default=None): + """ + Fetches the population of the country with the provided country code. + + :returns: The population. + :rtype: int + """ + return self.POPULATIONS.get(country_code, default)