summaryrefslogtreecommitdiff
path: root/pb2e.py
blob: f1b3911e760f82615bfba6234d7e7e9d72813441 (plain)
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
import json
import requests

request_headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    "Accept-Encoding": "gzip, deflate, br",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0"
}

url = 'https://pathbuilder2e.com/json.php'
cache_path = 'characters'

def download_character(pfb_id, name=None):
    r = requests.get(url, params={"id": pfb_id}, headers=request_headers)
    r.raise_for_status()
    res = r.json()
    if not res['success'] or 'build' not in res:
        raise RuntimeError("Unsuccessful build", res)
    res = res['build']
    res['pfb_id'] = pfb_id
    if name is None:
        name = res["name"]
    else:
        res["name"] = name
    with open(f"{cache_path}/{name}.json", "w", encoding="utf8") as out_file:
        json.dump(res, out_file, indent=2, ensure_ascii=False)
    return res


def open_character(name):
    with open(f"{cache_path}/{name}.json", "r", encoding="utf8") as js_file:
        return json.load(js_file)