#!/usr/bin/env python3 """Hent vejrdata for byer i Danmark via wttr.in (ingen dependencies).""" import json import urllib.request CITIES = ["Copenhagen", "Aarhus", "Odense", "Aalborg"] def get_weather(city: str) -> dict: url = f"https://wttr.in/{city}?format=j1" req = urllib.request.Request(url, headers={"User-Agent": "denmark-weather/1.0"}) with urllib.request.urlopen(req, timeout=10) as resp: return json.loads(resp.read()) def main(): print("=== Vejrdata for Danmark ===\n") for city in CITIES: try: data = get_weather(city) cur = data["current_condition"][0] temp = cur["temp_C"] feels = cur["FeelsLikeC"] humidity = cur["humidity"] wind_kmph = cur["windspeedKmph"] wind_dir = cur["winddir16Point"] desc = cur["weatherDesc"][0]["value"] precip = cur["precipMM"] print(f" {city}") print(f" Temperatur: {temp}°C (føles som {feels}°C)") print(f" Forhold: {desc}") print(f" Fugtighed: {humidity}%") print(f" Vind: {wind_kmph} km/t ({wind_dir})") print(f" Nedbør: {precip} mm") print() except Exception as e: print(f" {city}: fejl — {e}\n") print("Kilde: wttr.in") if __name__ == "__main__": main()