"""FreeModel core: cookies session, captcha solve, register/login, api calls.""" import base64, io, json, math, os, pickle, random, re, urllib.parse, urllib.request, http.cookiejar BASE = "https://freemodel.in" UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36" HERE = os.path.dirname(os.path.abspath(__file__)) class Sess: def __init__(self, cjfile=None, proxy=None): import requests self.cjfile = cjfile self.s = requests.Session() if proxy: self.s.proxies = {"http": "http://" + proxy, "https": "http://" + proxy} self.s.headers.update({"User-Agent": UA, "Accept-Language": "en-US,en;q=0.9"}) if cjfile and os.path.exists(cjfile): try: self.s.cookies.update(json.load(open(cjfile))) except Exception: pass def save(self): if self.cjfile: json.dump(self.s.cookies.get_dict(), open(self.cjfile, "w")) def req(self, path, data=None, json_body=None, accept=None): hdr = {} if accept: hdr["Accept"] = accept try: r = self.s.request("GET" if data is None and json_body is None else "POST", BASE + path, data=data, json=json_body, headers=hdr, timeout=45, allow_redirects=False) return r.status_code, r.text, r.headers.get("Location") or "" except Exception as e: return 0, "ERR %s" % e, "" def get(self, path, **kw): return self.req(path, accept="text/html,application/xhtml+xml", **kw) def ajax(self, path, **kw): return self.req(path, accept="application/json", **kw) class NoRedirect(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): return None def _png(b64): from PIL import Image return Image.open(io.BytesIO(base64.b64decode(b64.split(",", 1)[1]))) def solve_x(ch): import numpy as np bg = np.asarray(_png(ch["bg"]).convert("RGB")).astype(np.float32) pc = _png(ch["piece"]) if pc.mode not in ("RGBA", "LA"): pc = pc.convert("RGBA") a = np.asarray(pc.getchannel("A")).astype(np.float32) prgb = np.asarray(pc.convert("RGB")).astype(np.float32) size = ch["size"] m = a > 128 if m.sum() < 200: return None A = prgb[m] - prgb[m].mean(0) if len(A) < 4: return None best, bx = -2.0, 0 ys = {ch["y"] + d for d in (0, -2, 2, -4, 4)} for yy in ys: if yy < 0 or yy + size > bg.shape[0]: continue for x in range(0, ch["w"] - size + 1): win = bg[yy:yy + size, x:x + size] B = win[m] - win[m].mean(0) den = math.sqrt(float((A * A).sum()) * float((B * B).sum())) or 1e-6 c = float((A * B).sum()) / den if c > best: best, bx = c, x return bx, best def trail(target, n=26, dur=None): dur = dur or random.randint(520, 900) pts = [] for i in range(1, n + 1): r = 1 - (1 - i / n) ** 3 x = target * r + random.uniform(-0.8, 0.8) pts.append([int(round(dur * i / n)), round(max(0.0, min(target, x)), 1)]) pts.append([int(round(dur + random.randint(30, 90))), round(target, 1)]) return pts def pass_captcha(s): st, raw, _ = s.ajax("/captcha/new") ch = json.loads(raw) if "turnstile" in ch: return None, ch res = solve_x(ch) if not res: return None, {"err": "no_piece"} x, score = res st, raw, _ = s.ajax("/captcha/verify", json_body={"id": ch["id"], "x": x, "trail": trail(float(x))}) v = json.loads(raw) if v.get("ok"): return v["token"], {"score": round(score, 4), "x": x} return None, {"verify": v, "score": round(score, 4)} def register(s, email, password, name="Daniel Reeves", phone="+14155550137", ref="FM-REF-DISABLED"): st, html, _ = s.get("/register?ref=" + ref) m = re.search(r'name="_token" value="([^"]+)"', html) if not m: return {"stage": "page", "code": st} tok = m.group(1) ctoken, info = pass_captcha(s) if not ctoken: return {"stage": "captcha", "info": info} st, html, loc = s.req("/register", data={ "_token": tok, "name": name, "phone": phone, "email": email, "password": password, "password_confirmation": password, "captcha_token": ctoken, "ref": ref}) s.save() out = {"stage": "register", "code": st, "loc": loc, "captcha": info} t = re.search(r"