HF-MLH Toolkit v2 — stdlib OAuth/claim pipeline, resumable JSONL bank, config.json setup
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Refresh cached Hacktoberfest API tokens WITHOUT touching MLH at all.
|
||||
|
||||
hf_raw_claim.py stores {email: {accessToken, refreshToken, user, ...}} in
|
||||
hf_hf_tokens.json. The refreshToken outlives the accessToken, so a whole
|
||||
claim wave can run browser-free and OTP-free as long as refresh works.
|
||||
|
||||
Usage:
|
||||
python hf_refresh.py # refresh every cached token, rewrite the cache
|
||||
python hf_refresh.py <email> # refresh one account
|
||||
Exit code 0 if at least one token refreshed (or cache empty), 2 if all failed.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.abspath(__file__)))
|
||||
import hf_config as CFG
|
||||
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36"
|
||||
|
||||
|
||||
def log(*a):
|
||||
print(time.strftime("%H:%M:%S"), *a, flush=True)
|
||||
|
||||
|
||||
def load_cache():
|
||||
try:
|
||||
return json.load(open(CFG.data_file("hf_hf_tokens.json"), encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def save_cache(cache):
|
||||
json.dump(cache, open(CFG.data_file("hf_hf_tokens.json"), "w", encoding="utf-8"), indent=1)
|
||||
|
||||
|
||||
def refresh(refresh_token):
|
||||
"""POST /auth/refresh -> new token dict, or (None, err)."""
|
||||
body = json.dumps({"refreshToken": refresh_token}).encode()
|
||||
rq = urllib.request.Request(CFG.API + "/auth/refresh", data=body, method="POST")
|
||||
rq.add_header("Content-Type", "application/json")
|
||||
rq.add_header("User-Agent", UA)
|
||||
rq.add_header("Origin", "https://hacktoberfest.com")
|
||||
rq.add_header("Referer", "https://hacktoberfest.com/")
|
||||
try:
|
||||
r = urllib.request.urlopen(rq, timeout=45)
|
||||
return json.loads(r.read().decode()), None
|
||||
except urllib.error.HTTPError as e:
|
||||
return None, "http_%s %s" % (e.code, e.read().decode(errors="replace")[:120])
|
||||
except Exception as e:
|
||||
return None, str(e)[:120]
|
||||
|
||||
|
||||
def main():
|
||||
only = sys.argv[1] if len(sys.argv) > 1 else None
|
||||
cache = load_cache()
|
||||
if not cache:
|
||||
log("cache empty (%s) - run hf_raw_claim.py first" % CFG.data_file("hf_hf_tokens.json"))
|
||||
return
|
||||
ok = fail = 0
|
||||
for email, ent in list(cache.items()):
|
||||
if only and email != only:
|
||||
continue
|
||||
rt = (ent or {}).get("refreshToken")
|
||||
if not rt:
|
||||
log(email, "no refreshToken cached - skip")
|
||||
fail += 1
|
||||
continue
|
||||
new, err = refresh(rt)
|
||||
if new and new.get("accessToken"):
|
||||
merged = dict(ent)
|
||||
merged.update(new)
|
||||
merged["refreshed_ts"] = int(time.time())
|
||||
cache[email] = merged
|
||||
ok += 1
|
||||
log(email, "REFRESH OK")
|
||||
else:
|
||||
fail += 1
|
||||
log(email, "REFRESH FAIL:", err)
|
||||
time.sleep(1)
|
||||
save_cache(cache)
|
||||
log("done ok=%d fail=%d" % (ok, fail))
|
||||
if only or ok == 0:
|
||||
sys.exit(0 if ok else 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user