summaryrefslogtreecommitdiff
path: root/services/countries.py
blob: 8681e81fb2a14b46dd4190c4264c45afc8e5d861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
COUNTRY_NAME_TO_ISO_NUMERIC = {
    "argentina": 32,
    "australia": 36,
    "belgium": 56,
    "brazil": 76,
    "bulgaria": 100,
    "canada": 124,
    "china": 156,
    "czech republic": 203,
    "denmark": 208,
    "finland": 246,
    "france": 250,
    "germany": 276,
    "hong kong": 344,
    "hungary": 348,
    "india": 356,
    "iran": 364,
    "ireland": 372,
    "italy": 380,
    "japan": 392,
    "luxembourg": 442,
    "netherlands": 528,
    "new zealand": 554,
    "norway": 578,
    "northern ireland": 826,
    "russia": 643,
    "soviet union": 643,
    "spain": 724,
    "south africa": 710,
    "south korea": 410,
    "korea, south": 410,
    "republic of korea": 410,
    "sweden": 752,
    "taiwan": 158,
    "thailand": 764,
    "united arab emirates": 784,
    "united kingdom": 826,
    "uk": 826,
    "great britain": 826,
    "england": 826,
    "scotland": 826,
    "united states": 840,
    "united states of america": 840,
    "usa": 840,
}

ISO_NUMERIC_TO_COUNTRY_NAME = {
    value: key.title() for key, value in COUNTRY_NAME_TO_ISO_NUMERIC.items()
}
ISO_NUMERIC_TO_COUNTRY_NAME[410] = "South Korea"
ISO_NUMERIC_TO_COUNTRY_NAME[643] = "Russia"
ISO_NUMERIC_TO_COUNTRY_NAME[826] = "United Kingdom"
ISO_NUMERIC_TO_COUNTRY_NAME[840] = "United States of America"


def split_country_names(value: str | None) -> list[str]:
    if not value:
        return []

    normalized = value.replace(";", ",")
    return [item.strip() for item in normalized.split(",") if item.strip()]


def country_name_to_iso_numeric(value: str | None) -> int | None:
    if not value:
        return None
    return COUNTRY_NAME_TO_ISO_NUMERIC.get(value.casefold().strip())