diff options
| author | Supremist <sergkarv@gmail.com> | 2025-10-28 00:30:11 +0200 |
|---|---|---|
| committer | Supremist <sergkarv@gmail.com> | 2025-10-28 00:44:33 +0200 |
| commit | a792c35f6ee3b37ecb6fe1692f81574509728cae (patch) | |
| tree | cbc760039838f6dfdad9d01b953399f4fbe39f05 | |
| parent | 146aa6ddff5a347c61aef5ddd62bebd344508c3d (diff) | |
| download | pf2e_calc-a792c35f6ee3b37ecb6fe1692f81574509728cae.tar.xz pf2e_calc-a792c35f6ee3b37ecb6fe1692f81574509728cae.zip | |
Add character import from pathbuilder2e site
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | main.py | 9 | ||||
| -rw-r--r-- | pb2e.py | 33 |
3 files changed, 42 insertions, 1 deletions
@@ -1,4 +1,5 @@ venv/ .idea/ __pycache__/ +characters/ *.py[codz] @@ -2,6 +2,8 @@ import math from sys import base_exec_prefix from distribution import Distribution +import pb2e + def d(edge_count): res = Distribution() @@ -185,6 +187,11 @@ def test3(): show_stat("attack", attack) show_stat("falcata", falcata.strike(bonus, dmg_bonus, ac, 0)) +def main4(): + # char = pb2e.download_character(pfb_id=160180) + char = pb2e.open_character("quizrel2") + print(char) + if __name__ == '__main__': - test3() + main4() @@ -0,0 +1,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) + |
