# -*- coding: utf-8 -*- """Фарм Theos: по 5 аккаунтов на каждый egress (лимит гранта = 5 аккаунтов/IP). Использование: python farm_grant.py perfetch [per_ip] python farm_grant.py status proxies.txt: по одному URL прокси в строке (http://user:pass@host:port); строка DIRECT = прямое соединение. """ import json import sys import dpcode as D PER_IP_DEFAULT = 5 def load_bank(): 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 return recs def zero_accounts(recs): out = [] for r in recs.values(): t = r.get("theos") or {} try: g = float(t.get("granted") or 0) b = float(t.get("balance") or 0) except Exception: g = b = 0.0 if g <= 0 and b <= 0: out.append(r) out.sort(key=lambda r: r.get("ts") or 0) return out def status(): recs = load_bank() pos = [(r["email"], float((r.get("theos") or {}).get("balance") or 0)) for r in recs.values()] have = [(e, b) for e, b in pos if b > 0] print("accounts=%d with_balance=%d total=%.2f zero=%d" % (len(recs), len(have), sum(b for _, b in have), len(zero_accounts(recs)))) def perfetch(path, per_ip): recs = load_bank() todo = zero_accounts(recs) proxies = [x.strip() for x in open(path, encoding="utf-8") if x.strip()] print("zero=%d proxies=%d per_ip=%d" % (len(todo), len(proxies), per_ip), flush=True) idx = 0 got = 0.0 tried = 0 for px in proxies: if idx >= len(todo): break ok_here = 0 misses = 0 batch = [] while batch.__len__() < per_ip and idx < len(todo): batch.append(todo[idx]) idx += 1 for rec in batch: tried += 1 email = rec["email"] machine = rec.get("machine") or D.machine_header(salt=email) s = D.session_from(rec) if px.upper() != "DIRECT": s.proxies.update({"http": px, "https": px}) try: st, d = D.theos(s, machine) except Exception as e: misses += 1 print(" %-40s ERR %s" % (email, str(e)[:50]), flush=True) continue if not isinstance(d, dict): misses += 1 print(" %-40s http=%s %s" % (email, st, str(d)[:60]), flush=True) 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_here += 1 got += bal print(" %-40s +%.2f (ip_ok=%d)" % (email, bal, ok_here), flush=True) else: misses += 1 print(" %-40s zero" % email, flush=True) print("IP %-42s granted=%d/%.0f misses=%d sum=%.2f" % (px[-42:], ok_here, float(per_ip), misses, got), flush=True) if ok_here == 0 and misses >= per_ip: # прокси мёртв или IP уже выжжен — аккаунты не тратим, вернуть в очередь idx -= len(batch) print(" ^ вернул %d аккаунтов в очередь (IP не дал гранта)" % len(batch), flush=True) print("TOTAL tried=%d granted_sum=%.2f" % (tried, got), flush=True) def main(): cmd = sys.argv[1] if len(sys.argv) > 1 else "status" if cmd == "status": status() elif cmd == "perfetch": path = sys.argv[2] per_ip = int(sys.argv[3]) if len(sys.argv) > 3 else PER_IP_DEFAULT perfetch(path, per_ip) else: print("unknown command") if __name__ == "__main__": main()