修复三个 bug:清理 API 响应中的 null 字段、改用 Google Translate 提升翻译质量、过滤 README 中的分隔线

This commit is contained in:
MikiVL 2026-05-05 04:02:25 +08:00
parent 34ce3a3cf9
commit ab172e3d77
3 changed files with 19 additions and 13 deletions

View File

@ -9,7 +9,8 @@ function computeDaysUntilDeletion(row) {
} }
function formatRepo(row) { function formatRepo(row) {
return { ...row, days_until_deletion: computeDaysUntilDeletion(row) }; const { scraped_at, is_today, ...rest } = row;
return { ...rest, days_until_deletion: computeDaysUntilDeletion(row) };
} }
function getRepos(req, res) { function getRepos(req, res) {

View File

@ -89,13 +89,14 @@ function extractFeaturesSection(readme, description) {
if (result && result.length > 20) return result; if (result && result.length > 20) return result;
} }
// 兜底取README正文第一段有意义的文字跳过徽章行 // 兜底取README正文第一段有意义的文字跳过徽章行和分隔线
const lines = readme.split('\n'); const lines = readme.split('\n');
const paragraphs = []; const paragraphs = [];
for (const line of lines) { for (const line of lines) {
const trimmed = line.trim(); const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('!') || trimmed.startsWith('[!') || trimmed.startsWith('<')) continue; if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('!') || trimmed.startsWith('[!') || trimmed.startsWith('<')) continue;
if (trimmed.startsWith('[![') || trimmed.match(/^\[!\[/)) continue; if (trimmed.startsWith('[![') || trimmed.match(/^\[!\[/)) continue;
if (/^[-=]{3,}$/.test(trimmed)) continue;
if (trimmed.length > 40) paragraphs.push(trimmed); if (trimmed.length > 40) paragraphs.push(trimmed);
if (paragraphs.length >= 3) break; if (paragraphs.length >= 3) break;
} }

View File

@ -6,8 +6,8 @@ const HTML_TAG_RE = /<[^>]{1,100}>/g;
function cleanText(text) { function cleanText(text) {
if (!text) return text; if (!text) return text;
return text return text
.replace(HTML_TAG_RE, ' ') // 去除 HTML 标签 .replace(HTML_TAG_RE, ' ')
.replace(/&[a-z]+;/gi, ' ') // 去除 HTML 实体 .replace(/&[a-z]+;/gi, ' ')
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
.trim(); .trim();
} }
@ -15,21 +15,25 @@ function cleanText(text) {
async function translate(text) { async function translate(text) {
const cleaned = cleanText(text); const cleaned = cleanText(text);
if (!cleaned || cleaned.length < 5) return cleaned || text; if (!cleaned || cleaned.length < 5) return cleaned || text;
if (CJK_RE.test(cleaned)) return cleaned; // 已含中文,跳过 if (CJK_RE.test(cleaned)) return cleaned;
try { try {
const res = await axios.get('https://api.mymemory.translated.net/get', { const res = await axios.get('https://translate.googleapis.com/translate_a/single', {
params: { q: cleaned.slice(0, 500), langpair: 'en|zh-CN' }, params: {
headers: { 'User-Agent': 'github-trending-app/1.0' }, client: 'gtx',
sl: 'en',
tl: 'zh-CN',
dt: 't',
q: cleaned.slice(0, 500),
},
headers: { 'User-Agent': 'Mozilla/5.0' },
timeout: 10000, timeout: 10000,
}); });
const translated = res.data?.responseData?.translatedText; const translated = res.data?.[0]?.map(s => s?.[0]).filter(Boolean).join('');
if (!translated || translated === cleaned || translated.startsWith('PLEASE SELECT')) { if (!translated || translated === cleaned) return cleaned;
return cleaned;
}
return translated.trim(); return translated.trim();
} catch { } catch {
return cleaned; // 失败静默降级,返回清理后原文 return cleaned;
} }
} }