# -*- coding: utf-8 -*- """Copy timeslot exclude list from VPS → physical for dual-deploy parity.""" from __future__ import annotations import json import urllib.request import http.cookiejar import paramiko def api(port, user, pwd, sudo=False): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", port, user, pwd, timeout=25) return c GET_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") op.open(req, timeout=15) p=json.loads(op.open("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude", timeout=30).read()) print(json.dumps({"count":p.get("count"),"text":p.get("text") or ""}, ensure_ascii=False)) ''' SET_TMPL = r''' import json,urllib.request,http.cookiejar TEXT = %r 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") op.open(req, timeout=15) req=urllib.request.Request("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude", data=json.dumps({"text": TEXT}).encode(), headers={"Content-Type":"application/json"}, method="POST") p=json.loads(op.open(req, timeout=60).read()) print("saved", p.get("count"), "dup", p.get("skipped_dup"), "invalid", p.get("skipped_invalid")) st=json.loads(op.open("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude", timeout=30).read()) print("now", st.get("count"), "effective", st.get("effective_room_count")) ''' def py_run(c, code, sudo_pw=None): if sudo_pw: sftp = c.open_sftp() with sftp.file("/tmp/_exsync.py", "w") as f: f.write(code) sftp.close() _, o, e = c.exec_command( f"echo '{sudo_pw}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_exsync.py", timeout=90, ) else: _, o, e = c.exec_command("/opt/douyinWEB/backend/.venv/bin/python -", timeout=90) o.channel.sendall(code.encode()) o.channel.shutdown_write() out = o.read().decode("utf-8", "replace") err = e.read().decode("utf-8", "replace") return out, err def main(): # get from VPS v = api(22, "root", "Xiao4669805") out, err = py_run(v, GET_SCRIPT) v.close() data = json.loads(out.strip().splitlines()[-1]) text = data.get("text") or "" print("vps exclude count", data.get("count"), "chars", len(text)) if not text.strip(): print("nothing to sync") return # set on physical p = api(4014, "eric", "4669805") out2, err2 = py_run(p, SET_TMPL % text, sudo_pw="4669805") print(out2) if err2.strip(): print(err2[-400:]) p.close() if __name__ == "__main__": main()