import os import json import secrets from huggingface_hub import HfApi, hf_hub_download HF_TOKEN = os.environ.get("HF_TOKEN") DATASET_REPO_ID = os.environ.get("DATASET_REPO_ID") # nastav jako secret: "tvujusername/resumelens-data" def _get_licenses() -> dict: """Načte licenses.json z HF Dataset repo.""" api = HfApi(token=HF_TOKEN) try: path = api.hf_hub_download( repo_id=DATASET_REPO_ID, filename="licenses.json", repo_type="dataset", force_download=True ) with open(path) as f: return json.load(f) except Exception: return {} def _save_licenses(licenses: dict): """Uloží licenses.json zpět do HF Dataset repo.""" api = HfApi(token=HF_TOKEN) api.upload_file( path_or_fileobj=json.dumps(licenses, indent=2).encode(), path_in_repo="licenses.json", repo_id=DATASET_REPO_ID, repo_type="dataset", ) def create_license_key(email: str, plan: str) -> str: """Vygeneruje nový klíč, uloží ho a vrátí.""" key = "rl_" + secrets.token_hex(16) licenses = _get_licenses() licenses[key] = { "email": email, "plan": plan, "active": True } _save_licenses(licenses) return key def check_license_key(key: str) -> dict | None: """Vrátí info o klíči nebo None.""" if not key or len(key) < 10: return None licenses = _get_licenses() entry = licenses.get(key) if entry and entry.get("active"): return entry return None