File size: 1,566 Bytes
9ec4e30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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