Files

88 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
"""Follow a partner 'Claim your promo code' link, check claim requirements and redeem.
Usage: python partner_claim.py <cookies-file> <claim-url>
Claim URL = the tracked link from the MLH partner-offers email.
Output: promo_claimed.json (promoCode), redeem_page.html dumps."""
import json, re, sys, time, html as H, urllib.request, urllib.parse, urllib.error, http.cookiejar
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36"
CKF, CLAIM = sys.argv[1], sys.argv[2]
cj = http.cookiejar.CookieJar()
for p in open(CKF).read().strip().split("; "):
if "=" in p:
n, v = p.split("=", 1)
c = http.cookiejar.Cookie(0, n, v, None, False, ".mlh.com", True, False,
"/", True, False, None, False, None, None, {})
cj.set_cookie(c)
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [("User-Agent", UA)]
def inertia_props(html):
m = re.search(r'<script[^>]*type="application/json"[^>]*>(.*?)</script>', html, re.S)
if m:
try: return json.loads(m.group(1))
except Exception: pass
md = re.search(r'data-page="([^"]+)"', html)
if md:
try: return json.loads(H.unescape(md.group(1)))
except Exception: pass
return {}
r = opener.open(CLAIM, timeout=60)
d = r.read().decode(errors="replace")
print("final url:", r.geturl()[:200], "status:", r.status)
open("redeem_page.html", "w", encoding="utf-8").write(d)
pg = inertia_props(d)
props = pg.get("props", {})
print("component:", pg.get("component"))
unmet = props.get("unmetClaimRequirements") or []
reqs = props.get("claimRequirements") or []
pc = props.get("promoCode")
rtok = props.get("token"); rurl = props.get("redeemUrl")
print("claimRequirements:", reqs)
print("unmet:", unmet)
print("already has promoCode:", bool(pc))
if pc:
json.dump({"promoCode": pc}, open("promo_claimed.json", "w"))
print("SAVED promo_claimed.json")
sys.exit(0)
if unmet:
hints = {"verified_phone": "verify a phone number in account settings (Twilio SMS; free virtual pools often blocked)",
"github_oauth": "connect GitHub: GET /social_profiles/connect/github and complete OAuth",
"github_account_age": "GitHub account too young — connect an older one",
"checked_in": "register/check in to the GHW event"}
for u in unmet:
print("UNMET:", u, "->", hints.get(u, "?"))
sys.exit(1)
if rtok and rurl:
full = "https://www.mlh.com" + rurl if rurl.startswith("/") else rurl
body = json.dumps({"token": rtok}).encode()
rq = urllib.request.Request(full, data=body, method="POST")
for k, v in {"Content-Type": "application/json", "X-Inertia": "true",
"X-Inertia-Version": pg.get("version", ""), "X-Requested-With": "XMLHttpRequest",
"Origin": "https://www.mlh.com", "Referer": r.geturl(),
"Accept": "application/json, text/html"}.items():
rq.add_header(k, v)
try:
rr = opener.open(rq, timeout=60); rd = rr.read().decode(errors="replace")
print("claim POST:", rr.status)
except urllib.error.HTTPError as e:
rd = e.read().decode(errors="replace"); print("claim POST err:", e.code, rd[:200])
time.sleep(2)
r2 = opener.open(r.geturl(), timeout=60)
d2 = r2.read().decode(errors="replace")
open("redeem_page2.html", "w", encoding="utf-8").write(d2)
p2 = inertia_props(d2).get("props", {})
pc2 = p2.get("promoCode")
print("promoCode after claim:", bool(pc2))
if pc2:
json.dump({"promoCode": pc2, "sponsor": (p2.get("sponsor") or {}).get("name", "")},
open("promo_claimed.json", "w"))
print("SAVED promo_claimed.json")
else:
print("no token/redeemUrl in props — dump:", json.dumps(props)[:400])