-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.py
More file actions
executable file
·90 lines (75 loc) · 2.76 KB
/
Copy pathfetch.py
File metadata and controls
executable file
·90 lines (75 loc) · 2.76 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""Fetch current weather + 24h forecast for Indian cities via Open-Meteo.
Open-Meteo is free, no API key, AGPLv3 server (self-hostable), CC BY 4.0 data.
Docs: https://open-meteo.com/en/docs
"""
from __future__ import annotations
import argparse
import json
import sys
import urllib.parse
import urllib.request
from pathlib import Path
API = "https://api.open-meteo.com/v1/forecast"
CITIES_FILE = Path(__file__).parent / "cities.json"
CURRENT_FIELDS = [
"temperature_2m",
"relative_humidity_2m",
"apparent_temperature",
"precipitation",
"weather_code",
"wind_speed_10m",
]
HOURLY_FIELDS = ["temperature_2m", "precipitation_probability"]
def fetch(cities: list[dict], hours: int = 24) -> dict:
lats = ",".join(f"{c['lat']:.4f}" for c in cities)
lons = ",".join(f"{c['lon']:.4f}" for c in cities)
q = urllib.parse.urlencode({
"latitude": lats,
"longitude": lons,
"current": ",".join(CURRENT_FIELDS),
"hourly": ",".join(HOURLY_FIELDS),
"forecast_hours": hours,
"timezone": "Asia/Kolkata",
})
with urllib.request.urlopen(f"{API}?{q}", timeout=30) as r:
data = json.loads(r.read())
return data if isinstance(data, list) else [data]
def print_table(cities: list[dict], results: list[dict]) -> None:
print(f"{'City':<22} {'State':<22} {'Temp °C':>8} {'Feels':>7} {'RH %':>6} {'Wind':>6}")
print("-" * 75)
for city, res in zip(cities, results):
cur = res.get("current", {})
print(
f"{city['name']:<22} {city['state']:<22} "
f"{cur.get('temperature_2m', '?'):>8} "
f"{cur.get('apparent_temperature', '?'):>7} "
f"{cur.get('relative_humidity_2m', '?'):>6} "
f"{cur.get('wind_speed_10m', '?'):>6}"
)
def main() -> int:
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--city", help="Filter to a single city (case-insensitive substring)")
p.add_argument("--json", action="store_true", help="Emit raw JSON instead of table")
p.add_argument("--hours", type=int, default=24, help="Forecast hours (default 24)")
args = p.parse_args()
cities = json.loads(CITIES_FILE.read_text())
if args.city:
needle = args.city.lower()
cities = [c for c in cities if needle in c["name"].lower()]
if not cities:
print(f"no city matched '{args.city}'", file=sys.stderr)
return 1
results = fetch(cities, hours=args.hours)
if args.json:
json.dump(
[{"city": c, "weather": r} for c, r in zip(cities, results)],
sys.stdout,
separators=(",", ":"),
)
print()
else:
print_table(cities, results)
return 0
if __name__ == "__main__":
sys.exit(main())