aboutsummaryrefslogtreecommitdiffstats
path: root/hyprsunset_qt
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-07-17 10:26:48 +0200
committerDanilo M. <danix@danix.xyz>2026-07-17 10:26:48 +0200
commitfc4b8ae587fa5a26648c2bb94e61141d2954f9aa (patch)
tree775bd28ab376dcb819df31a164f268093c4bb1e3 /hyprsunset_qt
parent1b949b536dee4039625218ed7de0634a94e7fa86 (diff)
downloadhyprsunset-qt-fc4b8ae587fa5a26648c2bb94e61141d2954f9aa.tar.gz
hyprsunset-qt-fc4b8ae587fa5a26648c2bb94e61141d2954f9aa.zip
feat: geolocate and sunrise-sunset fetch
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'hyprsunset_qt')
-rw-r--r--hyprsunset_qt/sun.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/hyprsunset_qt/sun.py b/hyprsunset_qt/sun.py
index bc611f9..254fa38 100644
--- a/hyprsunset_qt/sun.py
+++ b/hyprsunset_qt/sun.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import json
+import urllib.request
from datetime import datetime, tzinfo
from pathlib import Path
from typing import Optional
@@ -29,3 +30,24 @@ def write_cache(payload: dict, path: Path) -> None:
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, indent=2))
+
+
+GEO_URL = "http://ip-api.com/json"
+SUN_URL = "https://api.sunrise-sunset.org/json?lat={lat}&lng={lon}&formatted=0"
+
+
+def _http_get(url: str, timeout: float = 10.0) -> str:
+ with urllib.request.urlopen(url, timeout=timeout) as resp:
+ return resp.read().decode()
+
+
+def geolocate() -> tuple[str, str]:
+ """Return (lat, lon) as strings from IP geolocation."""
+ data = json.loads(_http_get(GEO_URL))
+ return str(data["lat"]), str(data["lon"])
+
+
+def fetch_sun(lat: str, lon: str) -> dict:
+ """Fetch sunrise/sunset JSON for coords. Caller handles caching."""
+ url = SUN_URL.format(lat=lat, lon=lon)
+ return json.loads(_http_get(url))