# -*- coding: utf-8 -*- """Ротация ZTE-мобильного + фарм Theos: 5 аккаунтов на каждый новый egress. python rotate_farm.py [max_rounds] [per_ip] """ import json import subprocess import sys import time import dpcode as D ROT = r"C:/Users/User/tmp/zte_rotate3.py" PY = r"C:/Users/User/AppData/Local/Programs/Python/Python311/python.exe" ZTE = "http://127.0.0.1:8899" LOG = open("rotate_farm.log", "a", encoding="utf-8") def log(*a): s = " ".join(str(x) for x in a) print(s, flush=True) LOG.write(s + "\n") LOG.flush() def egress(px=ZTE, tries=3): for _ in range(tries): try: r = subprocess.run(["curl", "-s", "-m", "10", "-x", px, "https://api.ipify.org"], capture_output=True, text=True, timeout=20) ip = (r.stdout or "").strip() if ip.count(".") == 3: return ip except Exception: pass time.sleep(2) return "" def rotate(): try: subprocess.run([PY, ROT], capture_output=True, text=True, timeout=120, encoding="utf-8", errors="replace", env={"PATH": "C:/Windows/System32;/c/Windows", "PYTHONIOENCODING": "utf-8"}) return True except Exception as e: log("rotate err", str(e)[:80]) return False def zero(): recs = {} for line in open(D.BANK, encoding="utf-8"): line = line.strip() if not line: continue r = json.loads(line) if r.get("cookies"): recs[r["email"]] = r out = [] for r in recs.values(): t = r.get("theos") or {} try: if float(t.get("balance") or 0) <= 0 and float(t.get("granted") or 0) <= 0: out.append(r) except Exception: pass out.sort(key=lambda r: r.get("ts") or 0) return out def grant_batch(px, batch): got = 0.0 ok = 0 for rec in batch: email = rec["email"] machine = rec.get("machine") or D.machine_header(salt=email) s = D.session_from(rec) s.proxies.update({"http": px, "https": px}) try: st, d = D.theos(s, machine) except Exception as e: log(" %-38s ERR %s" % (email, str(e)[:50])) continue if not isinstance(d, dict): log(" %-38s http=%s %s" % (email, st, str(d)[:50])) continue try: bal = float(d.get("balance") or 0) except Exception: bal = 0.0 rec["theos"] = d D.append_bank(rec) if bal > 0: ok += 1 got += bal log(" %-38s +%.2f" % (email, bal)) else: log(" %-38s zero" % email) return ok, got def main(): rounds = int(sys.argv[1]) if len(sys.argv) > 1 else 12 per_ip = int(sys.argv[2]) if len(sys.argv) > 2 else 5 seen = set() total = 0.0 for i in range(rounds): todo = zero() if not todo: log("нулевых не осталось — стоп") break log("round %d: zero=%d, egress before=%s" % (i, len(todo), egress())) rotate() time.sleep(35) ip = egress(ZTE, tries=6) if not ip: log(" нет egress — пропуск") continue if ip in seen: log(" ip %s уже использован — ещё ротация" % ip) continue seen.add(ip) batch = todo[:per_ip] ok, got = grant_batch(ZTE, batch) total += got log("ip %s -> granted %d/%d, sum +%.2f (total %.2f)" % (ip, ok, len(batch), got, total)) log("DONE total_granted=%.2f" % total) if __name__ == "__main__": main()