aboutsummaryrefslogtreecommitdiff
path: root/desktop/scripts/countries-update-list.py
blob: 248e0f30403dc0b09ca67b8e94d23ec2631996f3 (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
#!/usr/bin/env python3
import os
import tempfile
import subprocess
import json

import onionshare_cli


def main():
    # Clone the country-list repo
    tmp_dir = tempfile.TemporaryDirectory()
    subprocess.run(
        ["git", "clone", "https://github.com/umpirsky/country-list.git"],
        cwd=tmp_dir.name,
    )
    repo_dir = os.path.join(tmp_dir.name, "country-list")

    # Get the list of enabled languages
    common = onionshare_cli.common.Common()
    settings = onionshare_cli.settings.Settings(common)
    available_locales = list(settings.available_locales)

    # Make a dictionary that makes a language's ISO 3166-1 to its name in all enabled languages
    os.makedirs(os.path.join("onionshare", "resources", "countries"), exist_ok=True)
    for locale in available_locales:
        with open(os.path.join(repo_dir, "data", locale, "country.json")) as f:
            countries = json.loads(f.read())

        # Remove countries we don't have images for
        for key in ["JE", "MH", "FM", "MP", "PS", "TV", "UM"]:
            del countries[key]

        with open(
            os.path.join("onionshare", "resources", "countries", f"{locale}.json"),
            "w",
        ) as f:
            f.write(json.dumps(countries))


if __name__ == "__main__":
    main()