Files
dotfiles/hosts/mainframe/bin/movie_recs_notify
T
connor e6644d0616 Rebuild dotfiles as one branch with per-host layers
Replaces the previous repo, which had split into two histories that never
met (mainframe on a dead GitLab remote, the laptops on Gitea) with 146
dirty files across three machines and the NAS never enrolled at all.

Branch-per-machine is gone. One main, with host differences expressed as
small files under hosts/<hostname>/ rather than as branches, so there is
nothing to merge. The reconciled zsh layer reduces 15-33 line forks to
1-7 effective lines per host; distro differences (oh-my-zsh prefix,
syntax-highlighting path, fd vs fdfind) are probed in common/ instead.

Fresh history: the old one carried six plaintext credentials, 45 MB of
mail caches, browser caches and vendored binaries. 5,096 tracked files
and 144 MB become 462 files and 2.6 MB. The .gitignore is now an
allowlist, which is what keeps that true.

Root cause of the rot: ~/.local/bin was a symlink to scripts/ with GOPATH
inside it, so every go install wrote into version control (2.2 GB on the
work laptop). PATH now points at the repo instead of the reverse.

Also: Hyprland replaces sway and is sourced in two halves so $browser is
defined before use; singleton automations carry ConditionHost= alongside
host-layer-only placement; ddns moves from cron to a guarded timer;
package manifests and pkg-snapshot/pkg-restore replace the X11-era
install_scripts/; networkmanager-dmenu added to system76 (the binding
always existed, the package never did).
2026-09-14 14:24:37 -04:00

110 lines
3.6 KiB
Python
Executable File

#!/usr/bin/python3
"""Run movie_recs and push the result to ntfy as a markdown notification.
Intended for the movie-recs.timer systemd unit. Failures are pushed too --
a recommendation job that silently stops producing is worse than a noisy one.
"""
import base64
import json
import os
import subprocess
import sys
import urllib.request
NTFY = "https://ntfy.rcjohnstone.com/movies"
# ntfy is auth-default-access: deny-all, so publishing needs the `bot`
# credential. Mirrored from the rbw entry `ntfy-bot`; rbw is the source of
# truth. Kept in a 0600 file because this runs unattended from a timer.
NTFY_ENV = os.path.expanduser("~/.config/ntfy/publish.env")
RECS = "/home/connor/.local/bin/movie_recs"
COUNT = "10"
def _auth_header():
user, pw = os.environ.get("NTFY_USER"), os.environ.get("NTFY_PASS")
if not (user and pw):
try:
with open(NTFY_ENV) as fh:
vals = dict(
line.strip().split("=", 1)
for line in fh if "=" in line and not line.startswith("#"))
user, pw = vals.get("NTFY_USER"), vals.get("NTFY_PASS")
except OSError:
return None
if not (user and pw):
return None
token = base64.b64encode(f"{user}:{pw}".encode()).decode()
return f"Basic {token}"
def push(title, body, tags, priority="default", markdown=True):
headers = {
"Title": title,
"Tags": tags,
"Priority": priority,
"Content-Type": "text/plain; charset=utf-8",
}
auth = _auth_header()
if auth:
headers["Authorization"] = auth
if markdown:
headers["Markdown"] = "yes"
req = urllib.request.Request(
NTFY, data=body.encode("utf-8"), headers=headers, method="POST")
with urllib.request.urlopen(req, timeout=30) as r:
return r.status
def main():
try:
proc = subprocess.run(
[RECS, "-n", COUNT, "--json"],
capture_output=True, text=True, timeout=2400)
except subprocess.TimeoutExpired:
push("Movie recs failed", "movie_recs timed out after 40 minutes.",
"warning", "high", markdown=False)
return 1
if proc.returncode != 0:
tail = (proc.stderr or "no stderr").strip().splitlines()[-6:]
push("Movie recs failed",
"exit %d\n\n%s" % (proc.returncode, "\n".join(tail)),
"warning", "high", markdown=False)
return 1
try:
data = json.loads(proc.stdout)
except json.JSONDecodeError:
push("Movie recs failed", "could not parse output as JSON",
"warning", "high", markdown=False)
return 1
recs = data.get("recommendations", [])
if not recs:
push("Movie recs: nothing today",
"The run completed but every candidate was filtered out.",
"warning", "default", markdown=False)
return 0
lines = []
for r in recs:
t = r["tmdb"]
lines.append("**%s** (%s) · %s/10, %sk votes"
% (t["title"], t["year"], t["score"], t["votes"] // 1000))
lines.append("%s" % r.get("reason", "").strip())
lines.append("*from: %s*" % ", ".join(r.get("anchors", [])))
lines.append("")
dropped = (len(data.get("rejected_not_in_tmdb", []))
+ len(data.get("rejected_already_owned", []))
+ len(data.get("rejected_below_min_votes", []))
+ len(data.get("rejected_invented_cluster", [])))
lines.append("_%d shown, %d candidates filtered_" % (len(recs), dropped))
push("%d film recommendations" % len(recs), "\n".join(lines), "clapper")
return 0
if __name__ == "__main__":
sys.exit(main())