summaryrefslogtreecommitdiffhomepage
path: root/gui/scripts/fetch-relay-locations.py
diff options
context:
space:
mode:
authorOskar <oskar@mullvad.net>2024-11-05 07:57:08 +0100
committerOskar <oskar@mullvad.net>2024-11-14 16:43:18 +0100
commit84f14d79c4f0dde73337820ec94ba8ff928a3797 (patch)
treece468658e5ba7b0a74950c7ad1b09b3a4d00520b /gui/scripts/fetch-relay-locations.py
parente3ce0eb5cd0610dbff6ec98cb8cb388415c74bf6 (diff)
downloadmullvadvpn-84f14d79c4f0dde73337820ec94ba8ff928a3797.tar.xz
mullvadvpn-84f14d79c4f0dde73337820ec94ba8ff928a3797.zip
Move gui directory to desktop/packages/mullvad-vpn
Diffstat (limited to 'gui/scripts/fetch-relay-locations.py')
-rwxr-xr-xgui/scripts/fetch-relay-locations.py125
1 files changed, 0 insertions, 125 deletions
diff --git a/gui/scripts/fetch-relay-locations.py b/gui/scripts/fetch-relay-locations.py
deleted file mode 100755
index 374ec98601..0000000000
--- a/gui/scripts/fetch-relay-locations.py
+++ /dev/null
@@ -1,125 +0,0 @@
-#!/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 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()