Files

112 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
"""Shared config for hf-mlh-toolkit.
Resolution order for every setting: environment variable > config.json (repo root,
gitignored) > built-in default. No secrets are stored in this repo.
Keys (env var name == config.json key):
IMAP_USER Gmail address used to read OTP / magic-link emails
IMAP_PASS Gmail app password (NOT the account password)
MAIL_ALIAS_BASE base email for +tag aliases (default: IMAP_USER)
TWO_CAPTCHA_KEY optional 2captcha fallback key for Turnstile
SIDECAR_URL local real-page Turnstile solver (default http://127.0.0.1:8877)
MLH_SITEKEY Turnstile sitekey (public value, safe default baked in)
TOOLKIT_DATA data dir (default <repo>/data) - banks, sessions, accounts
TOOLKIT_WORK work dir (default <repo>/_work) - debug dumps
HF_TOOLKIT_CONFIG path to config.json (default <repo>/config.json)
Data files (all under TOOLKIT_DATA):
mlh_farm_accounts.jsonl input: {"email": "..."} per line
hf_mlh_sessions.json MLH cookies per email (written by reauth scripts)
hf_hf_tokens.json HF OAuth tokens cache per email
hf_claim_bank.jsonl resumable claim results (email+sponsor keyed)
hf_promo_accounts.jsonl hf_promos.py output
captcha_keys_2captcha.json optional fallback key file
"""
import json
import os
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
_cfg = {}
_cfg_path = Path(os.environ.get("HF_TOOLKIT_CONFIG", str(ROOT / "config.json")))
if _cfg_path.exists():
try:
_cfg = json.loads(_cfg_path.read_text(encoding="utf-8"))
except Exception as e:
print("hf_config: config.json parse error:", e, file=sys.stderr)
def get(key, default=None):
"""env > config.json > default. Empty strings count as unset."""
v = os.environ.get(key)
if v is None or v == "":
v = _cfg.get(key)
if v is None or v == "":
return default
return v
DATA = Path(get("TOOLKIT_DATA", str(ROOT / "data")))
WORK = Path(get("TOOLKIT_WORK", str(ROOT / "_work")))
try:
DATA.mkdir(parents=True, exist_ok=True)
WORK.mkdir(parents=True, exist_ok=True)
except Exception:
pass
SIDECAR = get("SIDECAR_URL", "http://127.0.0.1:8877")
SITEKEY = get("MLH_SITEKEY", "0x4AAAAAAARi-8xgLgfKOXIb") # public sitekey
API = "https://hacktoberfest-api.mlh.com"
def imap_creds():
"""(user, app_password) or exit with a clear message."""
user = get("IMAP_USER")
pw = get("IMAP_PASS")
if not user or not pw:
sys.exit("hf_config ERROR: IMAP_USER / IMAP_PASS not set.\n"
" export IMAP_USER=you@gmail.com IMAP_PASS=<16-char app password>\n"
" or copy config.example.json -> config.json and fill it in.\n"
" Needed to read MLH OTP / magic-link emails.")
return user, pw
def alias_base():
return get("MAIL_ALIAS_BASE") or get("IMAP_USER") or ""
def alias_email(tag):
"""user+tag@domain from MAIL_ALIAS_BASE (falls back to IMAP_USER)."""
base = alias_base()
if not base or "@" not in base:
sys.exit("hf_config ERROR: MAIL_ALIAS_BASE / IMAP_USER not set - "
"cannot build alias email for tag %r" % tag)
local, domain = base.split("@", 1)
return "%s+%s@%s" % (local, tag, domain)
def two_captcha_key():
"""2captcha key: env/config first, then data/captcha_keys_2captcha.json, else None."""
k = get("TWO_CAPTCHA_KEY")
if k:
return k
f = DATA / "captcha_keys_2captcha.json"
if f.exists():
try:
d = json.loads(f.read_text(encoding="utf-8"))
if isinstance(d, dict) and "keys" in d:
return d["keys"][0]["key"]
if isinstance(d, list) and d:
return d[0]
except Exception:
pass
return None
def data_file(name):
"""Absolute path string for a file under the data dir."""
return str(DATA / name)