publish eve_orehold_watcher.py
This commit is contained in:
parent
1be030a05b
commit
6193418310
1 changed files with 36 additions and 2 deletions
|
|
@ -204,6 +204,40 @@ def _discord(cp, title, message):
|
||||||
print(f"[discord] FAILED: {e}")
|
print(f"[discord] FAILED: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def _discord_live(cp, key, title, message):
|
||||||
|
"""Post-or-EDIT a single 'live' message so status updates IN PLACE (no spam).
|
||||||
|
Stores the message id per key in .live_<key>. Skips when muted."""
|
||||||
|
if bot_muted(cp):
|
||||||
|
return
|
||||||
|
webhook = cp.get("discord", "webhook", fallback="").strip() or DEFAULT_WEBHOOK
|
||||||
|
if not webhook:
|
||||||
|
return
|
||||||
|
idfile = os.path.join(HERE, f".live_{key}")
|
||||||
|
body = json.dumps({"content": f"**{title}**\n{message}"}).encode("utf-8")
|
||||||
|
hdr = {"Content-Type": "application/json", "User-Agent": "eve-watcher"}
|
||||||
|
mid = None
|
||||||
|
try:
|
||||||
|
mid = (open(idfile).read().strip() or None)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
if mid: # try to edit the existing message
|
||||||
|
try:
|
||||||
|
urllib.request.urlopen(urllib.request.Request(
|
||||||
|
f"{webhook}/messages/{mid}", data=body, headers=hdr, method="PATCH"),
|
||||||
|
timeout=10).read()
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
mid = None # message gone -> repost below
|
||||||
|
try:
|
||||||
|
resp = json.loads(urllib.request.urlopen(urllib.request.Request(
|
||||||
|
f"{webhook}?wait=true", data=body, headers=hdr, method="POST"),
|
||||||
|
timeout=10).read())
|
||||||
|
with open(idfile, "w") as f:
|
||||||
|
f.write(str(resp.get("id", "")))
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[discord-live] {e}")
|
||||||
|
|
||||||
|
|
||||||
def _ntfy(cp, title, message, priority, tags):
|
def _ntfy(cp, title, message, priority, tags):
|
||||||
server = cp.get("ntfy", "server", fallback="https://ntfy.sh").rstrip("/")
|
server = cp.get("ntfy", "server", fallback="https://ntfy.sh").rstrip("/")
|
||||||
topic = cp.get("ntfy", "topic", fallback="").strip()
|
topic = cp.get("ntfy", "topic", fallback="").strip()
|
||||||
|
|
@ -391,7 +425,7 @@ def run_ocr(cp):
|
||||||
tail = f"filling {rate_m3min:,.0f} m³/min · full <t:{full_at}:R>"
|
tail = f"filling {rate_m3min:,.0f} m³/min · full <t:{full_at}:R>"
|
||||||
else:
|
else:
|
||||||
tail = "not growing yet — lasers/drones idle?"
|
tail = "not growing yet — lasers/drones idle?"
|
||||||
_discord(cp, "⛏️ Ore hold",
|
_discord_live(cp, "hold", "⛏️ Ore hold",
|
||||||
f"{pct:.0f}% ({cur:,}/{cap:,} m³) · {tail}")
|
f"{pct:.0f}% ({cur:,}/{cap:,} m³) · {tail}")
|
||||||
last_status = time.time()
|
last_status = time.time()
|
||||||
was_mining = mining
|
was_mining = mining
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue