# -*- coding: utf-8 -*- """Import timeslot exclude from manual featured on both hosts; push catalog.""" from __future__ import annotations import paramiko SCRIPT = r''' import json, urllib.request, http.cookiejar cj=http.cookiejar.CookieJar(); op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) req=urllib.request.Request("http://127.0.0.1:8014/api/auth/login", data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST") print("login", op.open(req, timeout=15).status) # import-manual req=urllib.request.Request("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude/import-manual", data=json.dumps({"merge":True}).encode(), headers={"Content-Type":"application/json"}, method="POST") raw=op.open(req, timeout=60).read() p=json.loads(raw) print("import added", p.get("added"), "count", p.get("count")) st=json.loads(op.open("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude", timeout=30).read()) print("exclude count", st.get("count"), "effective", st.get("effective_room_count"), "manual_extra", st.get("manual_featured_extra")) # push catalog if edge try: req=urllib.request.Request("http://127.0.0.1:8014/api/ops/neko/push-catalog", data=b"{}", headers={"Content-Type":"application/json"}, method="POST") print("push", op.open(req, timeout=90).read()[:400]) except Exception as e: print("push skip/err", type(e).__name__, e) # live-map lm=json.loads(op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=150).read()) print("live_count", lm.get("live_count"), "source", lm.get("source")) for k,v in sorted((lm.get("entries") or {}).items()): if v.get("is_live") and str(k).startswith("assign:"): print(" LIVE", (v.get("title") or k)[:28], v.get("source")) ''' def run(port, user, pwd, label, sudo=False): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", port, user, pwd, timeout=25) if sudo: sftp = c.open_sftp() with sftp.file("/tmp/_imp_ex.py", "w") as f: f.write(SCRIPT) sftp.close() cmd = f"echo '{pwd}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_imp_ex.py" _, o, e = c.exec_command(cmd, timeout=240) else: _, o, e = c.exec_command("/opt/douyinWEB/backend/.venv/bin/python -", timeout=240) o.channel.sendall(SCRIPT.encode()) o.channel.shutdown_write() print("====", label, "====") print(o.read().decode("utf-8", "replace")) err = e.read().decode("utf-8", "replace") if err.strip(): print(err[-600:]) c.close() if __name__ == "__main__": run(4014, "eric", "4669805", "phys", sudo=True) run(22, "root", "Xiao4669805", "vps")