summaryrefslogtreecommitdiffhomepage
path: root/gui/scripts/fetch-relay-locations.py
diff options
context:
space:
mode:
authorOskar Nyberg <oskar@mullvad.net>2024-05-10 14:29:56 +0200
committerOskar Nyberg <oskar@mullvad.net>2024-05-15 18:42:58 +0200
commit4bb137bdeec2564612b55fd5bf785fadac218968 (patch)
tree35e85ec9fa65e6dd4184aee63739560dae6a3313 /gui/scripts/fetch-relay-locations.py
parentb09ebf93a127f84573257f163dea612c193d678d (diff)
downloadmullvadvpn-4bb137bdeec2564612b55fd5bf785fadac218968.tar.xz
mullvadvpn-4bb137bdeec2564612b55fd5bf785fadac218968.zip
Remove map related code in geo data scripts
Diffstat (limited to 'gui/scripts/fetch-relay-locations.py')
-rwxr-xr-xgui/scripts/fetch-relay-locations.py126
1 files changed, 126 insertions, 0 deletions
diff --git a/gui/scripts/fetch-relay-locations.py b/gui/scripts/fetch-relay-locations.py
new file mode 100755
index 0000000000..eeea51d56d
--- /dev/null
+++ b/gui/scripts/fetch-relay-locations.py
@@ -0,0 +1,126 @@
+#!/usr/bin/env python3
+"""
+This module adds relay location city and country names to relay-locations.pot
+"""
+
+import os
+from os import path
+import json
+import urllib.request
+from polib import POFile, POEntry
+import colorful as c
+
+SCRIPT_DIR = path.dirname(path.realpath(__file__))
+
+# The output directory for the generated content
+OUT_DIR = path.join(SCRIPT_DIR, "out")
+
+# the directory with the generated localizations content
+LOCALE_OUT_DIR = path.join(OUT_DIR, "locales")
+
+# Relay locations gettext catalogue template filename (.pot)
+RELAY_LOCATIONS_POT_FILENAME = "relay-locations.pot"
+
+
+def extract_relay_translations():
+ try:
+ response = request_relays()
+ except Exception as e:
+ print(c.red("Failed to fetch the relays list: {}".format(e)))
+ raise
+
+ locations = response.get("locations")
+ countries = structure_locations(locations)
+
+ extract_relay_locations_pot(countries)
+
+
+def structure_locations(locations):
+ countries = {}
+
+ for location_key in locations:
+ location = locations.get(location_key)
+ country_name = location.get("country")
+ city_name = location.get("city")
+
+ if not "-" in location_key:
+ print("Location key incorrectly formatted: {}".format(location_key))
+ continue
+
+ country_code, city_code = location_key.split("-")
+
+ if country_name is None:
+ print("Country name missing for {}".format(location_key))
+ continue
+
+ if city_name is None:
+ print("City name missing for {}".format(location_key))
+ continue
+
+ if country_code not in countries:
+ countries[country_code] = {"name": country_name, "cities": {}}
+
+ country = countries[country_code]
+ cities = country["cities"]
+ if location_key != "se-bet":
+ if city_code not in cities:
+ cities[city_code] = city_name
+ else:
+ print("There are multiple entries for {} in {}".format(city_name, country_name))
+
+ return countries
+
+
+def extract_relay_locations_pot(countries):
+ pot = POFile(encoding='utf-8', check_for_duplicates=True)
+ pot.metadata = {"Content-Type": "text/plain; charset=utf-8"}
+ output_path = path.join(LOCALE_OUT_DIR, RELAY_LOCATIONS_POT_FILENAME)
+
+ print("Generating {}".format(output_path))
+
+ for country_code in countries:
+ country = countries[country_code]
+ entry = POEntry(
+ msgid=country["name"],
+ msgstr="",
+ comment=country_code.upper()
+ )
+ pot.append(entry)
+
+ cities = country["cities"]
+ for city_code in cities:
+ entry = POEntry(
+ msgid=cities[city_code],
+ msgstr="",
+ comment="{} {}".format(country_code.upper(), city_code.upper())
+ )
+
+ try:
+ pot.append(entry)
+ except ValueError as err:
+ print(c.orange("Cannot add an entry: {}".format(err)))
+
+ pot.save(output_path)
+
+
+def request_relays():
+ request = urllib.request.Request("https://api.mullvad.net/app/v1/relays")
+ with urllib.request.urlopen(request) as connection:
+ return json.load(connection)
+
+
+# Program main()
+
+def main():
+ # ensure output path exists
+ if not path.exists(OUT_DIR):
+ os.makedirs(OUT_DIR)
+
+ # ensure locales output path exists
+ if not path.exists(LOCALE_OUT_DIR):
+ os.makedirs(LOCALE_OUT_DIR)
+
+ # extract translations
+ extract_relay_translations()
+
+main()