#!/usr/bin/env -S uv run --script # /// script # dependencies = ["requests"] # /// import requests import json COUNTRIES = [ "Switzerland", "Germany", "Austria", "France" ] NR_ENTRIES = 15 def get_json_data(url): response = requests.get(url) # Check if request was successful if response.status_code == 200: return response.json() # Automatically parses JSON else: print(f"Error: {response.status_code}") return None def filter_json_data(data, countries=None, active=None): filtered = [] for url in data: include_item = True if url.get("country") not in COUNTRIES: include_item = False if url.get("protocol") != "https": include_item = False if url.get("score") is None: include_item = False if url.get("active") and include_item: filtered.append(url) return filtered def print_by_country(data): for country in COUNTRIES: printed = 0 print(f"Printing country: {country}") for url in data: if printed >= NR_ENTRIES: break if url.get("country") == country: print(f"Server = {url['url']}$repo/os/$arch {url['score']}") printed += 1 #print(f"printed: {printed} max: {NR_ENTRIES}") # Example usage url = "https://archlinux.org/mirrors/status/json/" data = get_json_data(url) all_urls = data['urls'] filtered_urls = filter_json_data(all_urls) sorted_urls = sorted(filtered_urls, key=lambda x: x["score"]) print_by_country(sorted_urls) #print(json.dumps(sorted_urls, indent=2))