Files
cloudinit-bot 5dde3f0a10 feat(scripts): papra_open.py — open Papra link from MID (REQ-INB-48)
---ci---
project: inboxos
phase: 1
milestone: v1.4
status: execute
wave: 1
task: T8
---/ci---
2026-06-09 14:10:19 +00:00

76 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Open the Papra link for a given Message-ID.
v1.4 REQ-INB-48.
"""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
import requests
NOCODB_BASE_URL = os.environ.get("NOCODB_BASE_URL", "http://localhost:8080")
NOCODB_API_TOKEN = os.environ.get("NOCODB_API_TOKEN", "")
TABLE_IDS = {
"finance": os.environ.get("NOCODB_TABLE_FINANCE", ""),
"crm": os.environ.get("NOCODB_TABLE_CRM", ""),
"intel": os.environ.get("NOCODB_TABLE_INTEL", ""),
"promotions": os.environ.get("NOCODB_TABLE_PROMOTIONS", ""),
}
def _headers() -> dict[str, str]:
return {"xc-token": NOCODB_API_TOKEN, "Content-Type": "application/json"}
def lookup_papra_link(mid: str) -> tuple[str, str] | None:
if not NOCODB_API_TOKEN:
sys.stderr.write("NOCODB_API_TOKEN is not set\n")
return None
for module, table_id in TABLE_IDS.items():
if not table_id:
continue
try:
r = requests.get(
f"{NOCODB_BASE_URL}/api/v2/tables/{table_id}/records",
headers=_headers(),
params={"where": f"(Message-ID,eq,{mid})", "limit": 1},
timeout=10,
)
r.raise_for_status()
rows = r.json().get("list", [])
if rows:
link = rows[0].get("Papra_Link", "")
if link:
return (module, link)
except requests.RequestException:
continue
return None
def main() -> int:
p = argparse.ArgumentParser(description="Open Papra link for a Message-ID")
p.add_argument("--mid", required=True, help="Message-ID to look up")
args = p.parse_args()
result = lookup_papra_link(args.mid)
if not result:
sys.stderr.write(f"No Papra_Link found for MID={args.mid}\n")
return 1
module, link = result
print(f"Found in module={module}: {link}", file=sys.stderr)
if os.environ.get("DISPLAY"):
subprocess.run(["xdg-open", link], check=False)
else:
print(link)
return 0
if __name__ == "__main__":
raise SystemExit(main())