fixed: 现在能显示作者名称

This commit is contained in:
meishibiezb
2026-05-04 22:36:37 +08:00
parent 696abd2e05
commit a5c5b0b38c
4 changed files with 76 additions and 19 deletions

View File

@@ -146,3 +146,67 @@ export function collectPins(): PinData[] {
const collector = new PinCollector();
return collector.collect();
}
/**
* 从花瓣 API 响应中的单个 pin 对象提取 PinData
* @param pin - /v3/boards/{id}/pins 返回的 pins 数组元素
*/
export function extractPinFromApi(pin: any): PinData {
return {
url: `/pins/${pin.pin_id}/`,
imgSmallSrc: pin.file?.url ?? '',
imgSrc: (pin.file?.url ?? '').replace(/_fw\d+webp|_png/, ''),
imgWidth: pin.file?.width ?? 0,
imgHeight: pin.file?.height ?? 0,
alt: pin.raw_text ?? '',
title: pin.raw_text ?? '',
author: parseAuthorFromRawText(pin.raw_text) ?? '',
time: formatTimestamp(pin.created_at) ?? '',
tags: extractTagsFromText(pin.raw_text),
};
}
function formatTimestamp(ts: number): string {
if (!ts) return '';
const date = new Date(ts * 1000); // API 给的是秒JS 需要毫秒
const now = Date.now();
const diff = now - date.getTime();
const hours = Math.floor(diff / 3600000);
if (hours < 24) return `${hours}小时`;
const days = Math.floor(hours / 24);
if (days < 30) return `${days}`;
return date.toLocaleDateString('zh-CN'); // 超过30天直接显示日期
}
/**
* 从 raw_text 中解析作者名
*/
function parseAuthorFromRawText(rawText: string): string {
if (!rawText) return '';
const lines = rawText.split('\n').map(s => s.trim()).filter(Boolean);
if (lines.length === 0) return '';
const firstLine = lines[0];
// 格式: "北道(きたみち)かえるAiart" → 去掉 @xxx 后缀
const atIndex = firstLine.indexOf('@');
if (atIndex > 0) return firstLine.slice(0, atIndex).trim();
// 格式: "@无硫火花 的个人主页 - 微博" → 取 @ 后到空格前的用户名
if (atIndex === 0) {
const username = firstLine.slice(1).split(/\s/)[0];
return username || firstLine;
}
// 格式: "Z3zz_的照片 - 微相册"
return firstLine;
}
/**
* 从文本中提取 #标签
*/
export function extractTagsFromText(text: string): string[] {
const tags: string[] = [];
const tagRegex = /#([\w\u4e00-\u9fff]+)/g;
let match;
while ((match = tagRegex.exec(text)) !== null) {
tags.push(match[1]);
}
return tags;
}