59 lines
2.3 KiB
Python
59 lines
2.3 KiB
Python
"""souz_pump: pick converted sessions -> upload to prod -> register -> sync keys. One cycle per call."""
|
|
import json, os, subprocess, sys, tarfile, time
|
|
|
|
BASE = r"C:\Users\User\tmp\tg_chat_grow"
|
|
OKF = os.path.join(BASE, "conv_sql_ok.json")
|
|
DONE = os.path.join(BASE, "souz_uploaded.txt")
|
|
TGZ = r"C:\Users\User\tmp\souz\pump.tgz"
|
|
KEY = r"C:\Users\User\.ssh\id_ed25519"
|
|
HOST = os.environ.get("SOUZ_SSH", "root@2.27.98.196")
|
|
SSH = ["ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=20", "-i", KEY, HOST]
|
|
SCP = ["scp", "-o", "StrictHostKeyChecking=no", "-i", KEY]
|
|
|
|
ENV = "TG_API_ID=23926822 TG_API_HASH=f383e16e2f55bc0857291510fd513002"
|
|
|
|
|
|
def log(*a):
|
|
print(time.strftime("%H:%M:%S"), *a, flush=True)
|
|
|
|
|
|
def main():
|
|
batch = int(sys.argv[1]) if len(sys.argv) > 1 else 250
|
|
if not os.path.exists(OKF):
|
|
log("no conv ok file yet")
|
|
return
|
|
ok = json.load(open(OKF, encoding="utf-8"))
|
|
done = set()
|
|
if os.path.exists(DONE):
|
|
done = set(x.strip() for x in open(DONE, encoding="utf-8") if x.strip())
|
|
new = [p for p in ok if p not in done and os.path.exists(p)][:batch]
|
|
log("converted=%d uploaded=%d new=%d" % (len(ok), len(done), len(new)))
|
|
if not new:
|
|
return
|
|
if os.path.exists(TGZ):
|
|
os.remove(TGZ)
|
|
t = tarfile.open(TGZ, "w:gz")
|
|
n = 0
|
|
for p in new:
|
|
t.add(p, arcname=os.path.basename(p))
|
|
n += 1
|
|
t.close()
|
|
size = os.path.getsize(TGZ)
|
|
log("packed %d (%.0f MB)" % (n, size / 1e6))
|
|
r = subprocess.run(SCP + [TGZ, HOST + ":/root/souz/pump.tgz"], capture_output=True, text=True)
|
|
if r.returncode != 0:
|
|
log("scp fail", r.stderr[-200:])
|
|
return
|
|
cmd = ("cd /root/souz && tar xzf pump.tgz -C sessions/ && cd sessions && ls *.session | python3 -c "
|
|
"'import sys,json;print(json.dumps([\"/root/souz/sessions/\"+l.strip() for l in sys.stdin]))' > pool_conv.json && "
|
|
"cd /root/souz && %s python3 souz_par.py %d 4 | tail -2 && python3 sync_keys.py && curl -s -m 5 http://127.0.0.1:48023/status" % (ENV, n + 30))
|
|
r = subprocess.run(SSH + [cmd], capture_output=True, text=True, timeout=3600)
|
|
log("remote:", (r.stdout or "").strip()[-300:])
|
|
if r.returncode == 0 and "DONE" in (r.stdout or ""):
|
|
with open(DONE, "a", encoding="utf-8") as f:
|
|
for p in new:
|
|
f.write(p + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |