/** * # 脚本 为全部已发布产品抓 post-.css → products.meta.page_css * 与 import-page-css 相同的翻译(css-translate);断点续传(已有 page_css 键跳过)。 * 用法:bun scripts/import-product-css.ts */ import { SQL } from "bun"; import { collectDocIds, filterCssForIds } from "../src/utils/css-translate"; const sql = new SQL("postgres://dusuniot:dusuniot@127.0.0.1:25432/dusuniot", { max: 1 }); const rows = await sql`select id, legacy_id, meta::text as meta from products where status = ${"published"} and legacy_id is not null`; let done = 0, skipped = 0, fail = 0; for (const row of rows) { const meta = JSON.parse(row.meta); if (meta.page_css !== undefined) { skipped++; continue; } const ids = collectDocIds(meta.elementor); let raw: string | null = null; for (let a = 0; a < 3 && raw === null; a++) { try { const res = await fetch(`https://www.dusuniot.com/wp-content/uploads/elementor/css/post-${row.legacy_id}.css`, { headers: { "User-Agent": "Mozilla/5.0" } }); if (res.ok) raw = await res.text(); else if (res.status === 404) break; } catch { await new Promise((r) => setTimeout(r, 300)); } } if (raw === null) { fail++; continue; } meta.page_css = filterCssForIds(raw, ids, row.legacy_id); await sql`update products set meta = ${JSON.stringify(meta)}::text::jsonb where id = ${row.id}`; done++; if (done % 25 === 0) console.log(`... ${done}`); } console.log("imported:", done, "skipped:", skipped, "fail:", fail); await sql.end();