askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
nc-feeds · 16.1 KB · 395 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """Weather and headlines for the desk: the dashboard's live lines, a page to |
| 3 | tap through (~/.cache/nc/today.html), and plain text for voice. |
| 4 | |
| 5 | nc-feeds lines full|compact|mini dashboard lines, from the cache (fast; conky runs it every |
| 6 | minute); an old cache is refreshed in the background |
| 7 | nc-feeds refresh fetch now (weather every 20 min, headlines every 30) |
| 8 | nc-feeds weather the weather as a sentence (voice) |
| 9 | nc-feeds news [N] the newest N headlines (voice) |
| 10 | nc-feeds place "Columbus, Ohio" where the weather is for |
| 11 | nc-feeds page open the Today page |
| 12 | |
| 13 | What is shown and from where are plain files the person edits, made from the |
| 14 | defaults in /etc/nc-dashboard/ the first time: |
| 15 | ~/.config/nc/dashboard.env WEATHER=on NEWS=on RADIO=on, PLACE=, UNITS= |
| 16 | ~/.config/nc/feeds.txt Name | https://.../rss.xml |
| 17 | Weather is Open-Meteo (free, no key); nothing about the person is sent but |
| 18 | the place name they chose. Items from feeds are shown as text, never run. |
| 19 | """ |
| 20 | import html, json, os, re, shutil, subprocess, sys, time, urllib.parse, urllib.request |
| 21 | import xml.etree.ElementTree as ET |
| 22 | from email.utils import parsedate_to_datetime |
| 23 | |
| 24 | HOME = os.path.expanduser("~") |
| 25 | CONF = f"{HOME}/.config/nc" |
| 26 | CACHE = f"{HOME}/.cache/nc" |
| 27 | DEFAULTS = "/etc/nc-dashboard" |
| 28 | UA = "network-computer-desk/1 (+https://github.com/askrobots/network-computer)" |
| 29 | WEATHER_EVERY, NEWS_EVERY = 20 * 60, 30 * 60 |
| 30 | |
| 31 | # WMO weather codes, in words |
| 32 | WMO = {0: "clear", 1: "mostly clear", 2: "partly cloudy", 3: "cloudy", 45: "fog", 48: "freezing fog", |
| 33 | 51: "light drizzle", 53: "drizzle", 55: "heavy drizzle", 56: "freezing drizzle", 57: "freezing drizzle", |
| 34 | 61: "light rain", 63: "rain", 65: "heavy rain", 66: "freezing rain", 67: "freezing rain", |
| 35 | 71: "light snow", 73: "snow", 75: "heavy snow", 77: "snow grains", 80: "showers", 81: "showers", |
| 36 | 82: "heavy showers", 85: "snow showers", 86: "snow showers", 95: "thunderstorms", |
| 37 | 96: "thunderstorms with hail", 99: "thunderstorms with hail"} |
| 38 | |
| 39 | |
| 40 | def setup(): |
| 41 | os.makedirs(CONF, exist_ok=True) |
| 42 | os.makedirs(CACHE, exist_ok=True) |
| 43 | for f in ("dashboard.env", "feeds.txt", "stations.txt"): |
| 44 | if not os.path.exists(f"{CONF}/{f}") and os.path.exists(f"{DEFAULTS}/{f}"): |
| 45 | shutil.copyfile(f"{DEFAULTS}/{f}", f"{CONF}/{f}") |
| 46 | |
| 47 | |
| 48 | def settings(): |
| 49 | s = {"WEATHER": "on", "NEWS": "on", "RADIO": "on", "PLACE": "", "UNITS": ""} |
| 50 | try: |
| 51 | for line in open(f"{CONF}/dashboard.env"): |
| 52 | m = re.match(r"\s*([A-Z_]+)\s*=\s*(.*?)\s*$", line) |
| 53 | if m: |
| 54 | s[m.group(1)] = m.group(2).split(" #")[0].strip().strip('"\'') |
| 55 | except OSError: |
| 56 | pass |
| 57 | return s |
| 58 | |
| 59 | |
| 60 | def on(s, key): |
| 61 | return s.get(key, "on").lower() not in ("off", "no", "false", "0") |
| 62 | |
| 63 | |
| 64 | def set_setting(key, value): |
| 65 | path = f"{CONF}/dashboard.env" |
| 66 | lines = open(path).read().splitlines() if os.path.exists(path) else [] |
| 67 | out, done = [], False |
| 68 | for line in lines: |
| 69 | if re.match(rf"\s*{key}\s*=", line): |
| 70 | out.append(f'{key}="{value}"') |
| 71 | done = True |
| 72 | else: |
| 73 | out.append(line) |
| 74 | if not done: |
| 75 | out.append(f'{key}="{value}"') |
| 76 | open(path, "w").write("\n".join(out) + "\n") |
| 77 | |
| 78 | |
| 79 | def get(url, timeout=10): |
| 80 | req = urllib.request.Request(url, headers={"User-Agent": UA}) |
| 81 | with urllib.request.urlopen(req, timeout=timeout) as r: |
| 82 | return r.read() |
| 83 | |
| 84 | |
| 85 | def load(name): |
| 86 | try: |
| 87 | return json.load(open(f"{CACHE}/{name}.json")) |
| 88 | except (OSError, ValueError): |
| 89 | return {} |
| 90 | |
| 91 | |
| 92 | def save(name, data): |
| 93 | tmp = f"{CACHE}/{name}.json.tmp" |
| 94 | json.dump(data, open(tmp, "w")) |
| 95 | os.replace(tmp, f"{CACHE}/{name}.json") |
| 96 | |
| 97 | |
| 98 | # --- weather --------------------------------------------------------------- |
| 99 | |
| 100 | def geocode(place): |
| 101 | """'Columbus, Ohio' or '39.96,-83.0' -> (name, lat, lon, country).""" |
| 102 | m = re.fullmatch(r"\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*", place) |
| 103 | if m: |
| 104 | return place, float(m.group(1)), float(m.group(2)), "" |
| 105 | name, _, region = place.partition(",") |
| 106 | q = urllib.parse.urlencode({"name": name.strip(), "count": 10, "language": "en"}) |
| 107 | hits = json.loads(get(f"https://geocoding-api.open-meteo.com/v1/search?{q}")).get("results") or [] |
| 108 | region = region.strip().lower() |
| 109 | if region: # "Columbus, Ohio" / "Paris, FR": prefer the one in that region or country |
| 110 | hits = [h for h in hits if region in (h.get("admin1", "") + " " + h.get("country", "") + " " |
| 111 | + h.get("country_code", "")).lower()] or hits |
| 112 | if not hits: |
| 113 | raise RuntimeError(f"no place called {place}") |
| 114 | h = hits[0] |
| 115 | return h["name"], h["latitude"], h["longitude"], h.get("country_code", "") |
| 116 | |
| 117 | |
| 118 | def refresh_weather(s): |
| 119 | place = s.get("PLACE", "") |
| 120 | if not place: |
| 121 | save("weather", {"at": time.time(), "error": "no place"}) |
| 122 | return |
| 123 | old = load("weather") |
| 124 | if old.get("place") == place and old.get("lat") is not None: |
| 125 | name, lat, lon, cc = old["name"], old["lat"], old["lon"], old.get("cc", "") |
| 126 | else: |
| 127 | name, lat, lon, cc = geocode(place) |
| 128 | units = (s.get("UNITS") or ("us" if cc in ("US", "LR", "MM") else "metric")).lower() |
| 129 | imperial = units.startswith(("us", "imp", "f")) |
| 130 | q = urllib.parse.urlencode({ |
| 131 | "latitude": lat, "longitude": lon, "timezone": "auto", "forecast_days": 2, |
| 132 | "current": "temperature_2m,apparent_temperature,weather_code,wind_speed_10m,precipitation", |
| 133 | "daily": "temperature_2m_max,temperature_2m_min,precipitation_probability_max,weather_code", |
| 134 | **({"temperature_unit": "fahrenheit", "wind_speed_unit": "mph", "precipitation_unit": "inch"} |
| 135 | if imperial else {"wind_speed_unit": "kmh"})}) |
| 136 | d = json.loads(get(f"https://api.open-meteo.com/v1/forecast?{q}")) |
| 137 | c, day = d["current"], d["daily"] |
| 138 | save("weather", { |
| 139 | "at": time.time(), "place": place, "name": name, "lat": lat, "lon": lon, "cc": cc, |
| 140 | "deg": "°F" if imperial else "°C", "wind": "mph" if imperial else "km/h", |
| 141 | "now": {"t": round(c["temperature_2m"]), "feels": round(c["apparent_temperature"]), |
| 142 | "sky": WMO.get(c["weather_code"], "?"), "wind": round(c["wind_speed_10m"])}, |
| 143 | "days": [{"hi": round(day["temperature_2m_max"][i]), "lo": round(day["temperature_2m_min"][i]), |
| 144 | "rain": day["precipitation_probability_max"][i] or 0, |
| 145 | "sky": WMO.get(day["weather_code"][i], "?")} for i in range(len(day["time"]))], |
| 146 | }) |
| 147 | |
| 148 | |
| 149 | def weather_text(w, short=False): |
| 150 | if not w or w.get("error") == "no place": |
| 151 | return None |
| 152 | if "now" not in w: |
| 153 | return f"weather: {w.get('error', 'not yet')}" |
| 154 | n, t = w["now"], w["days"][0] |
| 155 | if short: |
| 156 | return f"{n['t']}{w['deg']} {n['sky']}, {t['hi']}/{t['lo']}" |
| 157 | return (f"{w['name']}: {n['t']}{w['deg']} {n['sky']}, high {t['hi']} low {t['lo']}" |
| 158 | + (f", rain {t['rain']}%" if t["rain"] >= 20 else "")) |
| 159 | |
| 160 | |
| 161 | # --- headlines ------------------------------------------------------------- |
| 162 | |
| 163 | def feeds(): |
| 164 | out = [] |
| 165 | try: |
| 166 | for line in open(f"{CONF}/feeds.txt"): |
| 167 | line = line.strip() |
| 168 | if not line or line.startswith("#"): |
| 169 | continue |
| 170 | name, _, url = line.rpartition("|") |
| 171 | out.append(((name or url).strip(), url.strip())) |
| 172 | except OSError: |
| 173 | pass |
| 174 | return out |
| 175 | |
| 176 | |
| 177 | def text_of(el): |
| 178 | t = "".join(el.itertext()) if el is not None else "" |
| 179 | return re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", t))).strip() |
| 180 | |
| 181 | |
| 182 | def when(s): |
| 183 | try: |
| 184 | return parsedate_to_datetime(s).timestamp() |
| 185 | except (TypeError, ValueError): |
| 186 | pass |
| 187 | try: |
| 188 | from datetime import datetime |
| 189 | return datetime.fromisoformat(s.strip().replace("Z", "+00:00")).timestamp() |
| 190 | except (AttributeError, ValueError): |
| 191 | return 0 |
| 192 | |
| 193 | |
| 194 | def parse_feed(data, source): |
| 195 | root = ET.fromstring(data) |
| 196 | items = [] |
| 197 | for it in root.iter(): |
| 198 | tag = it.tag.rsplit("}", 1)[-1] |
| 199 | if tag not in ("item", "entry"): |
| 200 | continue |
| 201 | f = {c.tag.rsplit("}", 1)[-1]: c for c in it} |
| 202 | link = f.get("link") |
| 203 | href = (link.get("href") or link.text or "").strip() if link is not None else "" |
| 204 | title = text_of(f.get("title")) |
| 205 | if not title or not href.startswith(("http://", "https://")): |
| 206 | continue |
| 207 | date = next((f[k].text for k in ("pubDate", "published", "updated", "date") |
| 208 | if f.get(k) is not None and f[k].text), "") |
| 209 | items.append({"title": title[:200], "link": href, "source": source, "at": when(date)}) |
| 210 | return items[:15] |
| 211 | |
| 212 | |
| 213 | def refresh_news(): |
| 214 | items, errors = [], [] |
| 215 | for name, url in feeds(): |
| 216 | try: |
| 217 | items += parse_feed(get(url), name) |
| 218 | except Exception as e: # one feed down must not empty the rest |
| 219 | errors.append(f"{name}: {e.__class__.__name__}") |
| 220 | save("news", {"at": time.time(), "items": turns(items)[:60], "errors": errors}) |
| 221 | |
| 222 | |
| 223 | def turns(items): |
| 224 | """Newest first, but every source in turn: one busy feed must not fill the |
| 225 | five lines the dashboard has.""" |
| 226 | by = {} |
| 227 | for i in sorted(items, key=lambda i: -i["at"]): |
| 228 | by.setdefault(i["source"], []).append(i) |
| 229 | out = [] |
| 230 | while any(by.values()): |
| 231 | rnd = [q.pop(0) for q in by.values() if q] |
| 232 | out += sorted(rnd, key=lambda i: -i["at"]) |
| 233 | return out |
| 234 | |
| 235 | |
| 236 | # --- the page and the dashboard ----------------------------------------------- |
| 237 | |
| 238 | def radio_now(): |
| 239 | try: |
| 240 | return open(f"{os.environ.get('XDG_RUNTIME_DIR', '/tmp')}/nc-radio/now").read().strip() |
| 241 | except OSError: |
| 242 | return "" |
| 243 | |
| 244 | |
| 245 | def ago(t): |
| 246 | m = int((time.time() - t) / 60) if t else None |
| 247 | if m is None: |
| 248 | return "" |
| 249 | return f"{m} min ago" if m < 60 else f"{m // 60} h ago" if m < 48 * 60 else f"{m // 1440} days ago" |
| 250 | |
| 251 | |
| 252 | def write_page(s): |
| 253 | w, n = load("weather"), load("news") |
| 254 | e = html.escape |
| 255 | parts = ['<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">', |
| 256 | "<title>Today</title><style>body{font:17px/1.45 system-ui,sans-serif;max-width:46rem;margin:0 auto;" |
| 257 | "padding:1rem 16px;background:#fbfbfa;color:#1d2330}h1{font-size:1.5rem;margin:.2rem 0 1rem}" |
| 258 | "h2{font-size:1rem;text-transform:uppercase;letter-spacing:.06em;color:#5b6577;margin:1.6rem 0 .4rem}" |
| 259 | "a{color:#1f4fbf;text-decoration:none}li{margin:.55rem 0}ul{padding-left:1.1rem}small{color:#6b7280}" |
| 260 | "@media (prefers-color-scheme:dark){body{background:#15181e;color:#dfe4ec}a{color:#8fb4ff}h2{color:#94a0b4}}" |
| 261 | "</style>", "<h1>Today</h1>"] |
| 262 | if on(s, "WEATHER"): |
| 263 | parts.append("<h2>Weather</h2>") |
| 264 | if "now" in w: |
| 265 | parts.append(f"<p><strong>{e(weather_text(w))}</strong><br><small>feels like {w['now']['feels']}{w['deg']}, " |
| 266 | f"wind {w['now']['wind']} {w['wind']}</small></p>") |
| 267 | if len(w["days"]) > 1: |
| 268 | t = w["days"][1] |
| 269 | parts.append(f"<p>Tomorrow: {e(t['sky'])}, high {t['hi']} low {t['lo']}" |
| 270 | + (f", rain {t['rain']}%" if t["rain"] >= 20 else "") + "</p>") |
| 271 | else: |
| 272 | parts.append('<p>Say "weather for" and your town, or put PLACE="your town" in ~/.config/nc/dashboard.env.</p>') |
| 273 | if on(s, "RADIO"): |
| 274 | now = radio_now() |
| 275 | parts.append("<h2>Radio</h2><p>" + (f"Playing {e(now)}. " if now else "") |
| 276 | + 'Tap the Radio icon on the desktop, or say "play some jazz" or "stop the radio".</p>') |
| 277 | if on(s, "NEWS"): |
| 278 | parts.append("<h2>Headlines</h2><ul>") |
| 279 | for i in n.get("items", [])[:40]: |
| 280 | parts.append(f'<li><a href="{e(i["link"])}">{e(i["title"])}</a><br><small>{e(i["source"])}' |
| 281 | f'{" · " + ago(i["at"]) if i["at"] else ""}</small></li>') |
| 282 | parts.append("</ul>") |
| 283 | if n.get("errors"): |
| 284 | parts.append(f"<p><small>Not answering: {e(', '.join(n['errors']))}</small></p>") |
| 285 | parts.append("<p><small>Feeds: ~/.config/nc/feeds.txt</small></p>") |
| 286 | tmp = f"{CACHE}/today.html.tmp" |
| 287 | open(tmp, "w").write("\n".join(parts)) |
| 288 | os.replace(tmp, f"{CACHE}/today.html") |
| 289 | |
| 290 | |
| 291 | def refresh(force=False): |
| 292 | s = settings() |
| 293 | if on(s, "WEATHER") and (force or time.time() - load("weather").get("at", 0) > WEATHER_EVERY |
| 294 | or load("weather").get("place") != s.get("PLACE")): |
| 295 | try: |
| 296 | refresh_weather(s) |
| 297 | except Exception as e: |
| 298 | old = load("weather") |
| 299 | old.update({"at": time.time(), "error": str(e)[:80]}) |
| 300 | save("weather", old) |
| 301 | if on(s, "NEWS") and (force or time.time() - load("news").get("at", 0) > NEWS_EVERY): |
| 302 | refresh_news() |
| 303 | write_page(s) |
| 304 | |
| 305 | |
| 306 | def refresh_later(): |
| 307 | """The dashboard must not wait for the network: fetch in the background, once.""" |
| 308 | lock = f"{CACHE}/refresh.lock" |
| 309 | try: |
| 310 | if time.time() - os.path.getmtime(lock) < 120: |
| 311 | return |
| 312 | except OSError: |
| 313 | pass |
| 314 | open(lock, "w").close() |
| 315 | subprocess.Popen([sys.argv[0], "refresh"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 316 | start_new_session=True) |
| 317 | |
| 318 | |
| 319 | def cut(t, n): |
| 320 | return t if len(t) <= n else t[: n - 1].rstrip() + "…" |
| 321 | |
| 322 | |
| 323 | def lines(mode): |
| 324 | s = settings() |
| 325 | w, n = load("weather"), load("news") |
| 326 | stale = (on(s, "WEATHER") and time.time() - w.get("at", 0) > WEATHER_EVERY) or \ |
| 327 | (on(s, "NEWS") and time.time() - n.get("at", 0) > NEWS_EVERY) or \ |
| 328 | w.get("place", "") != s.get("PLACE", "") and on(s, "WEATHER") |
| 329 | if stale: |
| 330 | refresh_later() |
| 331 | out = [] |
| 332 | now = radio_now() if on(s, "RADIO") else "" |
| 333 | wt = weather_text(w, short=mode != "full") if on(s, "WEATHER") else None |
| 334 | if mode == "mini": # a phone: one line |
| 335 | bits = [b for b in (wt, f"♪ {now}" if now else "") if b] |
| 336 | return [cut(" ".join(bits), 44)] if bits else [] |
| 337 | if mode == "compact": |
| 338 | if wt: |
| 339 | out.append(cut(wt, 46)) |
| 340 | if now: |
| 341 | out.append(cut(f"♪ {now}", 46)) |
| 342 | return out |
| 343 | head = "${color2}TODAY${color}" |
| 344 | if on(s, "WEATHER"): |
| 345 | out.append(cut(wt, 46) if wt else 'weather: say "weather for <town>"') |
| 346 | if "now" in w and len(w["days"]) > 1: |
| 347 | t = w["days"][1] |
| 348 | out.append(cut(f"tomorrow {t['sky']}, {t['hi']}/{t['lo']}" + (f", rain {t['rain']}%" if t["rain"] >= 20 else ""), 46)) |
| 349 | if now: |
| 350 | out.append(cut(f"♪ {now}", 46)) |
| 351 | if on(s, "NEWS"): |
| 352 | for i in n.get("items", [])[:5]: |
| 353 | out.append("${color1}·${color} " + cut(i["title"], 44).replace("$", "$$").replace("#", "\\#")) |
| 354 | return [head] + out if out else [] |
| 355 | |
| 356 | |
| 357 | def main(): |
| 358 | setup() |
| 359 | cmd = sys.argv[1] if len(sys.argv) > 1 else "lines" |
| 360 | if cmd == "lines": |
| 361 | print("\n".join(lines(sys.argv[2] if len(sys.argv) > 2 else "full"))) |
| 362 | elif cmd == "refresh": |
| 363 | refresh(force="--force" in sys.argv) |
| 364 | elif cmd == "weather": |
| 365 | s = settings() |
| 366 | if not s.get("PLACE"): |
| 367 | print('I do not know where you are yet: say "weather for" and your town.') |
| 368 | return |
| 369 | if time.time() - load("weather").get("at", 0) > 300 or load("weather").get("place") != s["PLACE"]: |
| 370 | refresh_weather(s) |
| 371 | w = load("weather") |
| 372 | print(weather_text(w) + (f". Tomorrow {w['days'][1]['sky']}, high {w['days'][1]['hi']}." |
| 373 | if len(w.get("days", [])) > 1 else "")) |
| 374 | elif cmd == "news": |
| 375 | if time.time() - load("news").get("at", 0) > NEWS_EVERY: |
| 376 | refresh_news() |
| 377 | k = int(sys.argv[2]) if len(sys.argv) > 2 else 5 |
| 378 | for i in load("news").get("items", [])[:k]: |
| 379 | print(f"{i['title']} ({i['source']})") |
| 380 | elif cmd == "place": |
| 381 | place = " ".join(sys.argv[2:]).strip() |
| 382 | name = geocode(place)[0] if place else "" |
| 383 | set_setting("PLACE", place) |
| 384 | refresh(force=True) |
| 385 | print(f"weather for {name}" if place else "no place: weather is off until one is set") |
| 386 | elif cmd == "page": |
| 387 | refresh() |
| 388 | subprocess.Popen(["xdg-open", f"{CACHE}/today.html"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 389 | else: |
| 390 | print(__doc__) |
| 391 | sys.exit(2) |
| 392 | |
| 393 | |
| 394 | if __name__ == "__main__": |
| 395 | main() |