-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemer.py
More file actions
executable file
·175 lines (145 loc) · 5.97 KB
/
schemer.py
File metadata and controls
executable file
·175 lines (145 loc) · 5.97 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import sys
import json
from time import sleep
from bs4 import BeautifulSoup
from selenium import webdriver
import re
def main():
if len(sys.argv) < 2:
print("Usage: python schemer.py <Apple Developer Doc URL> [output_directory]")
sys.exit(1)
url = sys.argv[1]
output_dir = sys.argv[2] if len(sys.argv) > 2 else "."
# Use Selenium to fetch the documentation page with JS rendered
driver = webdriver.Firefox()
driver.get(url)
sleep(.5)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
driver.quit()
# Start with minimal schema containing only required fields
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"x-apple-developer-api-uri": url
}
print("================================================================")
# Print title via class="title"
title_tag = soup.find(class_="title")
if title_tag:
print("Title:", title_tag.text)
schema['title'] = title_tag.text
else:
print("Title: NOT FOUND - skipping")
# Print description via class="abstract content"
desc_tag = soup.find(class_="abstract content")
if desc_tag:
print("Description:", desc_tag.text)
schema['description'] = desc_tag.text
else:
print("Description: NOT FOUND - skipping")
# Print type via class="eyebrow"
type_tag = soup.find(class_="eyebrow")
if type_tag:
print("Type:", type_tag.text)
schema['type'] = type_tag.text.lower()
else:
print("Type: NOT FOUND - skipping")
# Print x-apple-developer-api-version via class="platform"
version_tag = soup.find(class_="platform")
if version_tag:
print("x-apple-developer-api-version:", version_tag.text)
# Extract only the version number (e.g., '1.1+') from the version tag
version_text = version_tag.text
match = re.search(r'[0-9]+\.[0-9]+\+?', version_text)
if match:
schema['x-apple-developer-api-version'] = match.group(0)
else:
print("x-apple-developer-api-version: NOT FOUND - skipping")
# For each property in class="row param"
property_sections = soup.find_all(class_="row param")
print(f"Found {len(property_sections)} property sections")
properties = {}
required = []
for section in property_sections:
# Name and type inside class="col param-symbol large-3 small-12"
symbol_col = section.find(class_="col param-symbol large-3 small-12")
if not symbol_col:
continue
# Name inside class="property-name"
name_tag = symbol_col.find(class_="property-name")
if not name_tag:
continue
prop_name = name_tag.text.strip()
# Type inside class="property-metadata property-type"
type_tag = symbol_col.find(class_="property-metadata property-type")
prop_type = type_tag.text.strip().lower() if type_tag else ""
# Check if type is uri-reference, that should be described as type: string, format: uri-reference
if "uri-reference" in prop_type:
prop_type = "string"
format = "uri-reference"
else:
format = ""
# Requirement and Description inside class="col param-content large-9 small-12"
content_col = section.find(class_="col param-content large-9 small-12")
is_required = False
description = ""
if content_col:
# Required if this class="property-text" field exists
required_tag = content_col.find(class_="property-text")
if required_tag and "required" in required_tag.text.lower():
is_required = True
# Description inside class="content"
desc_tag = content_col.find(class_="content")
if desc_tag:
description = desc_tag.text.strip()
else:
# Sometimes the type is in this field if no description
description = content_col.text.strip()
# Default Value is inside this div/class
# Default value inside class="property-metadata" (if present)
default_value = None
default_tag = content_col.find(class_="property-metadata")
if default_tag and "value" in default_tag.text.lower():
default_value = default_tag.text.lower()
default_value = default_value[8:] #this parses out the " Value: " part of the text
# Build property schema
prop_schema = {}
if description:
prop_schema["description"] = description
# Check if property type is linked to another object or not
if "." in prop_type:
prop_type += ".json"
prop_schema["$ref"] = prop_type
else:
prop_schema["type"] = prop_type
if format:
prop_schema["format"] = format
if default_value:
prop_schema["default"] = default_value
properties[prop_name] = prop_schema
if is_required:
required.append(prop_name)
if required:
schema['required'] = required
else:
print("Required fields: NONE FOUND - skipping")
if properties:
schema['properties'] = properties
else:
print("Properties: NOT FOUND - skipping")
print("================================================================")
# Save the populated schema
import os
# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
filename = title_tag.text if title_tag else "UnknownSchema"
if title_tag:
filename = title_tag.text
output_file = os.path.join(output_dir, f'{filename}.json')
with open(output_file, 'w') as f:
json.dump(schema, f, indent=2)
print(f"Schema generated and saved to {output_file}")
else:
print("Couldn't load page and/or parse data, not writing file.")
if __name__ == "__main__":
main()