-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelio_material_updater.py
More file actions
326 lines (278 loc) · 10.8 KB
/
helio_material_updater.py
File metadata and controls
326 lines (278 loc) · 10.8 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""查询 Helio API 所有材料并按品牌分类输出全部字段。"""
import os
from datetime import datetime, timezone, timedelta
import requests
from rich.console import Console
from rich.table import Table
API_URL = "https://api.helioadditive.com/graphql"
TOKEN = os.environ.get("HELIO_TOKEN")
if not TOKEN:
raise SystemExit("错误: 未设置 HELIO_TOKEN 环境变量\n提示: export HELIO_TOKEN='your_token_here'")
HEADERS = {
"Content-Type": "application/json",
"Authorization": f"Bearer {TOKEN}",
}
PAGE_SIZE = 50
# 当前已知的 Material 字段列表,用于检测 API 是否新增了字段
KNOWN_FIELDS = {
"id", "name", "brand", "alternativeNames", "description",
"insertedAt", "updatedAt", "density", "capacity",
"conductivityX", "conductivityY", "conductivityZ",
"emissivity", "dryingTimeHours", "heatedChamberRequirement",
"applicationAreas", "tg", "feedstock", "emailToBuy",
"maxExtrusionTemp", "minExtrusionTemp", "dryingTemp",
"bedTempMin", "bedTempMax",
}
# 加热仓要求翻译
CHAMBER_MAP = {
"NOT_REQUIRED": "不需要",
"OPTIONAL": "可选",
"REQUIRED": "必须",
}
# 应用领域翻译
AREA_MAP = {
"AEROSPACE": "航空航天",
"ART": "艺术",
"AUTOMOTIVE": "汽车",
"CONSTRUCTION_ARCHITECTURE": "建筑",
"CONSUMER_PRODUCTS": "消费品",
"DRONES": "无人机",
"ECO_SUSTAINABILITY": "环保",
"EDUCATION": "教育",
"ELECTRICAL": "电气",
"ELECTRONICS": "电子",
"ENGINEERING_PROTOTYPING": "工程原型",
"GENERAL_PROTOTYPING": "通用原型",
"HOBBY": "爱好",
"INDUSTRIAL_PROTOTYPING": "工业原型",
"INTERIOR_DESIGN": "室内设计",
"LARGE_SCALE_PROTOTYPING": "大型原型",
"LIGHTING_EQUIPMENT": "照明",
"LOW_TEMP_MOLD": "低温模具",
"MARINE": "船舶",
"MED_TEMP_MOLD": "中温模具",
"MEDICAL": "医疗",
"OUTDOOR_EQUIPMENT": "户外",
"ROBOTICS": "机器人",
"TOOLING_MANUFACTURING": "工装制造",
"WEARABLES": "穿戴设备",
}
console = Console(width=300)
def query(gql, variables=None):
resp = requests.post(API_URL, json={"query": gql, "variables": variables or {}}, headers=HEADERS)
resp.raise_for_status()
data = resp.json()
if "errors" in data:
raise RuntimeError(data["errors"])
return data["data"]
def fetch_summary():
return query("{ materialsSummary { count desktopMaterials lfamMaterials experimentalMaterials brands } }")["materialsSummary"]
def check_new_fields():
"""通过 introspection 检测 Material 类型是否有新增字段。"""
gql = '{ __type(name: "Material") { fields { name } } }'
data = query(gql)
api_fields = {f["name"] for f in data["__type"]["fields"]}
new_fields = api_fields - KNOWN_FIELDS
if new_fields:
console.print(f" [bold red][!] 检测到新字段: {', '.join(sorted(new_fields))}, 请更新代码[/]")
else:
console.print(" 字段无变更")
return new_fields
def fetch_all_materials():
gql = """
query($page: Int, $pageSize: Int) {
materials(page: $page, pageSize: $pageSize) {
pages
pageInfo { hasNextPage }
objects {
... on Material {
id name feedstock
brand { name }
alternativeNames { bambustudio }
description
minExtrusionTemp maxExtrusionTemp
bedTempMin bedTempMax
dryingTemp dryingTimeHours
density tg capacity
conductivityX conductivityY conductivityZ
emissivity
heatedChamberRequirement
applicationAreas
emailToBuy
insertedAt updatedAt
}
}
}
}
"""
all_materials = []
page = 1
while True:
data = query(gql, {"page": page, "pageSize": PAGE_SIZE})
materials = data["materials"]
all_materials.extend(materials["objects"])
console.print(f" 第 {page}/{materials['pages']} 页, 获取 {len(materials['objects'])} 条")
if not materials["pageInfo"]["hasNextPage"]:
break
page += 1
return all_materials
def k_to_c(k):
if k is None:
return None
return round(k - 273.15)
def clean(s):
if not s:
return ""
for ch in ("\u2122", "\u00ae", "\u00a9"):
s = s.replace(ch, "")
return s.strip()
def fmt_temp_range(lo_k, hi_k):
lo = k_to_c(lo_k)
hi = k_to_c(hi_k)
if lo is not None and hi is not None:
return f"{lo}~{hi}" if lo != hi else str(lo)
return "-"
def fmt_val(v, fmt=".0f"):
if v is None:
return "-"
return f"{v:{fmt}}"
def translate_chamber(val):
if not val:
return "-"
return CHAMBER_MAP.get(val, val)
def translate_areas(areas):
if not areas:
return "-"
return ", ".join(AREA_MAP.get(a, a) for a in areas)
def build_table(material_list, title):
table = Table(title=title, show_lines=False, pad_edge=False, expand=False)
table.add_column("品牌", style="cyan", no_wrap=True)
table.add_column("名称", style="white", no_wrap=True)
table.add_column("挤出温度", justify="right")
table.add_column("热床温度", justify="right")
table.add_column("干燥温度", justify="right")
table.add_column("干燥h", justify="right")
table.add_column("密度", justify="right")
table.add_column("Tg(玻璃化转变温度)", justify="right")
table.add_column("比热容", justify="right")
table.add_column("导热X", justify="right")
table.add_column("导热Y", justify="right")
table.add_column("导热Z", justify="right")
table.add_column("发射率", justify="right")
table.add_column("加热仓", justify="center")
table.add_column("应用领域", style="dim")
for m in sorted(material_list, key=lambda x: (x["brand"]["name"], x["name"])):
table.add_row(
clean(m["brand"]["name"]),
clean(m["name"]),
fmt_temp_range(m.get("minExtrusionTemp"), m.get("maxExtrusionTemp")),
fmt_temp_range(m.get("bedTempMin"), m.get("bedTempMax")),
fmt_val(k_to_c(m.get("dryingTemp"))),
fmt_val(m.get("dryingTimeHours")),
fmt_val(m.get("density")),
fmt_val(m.get("tg")),
fmt_val(m.get("capacity")),
fmt_val(m.get("conductivityX"), ".3f"),
fmt_val(m.get("conductivityY"), ".3f"),
fmt_val(m.get("conductivityZ"), ".3f"),
fmt_val(m.get("emissivity"), ".2f"),
translate_chamber(m.get("heatedChamberRequirement")),
translate_areas(m.get("applicationAreas")),
)
return table
def main():
# 1. 总览
summary = fetch_summary()
summary_table = Table(title="Helio API 材料总览", show_header=False, expand=False)
summary_table.add_column("项目", style="bold")
summary_table.add_column("数量", justify="right", style="green")
summary_table.add_row("总数", str(summary["count"]))
summary_table.add_row("桌面级", str(summary["desktopMaterials"]))
summary_table.add_row("LFAM", str(summary["lfamMaterials"]))
summary_table.add_row("实验性", str(summary["experimentalMaterials"]))
summary_table.add_row("品牌数", str(summary["brands"]))
console.print(summary_table)
console.print()
# 2. 检测新字段
console.print("正在检测 API 字段变更...")
check_new_fields()
console.print()
# 3. 拉取全部材料
console.print("正在拉取所有材料...")
materials = fetch_all_materials()
console.print(f"\n实际获取: [bold]{len(materials)}[/] 种材料")
if len(materials) < summary["count"]:
console.print(f" (平台共 {summary['count']} 种, 当前 PAT 仅可访问 {len(materials)} 种)")
console.print()
# 4. 按 feedstock 分类输出
filament = [m for m in materials if m["feedstock"] == "FILAMENT"]
pellet = [m for m in materials if m["feedstock"] == "PELLET"]
if filament:
console.print(build_table(filament, f"FILAMENT 线材 ({len(filament)}种)"))
console.print()
if pellet:
console.print(build_table(pellet, f"PELLET 颗粒料 ({len(pellet)}种)"))
# 5. 保存 Markdown 文件
md_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "README.md")
save_markdown(summary, materials, md_path)
console.print(f"\n已保存到 [bold]{md_path}[/]")
def md_table_row(m):
"""生成一行 Markdown 表格数据。"""
return (
f"| {clean(m['brand']['name'])} "
f"| {clean(m['name'])} "
f"| {fmt_temp_range(m.get('minExtrusionTemp'), m.get('maxExtrusionTemp'))} "
f"| {fmt_temp_range(m.get('bedTempMin'), m.get('bedTempMax'))} "
f"| {fmt_val(k_to_c(m.get('dryingTemp')))} "
f"| {fmt_val(m.get('dryingTimeHours'))} "
f"| {fmt_val(m.get('density'))} "
f"| {fmt_val(m.get('tg'))} "
f"| {fmt_val(m.get('capacity'))} "
f"| {fmt_val(m.get('conductivityX'), '.3f')} "
f"| {fmt_val(m.get('conductivityY'), '.3f')} "
f"| {fmt_val(m.get('conductivityZ'), '.3f')} "
f"| {fmt_val(m.get('emissivity'), '.2f')} "
f"| {translate_chamber(m.get('heatedChamberRequirement'))} "
f"| {translate_areas(m.get('applicationAreas'))} |"
)
MD_HEADER = "| 品牌 | 名称 | 挤出温度 | 热床温度 | 干燥温度 | 干燥h | 密度 | Tg(玻璃化转变温度) | 比热容 | 导热X | 导热Y | 导热Z | 发射率 | 加热仓 | 应用领域 |"
MD_SEP = "| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | :---: | --- |"
def save_markdown(summary, materials, path):
filament = sorted(
[m for m in materials if m["feedstock"] == "FILAMENT"],
key=lambda x: (x["brand"]["name"], x["name"]),
)
pellet = sorted(
[m for m in materials if m["feedstock"] == "PELLET"],
key=lambda x: (x["brand"]["name"], x["name"]),
)
lines = [
f"# Helio API 材料列表",
f"",
f"> 生成时间: {datetime.now(timezone(timedelta(hours=8))).strftime('%Y-%m-%d %H:%M:%S')} (北京时间)",
f">",
f"> 总数: {summary['count']} | 桌面级: {summary['desktopMaterials']} | LFAM: {summary['lfamMaterials']} | 实验性: {summary['experimentalMaterials']} | 品牌: {summary['brands']}",
f">",
f"> 实际获取: {len(materials)} 种",
f"",
f"## FILAMENT 线材 ({len(filament)}种)",
f"",
MD_HEADER,
MD_SEP,
]
for m in filament:
lines.append(md_table_row(m))
lines += [
f"",
f"## PELLET 颗粒料 ({len(pellet)}种)",
f"",
MD_HEADER,
MD_SEP,
]
for m in pellet:
lines.append(md_table_row(m))
lines.append("")
with open(path, "w", encoding="utf-8") as f:
f.write("\n".join(lines))
if __name__ == "__main__":
main()