Files
LFSpecs/scripts/sync-phonerecommend.mjs
2026-11-06 15:07:02 +08:00

1008 lines
38 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 从 phonerecommend.top 同步手机数据到本项目的导入格式 / 数据库。
*
* 默认只产出 formatVersion: 1 的 JSON(可上传到 /admin/import);
* --write 直接幂等写入 data/app.db,--images 下载封面与相册到 data/uploads。
*
* 示例:
* pnpm sync:phonerecommend
* pnpm sync:phonerecommend -- --write
* pnpm sync:phonerecommend -- --write --images
* pnpm sync:phonerecommend -- --write --images --brand 华为 --brand 小米
*/
import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { pinyin } from 'pinyin-pro';
import sharp from 'sharp';
import { deviceSchema } from '../src/lib/schema/device';
import { importEntities } from '../src/lib/repo';
import { getStorage, storageRoot } from '../src/lib/storage';
const BASE = 'https://www.phonerecommend.top';
const HEADERS = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
Accept: 'application/json, text/plain, */*',
Referer: `${BASE}/`,
Origin: BASE,
};
/** 源站中文品牌 → 本地品牌规范名(与现有数据保持一致) */
const BRAND_MAP = {
华为: 'Huawei',
荣耀: 'Honor',
小米: 'Xiaomi',
红米: 'Redmi',
OPPO: 'OPPO',
vivo: 'vivo',
一加: 'OnePlus',
真我: 'realme',
iQOO: 'iQOO',
努比亚: 'nubia',
中兴: 'ZTE',
摩托罗拉: 'Motorola',
三星: 'Samsung',
苹果: 'Apple',
魅族: 'Meizu',
WIKO: 'WIKO',
ROG: 'ROG',
金立: 'Gionee',
酷派: 'Coolpad',
蔚来Phone: 'NIO Phone',
麦芒: 'Maimang',
飞利浦: 'Philips',
水月雨MIAD: 'Moondrop MIAD',
联想拯救者手机: 'Lenovo Legion',
};
const CAMERA_POSITION_FALLBACK = ['wide', 'ultrawide', 'tele', 'periscope'];
/* ------------------------------- CLI ------------------------------- */
function parseArgs(argv) {
const options = { write: false, images: false, brands: [], limit: 0, pageSize: 100, concurrency: 6, out: '' };
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
if (arg === '--write') options.write = true;
else if (arg === '--images') options.images = true;
else if (arg === '--brand') options.brands.push(String(argv[++i] ?? ''));
else if (arg === '--limit') options.limit = Number(argv[++i] ?? 0);
else if (arg === '--page-size') options.pageSize = Math.min(100, Math.max(1, Number(argv[++i] ?? 100)));
else if (arg === '--concurrency') options.concurrency = Math.max(1, Number(argv[++i] ?? 6));
else if (arg === '--out') options.out = String(argv[++i] ?? '');
}
return options;
}
/* ----------------------------- 基础工具 ----------------------------- */
const PLACEHOLDERS = new Set(['', '-', '—', '/', '暂无', '暂无数据', '无数据']);
function clean(value) {
if (typeof value !== 'string') return '';
const text = value
.replace(/<\/?br\s*\/?>/gi, '\n')
.replace(/<[^>]*>/g, ' ')
.replace(/&nbsp;/gi, ' ')
.replace(/&lt;/gi, '<')
.replace(/&gt;/gi, '>')
.replace(/&amp;/gi, '&')
.replace(/[ \t\u3000]+/g, ' ')
.replace(/\n{2,}/g, '\n')
.trim();
if (PLACEHOLDERS.has(text)) return '';
return text;
}
function splitList(value) {
return clean(value)
.split(/[,,、;;\n]+/)
.map((item) => item.trim())
.filter((item) => item && !PLACEHOLDERS.has(item));
}
function firstNumber(value) {
const match = clean(value).match(/-?\d+(?:\.\d+)?/);
return match ? match[0] : '';
}
function normalizeMetric(value) {
return clean(value)
.replace(/(\d+(?:\.\d+)?)\s*毫米/g, '$1 mm')
.replace(/(\d+(?:\.\d+)?)\s*克/g, '$1 g')
.trim();
}
function toIsoDate(value) {
const text = clean(value);
if (!text) return undefined;
const match = text.match(/(\d{4})[年./-](\d{1,2})[月./-](\d{1,2})/);
if (match) {
return `${match[1]}-${match[2].padStart(2, '0')}-${match[3].padStart(2, '0')}`;
}
const parsed = new Date(text);
return Number.isNaN(parsed.getTime()) ? undefined : parsed.toISOString().slice(0, 10);
}
function slugify(value) {
const syllables = pinyin(value, { toneType: 'none', type: 'array', nonZh: 'consecutive' });
return syllables
.join('-')
.toLowerCase()
.replace(/\+/g, '-plus')
.replace(/&/g, '-and')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.replace(/-{2,}/g, '-');
}
function unique(items) {
return [...new Set(items.filter(Boolean))];
}
/** 源站部分文本会把商城推荐、评价等内容拼进参数,按已知分隔词截断 */
const SOURCE_JUNK_MARKERS = [
'暂无用户评价',
'为你推荐',
'没有更多了',
'扫一扫',
'到手价',
'预售价',
'官方标配',
'¥',
'¥',
];
function sanitizeSourceText(value) {
let text = clean(value);
const cuts = SOURCE_JUNK_MARKERS.map((marker) => text.indexOf(marker)).filter((index) => index >= 0);
if (cuts.length > 0) text = text.slice(0, Math.min(...cuts)).trim();
return text;
}
/**
* 解析屏幕亮度:区分典型亮度、局部/HDR 峰值、全局峰值与最低亮度。
* 源站文案顺序不固定且会把多个指标连写,这里按空格 / 标点切段后再判断,
* 避免一个数字的上下文串到相邻指标(如把「最低亮度」误判成峰值)。
*/
function parseBrightness(raw) {
const text = clean(raw);
if (!text) return {};
const typical = [];
const peak = [];
const globalPeak = [];
const unlabeled = [];
const segments = text.split(/[\s,,;;、/]+/).filter(Boolean);
for (const segment of segments) {
const matches = [...segment.matchAll(/(\d+(?:\.\d+)?)\s*(?:尼特|nits?|nit)/gi)];
if (matches.length === 0) continue;
if (/最低|min/i.test(segment)) continue;
const strongPeak = /峰值|激发|HDR|局部|peak/i.test(segment);
const strongTypical = /典型|默认|常规|typical/i.test(segment);
const global = /全局|global/i.test(segment);
for (const match of matches) {
const value = Number(match[1]);
if (!Number.isFinite(value)) continue;
if (strongPeak) {
if (global) globalPeak.push(value);
else peak.push(value);
} else if (strongTypical) {
typical.push(value);
} else {
unlabeled.push(value);
}
}
}
const result = {};
const typicalValues = [...new Set(typical)].sort((a, b) => a - b);
let typicalValue = typicalValues[0];
let peakValue = peak.length > 0 ? Math.max(...peak) : undefined;
// 多个“典型”读数通常对应不同场景:最低值作典型,最高值作峰值
if (typicalValues.length > 1) {
typicalValue = typicalValues[0];
peakValue = Math.max(peakValue ?? 0, typicalValues[typicalValues.length - 1]);
}
// 峰值低于典型值属于源站文案自相矛盾,宁可不展示峰值
if (typicalValue !== undefined && peakValue !== undefined && peakValue < typicalValue) {
peakValue = undefined;
}
if (typicalValue !== undefined) result.brightnessTypical = `${typicalValue} nit`;
if (peakValue !== undefined) result.brightnessPeak = `${peakValue} nit`;
if (globalPeak.length > 0) result.brightnessGlobalPeak = `${Math.max(...globalPeak)} nit`;
if (!result.brightnessTypical && !result.brightnessPeak && unlabeled.length > 0) {
const values = [...new Set(unlabeled)].sort((a, b) => a - b);
result.brightnessTypical = `${values[0]} nit`;
if (values.length > 1) result.brightnessPeak = `${values[values.length - 1]} nit`;
}
return result;
}
function truncate(value, max) {
return value.length > max ? `${value.slice(0, max - 1)}…` : value;
}
/* ------------------------------ 抓取 ------------------------------ */
async function requestJson(path, attempt = 1) {
try {
const response = await fetch(`${BASE}${path}`, { headers: HEADERS });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const json = await response.json();
if (!json?.success) throw new Error(json?.message ?? '接口返回失败');
return json;
} catch (error) {
if (attempt >= 3) throw error;
await new Promise((resolve) => setTimeout(resolve, 300 * attempt));
return requestJson(path, attempt + 1);
}
}
async function fetchAllPhones(pageSize) {
const first = await requestJson(`/api/phones?limit=${pageSize}&page=1`);
const pages = Math.max(1, Number(first.pagination?.pages ?? 1));
const phones = [...(first.data ?? [])];
for (let page = 2; page <= pages; page += 1) {
const json = await requestJson(`/api/phones?limit=${pageSize}&page=${page}`);
phones.push(...(json.data ?? []));
console.log(` 已拉取第 ${page}/${pages} 页,累计 ${phones.length} 款`);
}
return { phones, total: Number(first.pagination?.total ?? phones.length) };
}
/* ----------------------------- 字段映射 ----------------------------- */
function parseLabeled(raw, labels) {
const text = clean(raw);
const result = {};
const matches = [...text.matchAll(new RegExp(`(${labels.join('|')})[::]`, 'g'))];
matches.forEach((match, index) => {
const start = match.index + match[0].length;
const end = index + 1 < matches.length ? matches[index + 1].index : text.length;
result[match[1]] = text.slice(start, end).trim();
});
return result;
}
function parseOs(raw) {
const text = clean(raw);
if (!text) return {};
if (/android/i.test(text)) {
const skin = text.split(/[,,]/)[0].trim();
const version = text.match(/android\s*([\d.]+)/i);
return {
os: version ? `Android ${version[1]}` : 'Android',
skin: skin && !/android/i.test(skin) ? skin : undefined,
};
}
return { os: text };
}
function parseCameraSegments(raw) {
const text = clean(raw);
const map = new Map();
const matches = [...text.matchAll(/(后置|前置)摄像头(\d+)[::]/g)];
if (matches.length === 0) {
if (text) map.set('rear-1', text);
return map;
}
matches.forEach((match, index) => {
const start = match.index + match[0].length;
const end = index + 1 < matches.length ? matches[index + 1].index : text.length;
const side = match[1] === '后置' ? 'rear' : 'front';
map.set(`${side}-${match[2]}`, text.slice(start, end).trim());
});
return map;
}
function parseCameraNames(raw) {
if (typeof raw !== 'string' || !raw.trim()) return [];
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed : [];
} catch {
return [];
}
}
function positionFromName(name, index, type) {
if (type === 'front') return 'front';
if (/潜望/.test(name)) return 'periscope';
if (/超广角/.test(name)) return 'ultrawide';
if (/长焦/.test(name)) return 'tele';
if (/微距/.test(name)) return 'macro';
if (/景深|深感|3D|TOF/i.test(name)) return 'depth';
if (/黑白|mono/i.test(name)) return 'mono';
if (/红枫|原色/.test(name)) return 'color';
if (/广角|主摄|超光变/.test(name)) return 'wide';
return CAMERA_POSITION_FALLBACK[index] ?? 'wide';
}
function parseVideoMax(raw) {
const text = clean(raw);
const matches = [...text.matchAll(/(8K|4K|1080P|720P)/gi)].map((match) => {
const tail = text.slice(match.index + match[0].length, match.index + match[0].length + 32);
const fpsMatch = tail.match(/(\d+)\s*fps/i);
return { resolution: match[1].toUpperCase(), fps: fpsMatch ? Number(fpsMatch[1]) : 0 };
});
if (matches.length === 0) return '';
const rank = { '8K': 4, '4K': 3, '1080P': 2, '720P': 1 };
let best = matches[0];
for (const match of matches) {
const currentRank = rank[match.resolution] ?? 0;
const bestRank = rank[best.resolution] ?? 0;
if (currentRank > bestRank || (currentRank === bestRank && match.fps > best.fps)) best = match;
}
return best.fps ? `${best.resolution}@${best.fps}fps` : best.resolution;
}
function buildCameras(remote) {
const camera = remote.camera ?? {};
const names = parseCameraNames(camera.camera_names);
const pixelMap = parseCameraSegments(camera.pixels);
const apertureMap = parseCameraSegments(camera.aperture);
const focalMap = parseCameraSegments(camera.focal_length);
const lensMap = parseCameraSegments(camera.lens_elements);
const sensorMap = parseCameraSegments(camera.sensor_model);
const focusMap = parseCameraSegments(camera.focus_method);
const stabilizationMap = parseCameraSegments(camera.image_stabilization);
const rear = [];
const front = [];
const add = (item, side, index) => {
const key = `${side}-${String(item.position ?? index + 1)}`;
const resolution = pixelMap.get(key) ?? '';
const aperture = apertureMap.get(key) ?? '';
const focalLength = focalMap.get(key) ?? '';
const lensElements = lensMap.get(key) ?? '';
const sensorModel = sensorMap.get(key) ?? '';
const autofocusType = focusMap.get(key) ?? '';
const stabilization = stabilizationMap.get(key) ?? '';
if (!resolution && !aperture && !focalLength && !sensorModel) return;
const zoomText = clean(camera.zoom);
const optical = zoomText.match(/(\d+(?:\.\d+)?)\s*倍光学变焦/);
const digital = zoomText.match(/(\d+(?:\.\d+)?)\s*倍数码变焦/);
const zoom =
side === 'rear' && (optical || digital)
? [optical ? `${optical[1]}x 光学` : '', digital ? `${digital[1]}x 数码` : ''].filter(Boolean).join(' / ')
: '';
const videoMax = parseVideoMax(side === 'rear' ? camera.rear_video : camera.front_video);
const module = {
position: item.position_enum,
...(resolution ? { resolution } : {}),
...(aperture ? { aperture } : {}),
...(focalLength ? { focalLength } : {}),
...(lensElements ? { lensElements } : {}),
...(sensorModel ? { sensorModel } : {}),
...(autofocusType ? { autofocusType } : {}),
...(stabilization
? { ois: /OIS/i.test(stabilization), eis: /EIS/i.test(stabilization) }
: {}),
...(zoom && ['tele', 'periscope'].includes(item.position_enum) ? { zoom } : {}),
...(side === 'rear' && item.position_enum === 'ultrawide' && clean(camera.wide_angle)
? { fov: clean(camera.wide_angle) }
: {}),
...(side === 'rear' && index === 0 && clean(camera.flash) ? { flash: clean(camera.flash) } : {}),
...(videoMax && index === 0 ? { video: { max: videoMax, modes: [] } } : {}),
extra: {},
};
if (side === 'rear') rear.push(module);
else front.push(module);
};
const rearNames = names.filter((item) => item.type === 'rear');
const frontNames = names.filter((item) => item.type === 'front');
rearNames.forEach((item, index) =>
add(
{ ...item, position_enum: positionFromName(item.name, index, 'rear') },
'rear',
index,
),
);
frontNames.forEach((item, index) =>
add({ ...item, position_enum: 'front' }, 'front', index),
);
if (rear.length === 0 && front.length === 0) {
const totalText = clean(camera.camera_total);
const rearCount = Number((totalText.match(/后(\d+)/) ?? [])[1] ?? 0) || 3;
const frontCount = Number((totalText.match(/前(\d+)/) ?? [])[1] ?? 0) || 1;
for (let index = 0; index < rearCount; index += 1) {
add(
{ position: index + 1, position_enum: CAMERA_POSITION_FALLBACK[index] ?? 'wide' },
'rear',
index,
);
}
for (let index = 0; index < frontCount; index += 1) {
add({ position: index + 1, position_enum: 'front' }, 'front', index);
}
}
return { rear, front };
}
function buildSpeakers(remote) {
const hardware = remote.hardware ?? {};
const text = clean(hardware.speaker || hardware.speaker_count);
if (!text) return undefined;
const countMatch = text.match(/(\d+)\s*个/);
const count = countMatch ? Number(countMatch[1]) : /双|立体/.test(text) ? 2 : 1;
const models = [...text.matchAll(/\b(\d{3,4}[A-Za-z]?)\b/g)]
.map((match) => match[1])
.filter((model, index, list) => list.indexOf(model) === index)
.slice(0, 4);
const units = models.map((model, index) => ({
label: count > 1 ? `单元${index + 1}` : '扬声器',
model,
}));
if (units.length === 1 && count > 1) {
for (let index = 2; index <= count; index += 1) {
units.push({ ...units[0], label: `单元${index}` });
}
}
return {
count,
stereo: /立体|双/.test(text) || count >= 2,
units,
...( /杜比/.test(text) ? { dolbyAtmos: true } : {}),
...( /Hi-Res|金标/i.test(text) ? { hiRes: true } : {}),
};
}
function parseBands(raw) {
const text = clean(raw);
if (!text) return [];
const nrStart = text.search(/5G\s*NR[::]/);
const nrSection = nrStart >= 0 ? text.slice(nrStart) : '';
const nrBands = [...nrSection.matchAll(/n\d{1,3}/g)].map((match) => match[0]);
const lteStart = text.search(/4G\s*LTE/);
const lteSection =
lteStart >= 0 ? text.slice(lteStart, nrStart > lteStart ? nrStart : undefined) : '';
const lteBands = [...lteSection.matchAll(/\b(\d{1,3})\b/g)].map((match) => `B${match[1]}`);
return unique([...nrBands, ...lteBands]).slice(0, 80);
}
function buildSensors(remote) {
const features = remote.features ?? {};
const basic = remote.basic_info ?? {};
const list = splitList(String(features.sensors ?? '').replace(/注:[\s\S]*$/m, ''));
const fingerprint = clean(basic.fingerprint) || clean(basic.fingerprint_tech);
const fingerprintTypeMatch = `${fingerprint} ${clean(basic.fingerprint_tech)}`.match(
/(超声波|超声|光学|侧面|侧边|屏下|短焦)/,
);
return {
...(fingerprint ? { fingerprint } : {}),
...(fingerprintTypeMatch ? { fingerprintType: fingerprintTypeMatch[1] } : {}),
...(fingerprint ? { fingerprintPosition: fingerprint } : {}),
...(list.some((item) => /陀螺仪/.test(item)) ? { gyro: true } : {}),
...(list.some((item) => /气压/.test(item)) ? { barometer: true } : {}),
...(list.some((item) => /色温/.test(item)) ? { colorSensor: true } : {}),
...(list.some((item) => /闪烁|flicker/i.test(item)) ? { flickerSensor: true } : {}),
...(list.length > 0 ? { list } : {}),
};
}
/* ------------------------------ 单机映射 ------------------------------ */
function mapPhone(remote, index) {
const brandSource = clean(remote.brand);
const brand = BRAND_MAP[brandSource] ?? brandSource ?? 'Unknown';
const basic = remote.basic_info ?? {};
const rawModel = clean(remote.model);
const productModel = clean(basic.product_model);
const stripBrand = (value) => {
for (const prefix of [brandSource, brand]) {
if (prefix && value.toLowerCase().startsWith(prefix.toLowerCase())) {
return value.slice(prefix.length).trim();
}
}
return value;
};
// 源站个别机型 model 只写了品牌,退回 product_model 并去掉品牌前缀
const model =
rawModel && rawModel.toLowerCase() !== brand.toLowerCase()
? rawModel
: stripBrand(productModel) || rawModel || productModel || `Phone ${index + 1}`;
const dimensions = remote.dimensions ?? {};
const hardware = remote.hardware ?? {};
const screen = remote.screen ?? {};
const camera = remote.camera ?? {};
const network = remote.network ?? {};
const battery = remote.battery ?? {};
const features = remote.features ?? {};
const perVersion = remote.per_version ?? {};
const versions = Array.isArray(remote.versions) ? remote.versions : [];
const releaseDate =
toIsoDate(basic.launch_date) ??
toIsoDate(basic.domestic_release_date) ??
(typeof basic.release_date_timestamp === 'number'
? new Date(basic.release_date_timestamp).toISOString().slice(0, 10)
: undefined);
const announceDate = toIsoDate(basic.domestic_release_date) ?? releaseDate;
const lifecycle = releaseDate && releaseDate > new Date().toISOString().slice(0, 10)
? 'announced'
: 'released';
const display = {
...(clean(screen.screen_size) ? { size: clean(screen.screen_size) } : {}),
...(clean(screen.resolution)
? { resolution: clean(screen.resolution).replace(/\s*像素$/, '').trim() }
: {}),
...(clean(screen.screen_material) ? { panel: clean(screen.screen_material) } : {}),
};
const refreshText = clean(screen.refresh_rate);
const refreshRangeMatch = refreshText.match(/(\d+)\s*[-~至]\s*(\d+)\s*Hz/);
const refreshNumbers = [...refreshText.matchAll(/(\d+)\s*Hz/g)].map((match) => Number(match[1]));
if (refreshRangeMatch) {
display.refreshRate = `${refreshRangeMatch[2]}Hz`;
display.refreshRange = `${refreshRangeMatch[1]}-${refreshRangeMatch[2]}Hz`;
} else if (refreshNumbers.length > 0) {
display.refreshRate = `${Math.max(...refreshNumbers)}Hz`;
}
if (refreshText && unique(refreshNumbers).length > 1) display.refreshNote = refreshText;
const touchText = clean(screen.touch_sampling_rate);
const instantMatch = touchText.match(/瞬时(?:采样率)?[::]?\s*(\d+)\s*Hz/);
if (/最高\s*\d+\s*Hz/.test(touchText)) display.touchSampling = touchText.match(/最高\s*\d+\s*Hz/)[0];
if (instantMatch) display.touchSamplingInstant = `${instantMatch[1]}Hz`;
if (clean(screen.screen_color)) display.colorDepth = clean(screen.screen_color).split(/[,,]/)[0].trim();
if (clean(screen.pixel_density)) display.pixelDensity = clean(screen.pixel_density);
if (clean(screen.screen_to_body_ratio)) display.screenBodyRatio = clean(screen.screen_to_body_ratio);
const brightness = parseBrightness(screen.screen_brightness);
Object.assign(display, brightness);
if (clean(screen.glass)) display.protection = clean(screen.glass);
if (clean(screen.dimming_method)) display.pwmDimming = truncate(clean(screen.dimming_method), 180);
if (/^支持/.test(clean(screen.aod))) display.alwaysOn = true;
const screenPoints = splitList(screen.selling_points);
if (screenPoints.length > 0) display.features = screenPoints;
const buildRaw = clean(basic.body_material);
const buildParts = parseLabeled(buildRaw, ['屏幕盖板材质', '中框材质', '电池盖材质', '后盖', '边框']);
const bodyBuild = {
...(buildParts['屏幕盖板材质'] ? { front: buildParts['屏幕盖板材质'] } : {}),
...(buildParts['中框材质'] || buildParts['边框']
? { frame: buildParts['中框材质'] || buildParts['边框'] }
: {}),
...(buildParts['电池盖材质'] || buildParts['后盖']
? { back: buildParts['电池盖材质'] || buildParts['后盖'] }
: {}),
};
if (Object.keys(bodyBuild).length === 0 && buildRaw) bodyBuild.back = buildRaw;
const length = firstNumber(dimensions.length);
const width = firstNumber(dimensions.width);
const thickness = firstNumber(dimensions.thickness);
const ipMatches = clean(features.waterproof ?? '').match(/IP\d{2}(?:K)?/g);
const body = {
...(length && width && thickness ? { dimensions: `${length} x ${width} x ${thickness} mm` } : {}),
...(clean(dimensions.weight) ? { weight: normalizeMetric(dimensions.weight) } : {}),
...(Object.keys(bodyBuild).length > 0 ? { build: bodyBuild } : {}),
...(clean(network.sim_type) ? { sim: clean(network.sim_type) } : {}),
...(ipMatches ? { ipRating: unique(ipMatches).join(',') } : {}),
...(clean(hardware.cooling) ? { cooling: truncate(clean(hardware.cooling), 300) } : {}),
...(splitList(basic.body_color).length > 0 ? { colors: splitList(basic.body_color) } : {}),
};
const osParts = parseOs(hardware.os);
const ramValues = unique(versions.map((version) => clean(version.ram)));
if (ramValues.length === 0 && clean(perVersion.ram_capacity)) ramValues.push(clean(perVersion.ram_capacity));
const storageValues = unique(versions.map((version) => clean(version.rom)));
if (storageValues.length === 0 && clean(perVersion.rom_capacity)) {
storageValues.push(clean(perVersion.rom_capacity).split(/\s/)[0]);
}
const memory = {
...(ramValues.length > 0 ? { ram: ramValues.join(' / ') } : {}),
...(clean(perVersion.ram_type) || clean(features['运行内存'])
? { ramType: clean(perVersion.ram_type) || clean(features['运行内存']) }
: {}),
...(storageValues.length > 0 ? { storage: storageValues.join(' / ') } : {}),
...(clean(perVersion.rom_type) || clean(features['机身存储'])
? { storageType: clean(perVersion.rom_type) || clean(features['机身存储']) }
: {}),
...(clean(hardware.storage_card) ? { cardSlot: clean(hardware.storage_card) } : {}),
};
const cameras = buildCameras(remote);
const speakerText = clean(hardware.speaker || hardware.speaker_count);
const speakerModel = speakerText.match(/\b\d{3,4}[A-Za-z]?\b/);
const vibrationText = clean(hardware.vibration_motor);
const vibrationModel = vibrationText.match(/\b(\d{3,4})\b/);
const pricing = versions
.filter((version) => typeof version.price === 'number')
.sort((a, b) => a.price - b.price)
.map((version) => `${clean(version.version_name) || '版本'} ${version.price} 元`);
const rawAll = JSON.stringify([remote.features, remote.network, remote.other_params]);
const satellite = /卫星通话|卫星通信|天通卫星/.test(rawAll);
const satelliteProvider = satellite
? /天通/.test(rawAll)
? '天通'
: /北斗/.test(rawAll)
? '北斗'
: undefined
: undefined;
const specs = {
body: Object.keys(body).length > 0 ? body : undefined,
display: Object.keys(display).length > 0 ? display : undefined,
platform: {
...(osParts.os ? { os: osParts.os } : {}),
...(osParts.skin ? { skin: osParts.skin } : {}),
...(clean(hardware.cpu_model) ? { chipset: clean(hardware.cpu_model) } : {}),
},
memory: Object.keys(memory).length > 0 ? memory : undefined,
rearCamera: cameras.rear,
frontCamera: cameras.front,
vibration: vibrationText
? {
type: vibrationText,
...(vibrationModel ? { size: vibrationModel[1] } : {}),
}
: undefined,
speakers: buildSpeakers(remote),
connectivity: {
...(clean(network.network_type) ? { cellular: clean(network.network_type).replace(/\s*,\s*/g, ' / ') } : {}),
...(parseBands(network.network_bands).length > 0 ? { bands: parseBands(network.network_bands) } : {}),
...(satellite ? { satellite: true } : {}),
...(satelliteProvider ? { satelliteProvider } : {}),
...(clean(network.wlan)
? { wifi: truncate(sanitizeSourceText(network.wlan), 300) }
: {}),
...(clean(network.bluetooth)
? { bluetooth: truncate(sanitizeSourceText(network.bluetooth), 200) }
: {}),
...(/^支持/.test(clean(network.nfc)) ? { nfc: true } : {}),
...(clean(network.navigation) ? { gps: sanitizeSourceText(network.navigation) } : {}),
...([clean(network.body_interface), clean(hardware.usb_version)].filter(Boolean).length > 0
? { usb: [clean(network.body_interface), clean(hardware.usb_version)].filter(Boolean).join(',') }
: {}),
...(/^支持/.test(clean(network.infrared)) ? { ir: true } : {}),
...(/UWB/i.test(rawAll) ? { uwb: true } : {}),
},
battery: {
...(clean(battery.battery_capacity) ? { capacity: clean(battery.battery_capacity) } : {}),
...(clean(battery.battery_type) ? { type: clean(battery.battery_type) } : {}),
...(clean(battery.wired_charging) ? { wired: clean(battery.wired_charging) } : {}),
...(clean(battery.wireless_charging) ? { wireless: clean(battery.wireless_charging) } : {}),
...(clean(battery.reverse_charging) ? { reverse: clean(battery.reverse_charging) } : {}),
...(clean(battery.charging) ? { chargingProtocol: truncate(clean(battery.charging), 300) } : {}),
},
sensors: buildSensors(remote),
software: {
...(clean(hardware.os) ? { launchVersion: clean(hardware.os) } : {}),
},
features: splitList(basic.use_scenario),
cameraFeatures: splitList(camera.rear_camera_features).slice(0, 30),
benchmarks: [],
pricing,
...(clean(remote.official_price_url) ? { officialUrl: clean(remote.official_price_url) } : {}),
extra: {},
};
const extra = {
源站机型ID: String(remote._id ?? ''),
数据来源: `${BASE}/phone/${remote._id}`,
};
if (clean(remote.other_params)) extra['其他参数'] = truncate(clean(remote.other_params), 400);
if (clean(dimensions.other_dimensions)) extra['尺寸/重量说明'] = truncate(clean(dimensions.other_dimensions), 300);
if (clean(camera.zoom)) extra['变焦说明'] = truncate(clean(camera.zoom), 200);
if (clean(camera.other_camera_params)) extra['影像补充'] = truncate(clean(camera.other_camera_params), 400);
if (clean(features.audio_effects)) extra['音效'] = clean(features.audio_effects);
if (clean(features.in_the_box)) extra['包装清单'] = truncate(clean(features.in_the_box), 300);
if (clean(basic.face_recognition)) extra['面部识别'] = clean(basic.face_recognition);
if (clean(hardware.microphone_count)) extra['麦克风'] = clean(hardware.microphone_count);
if (clean(perVersion.storage)) extra['存储版本'] = clean(perVersion.storage);
if (speakerModel) extra['扬声器单元原文'] = speakerText;
specs.extra = extra;
const summaryParts = [
display.size,
display.panel,
clean(hardware.cpu_model),
clean(battery.battery_capacity),
clean(battery.wired_charging) ? `${clean(battery.wired_charging)} 有线充电` : '',
].filter(Boolean);
const summary = truncate(summaryParts.join(','), 120);
const aliases = unique([
`${brandSource} ${model}`.trim(),
clean(basic.product_model),
`${brand} ${model}`,
]);
const imageId = String(remote._id ?? '');
const coverSource = clean(remote.images?.cover_image);
const gallerySources = unique(
(Array.isArray(remote.images?.gallery_images) ? remote.images.gallery_images : [])
.map(clean)
.filter((key) => key.startsWith(`phones/${imageId}/`) && key !== coverSource),
);
const brandSlug = slugify(brand);
const modelSlug = slugify(model);
let slug = modelSlug.startsWith(`${brandSlug}-`) ? modelSlug : `${brandSlug}-${modelSlug}`;
slug = slug.slice(0, 80).replace(/-+$/g, '');
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(slug)) slug = `${brandSlug}-${imageId}`;
const device = {
slug,
category: 'phone',
brand,
model,
aliases,
announceDate,
releaseDate,
lifecycle,
tags: splitList(basic.use_scenario).slice(0, 12),
summary: summary || undefined,
chipRefs: {},
specs,
meta: [],
};
// 图片 key 依赖最终 slug(可能因重名追加后缀),等 slug 确定后再写入
deviceImageSources.set(device, { cover: coverSource, gallery: gallerySources });
device.meta = buildMeta(device, `${BASE}/phone/${imageId}`);
return device;
}
function buildMeta(device, sourceUrl) {
const meta = [];
const capturedAt = new Date().toISOString();
const add = (path) => {
if (meta.length >= 80) return;
meta.push({ path, confidence: 'medium', sourceUrl, verified: false, capturedAt });
};
const walk = (value, path) => {
if (meta.length >= 80) return;
if (Array.isArray(value)) {
if (value.length === 0) return;
if (value.every((item) => typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean')) {
add(path);
return;
}
value.forEach((item, index) => walk(item, `${path}[${index}]`));
return;
}
if (value && typeof value === 'object') {
for (const [key, item] of Object.entries(value)) walk(item, `${path}.${key}`);
return;
}
if (typeof value === 'string' && value.trim()) add(path);
else if (typeof value === 'number' || typeof value === 'boolean') add(path);
};
for (const [group, value] of Object.entries(device.specs)) {
if (group === 'extra') continue;
walk(value, `specs.${group}`);
}
for (const key of ['slug', 'brand', 'model', 'summary']) {
if (typeof device[key] === 'string' && device[key]) add(key);
}
if (device.aliases.length > 0) add('aliases');
if (device.tags.length > 0) add('tags');
if (device.announceDate) add('announceDate');
if (device.releaseDate) add('releaseDate');
return meta;
}
/* ------------------------------ 图片 ------------------------------ */
/** 图片落到 data/uploads/devices/<slug>/...,页面统一用 slug 路径读取 */
const imageSources = new Map();
const deviceImageSources = new WeakMap();
const localImageKey = (slug, name) => `devices/${slug}/${name}.webp`;
/** slug / 重名后缀确定后,再生成封面与相册的本地 key */
function applyImageKeys(device) {
const sources = deviceImageSources.get(device);
if (!sources) return;
const cover = sources.cover ? localImageKey(device.slug, 'cover') : undefined;
const gallery = sources.gallery.map((source, index) => {
const key = localImageKey(device.slug, `gallery-${index + 1}`);
imageSources.set(key, source);
return key;
});
if (cover) imageSources.set(cover, sources.cover);
device.cover = cover;
device.gallery = gallery;
}
async function downloadImage(key, attempt = 1) {
const source = imageSources.get(key);
if (!source) return 'missing';
const target = join(storageRoot(), key);
if (existsSync(target)) return 'skip';
try {
const response = await fetch(`${BASE}/images/${source}`, {
headers: { ...HEADERS, Accept: 'image/avif,image/webp,image/apng,image/*,*/*;q=0.8' },
signal: AbortSignal.timeout(20000),
});
if (!response.ok) {
if (response.status === 404) return 'missing';
throw new Error(`HTTP ${response.status}`);
}
const contentType = response.headers.get('content-type') ?? '';
if (!contentType.startsWith('image/')) return 'not-image';
const buffer = Buffer.from(await response.arrayBuffer());
// 源站存在 .png 实为 JPEG 的文件,统一转 WebP,避免扩展名与内容不一致
const webp = await sharp(buffer).webp({ quality: 82 }).toBuffer();
await getStorage().put(key, new Uint8Array(webp), 'image/webp');
return 'ok';
} catch (error) {
if (attempt >= 2) throw error;
await new Promise((resolve) => setTimeout(resolve, 200 * attempt));
return downloadImage(key, attempt + 1);
}
}
async function downloadImages(devices, concurrency) {
const jobs = [];
for (const device of devices) {
if (device.cover) jobs.push(device.cover);
for (const key of device.gallery) jobs.push(key);
}
const uniqueJobs = unique(jobs);
const stats = { ok: 0, skip: 0, missing: 0, notImage: 0, fail: 0 };
let cursor = 0;
async function worker() {
while (cursor < uniqueJobs.length) {
const key = uniqueJobs[cursor];
cursor += 1;
try {
const result = await downloadImage(key);
stats[result] += 1;
} catch (error) {
stats.fail += 1;
console.warn(` 图片失败 ${key}: ${error instanceof Error ? error.message : error}`);
}
const done = stats.ok + stats.skip + stats.missing + stats.notImage + stats.fail;
if (done % 100 === 0) console.log(` 图片进度 ${done}/${uniqueJobs.length}`);
}
}
await Promise.all(Array.from({ length: Math.min(concurrency, uniqueJobs.length) }, () => worker()));
return { total: uniqueJobs.length, ...stats };
}
/** 下载完成后去掉本地不存在的图片引用,避免页面上出现破图 */
function pruneMissingImages(devices) {
let removed = 0;
for (const device of devices) {
if (device.cover && !existsSync(join(storageRoot(), device.cover))) {
device.cover = undefined;
removed += 1;
}
const kept = device.gallery.filter((key) => existsSync(join(storageRoot(), key)));
removed += device.gallery.length - kept.length;
device.gallery = kept;
}
return removed;
}
/* ------------------------------- 主流程 ------------------------------- */
async function main() {
const options = parseArgs(process.argv.slice(2));
console.log(`拉取 ${BASE}/api/phones ...`);
const { phones, total } = await fetchAllPhones(options.pageSize);
console.log(`源站共 ${total} 款,已获取 ${phones.length} 款`);
let filtered = phones;
if (options.brands.length > 0) {
const wanted = new Set(
options.brands.flatMap((brand) => [brand, BRAND_MAP[brand]]).filter(Boolean),
);
filtered = filtered.filter((phone) => wanted.has(clean(phone.brand)));
}
if (options.limit > 0) filtered = filtered.slice(0, options.limit);
console.log(`本次处理 ${filtered.length} 款`);
const devices = [];
const issues = [];
const slugOwner = new Map();
const duplicateOwner = new Map();
filtered.forEach((phone, index) => {
try {
const device = mapPhone(phone, index);
const duplicateKey = `${device.brand}|${device.model}|${device.releaseDate ?? device.announceDate ?? ''}`.toLowerCase();
if (duplicateOwner.has(duplicateKey)) {
console.warn(
` 跳过重复机型:${phone.brand} ${phone.model}(与 ${duplicateOwner.get(duplicateKey)} 重复)`,
);
return;
}
const owner = slugOwner.get(device.slug);
if (owner) {
let suffix = 2;
let candidate = `${device.slug}-${suffix}`;
while (slugOwner.has(candidate)) {
suffix += 1;
candidate = `${device.slug}-${suffix}`;
}
console.warn(` slug 冲突:${device.slug}(${owner} 与 ${phone.brand} ${phone.model}),改用 ${candidate}`);
device.slug = candidate;
}
slugOwner.set(device.slug, `${phone.brand} ${phone.model}`);
applyImageKeys(device);
const parsed = deviceSchema.safeParse(device);
if (!parsed.success) {
issues.push({
name: `${phone.brand} ${phone.model}`,
issues: parsed.error.issues.slice(0, 8).map((issue) => `${issue.path.join('.')}: ${issue.message}`),
});
return;
}
devices.push(parsed.data);
duplicateOwner.set(duplicateKey, `${phone.brand} ${phone.model}`);
} catch (error) {
issues.push({ name: `${phone?.brand ?? '?'} ${phone?.model ?? '?'}`, issues: [String(error)] });
}
});
if (issues.length > 0) {
console.error(`\n有 ${issues.length} 款映射失败:`);
for (const issue of issues.slice(0, 20)) {
console.error(`- ${issue.name}`);
for (const line of issue.issues) console.error(` ${line}`);
}
throw new Error('映射失败,已中止,未写入任何数据');
}
console.log(`映射成功 ${devices.length} 款`);
if (options.images) {
console.log('开始下载封面与相册 ...');
const stats = await downloadImages(devices, options.concurrency);
console.log(
`图片完成:共 ${stats.total},新增 ${stats.ok},已存在 ${stats.skip},缺失 ${stats.missing},非图片 ${stats.notImage},失败 ${stats.fail}`,
);
const removed = pruneMissingImages(devices);
if (removed > 0) console.log(`已清理 ${removed} 个本地缺失的图片引用`);
}
const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const outPath = options.out || `data/import/phonerecommend-${stamp}.json`;
const payload = {
formatVersion: 1,
generator: 'LFSpecs / sync-phonerecommend',
exportedAt: new Date().toISOString(),
devices,
chips: [],
};
await mkdir(dirname(outPath), { recursive: true });
await writeFile(outPath, `${JSON.stringify(payload, null, 2)}\n`, 'utf8');
console.log(`JSON 已写入 ${outPath}(${devices.length} 款)`);
if (options.write) {
console.log('写入数据库 ...');
const counts = importEntities(devices, []);
console.log(`数据库写入完成:devices ${counts.devices} / chips ${counts.chips}`);
} else {
console.log('未指定 --write,本次只产出 JSON。');
}
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});