forked from pmmp/BedrockData
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathenums.py
45 lines (38 loc) · 1.32 KB
/
enums.py
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
from bs4 import BeautifulSoup
import json
import requests
import os
import shutil
save = ["ParticleType"]
def prepare():
if os.path.exists("enums"):
shutil.rmtree("enums")
os.makedirs("enums")
def fetch_html(branch):
url = f"https://raw.githubusercontent.com/Mojang/bedrock-protocol-docs/refs/heads/{branch}/html/enums.html"
response = requests.get(url)
response.raise_for_status()
return response.text
def dump_enums(html_content):
soup = BeautifulSoup(html_content, "html.parser")
rows = soup.table.contents[3:]
for row in rows:
enum_name = row.td.string
enum_values = {}
for line in row.find_all("tr"):
cols = line.find_all("td")
key = cols[0].string
value = cols[1].string.split(" ")[0]
try:
enum_values[key] = int(value)
except:
enum_values[key] = value
print(enum_name, enum_values)
if enum_name in save:
file_name = enum_name.replace("::", "_")
with open(f"enums/{file_name}.json", "w", encoding="utf-8") as json_file:
json.dump({"name": enum_name, "values": enum_values}, json_file, indent=4)
prepare()
branch = input("Enter branch name (main): ") or "main"
html = fetch_html(branch)
dump_enums(html)