fix(route): 界面新闻重复文章视频 (#14162)

* fix(route): 界面新闻重复文章视频

* fix: improve url sanitization
This commit is contained in:
Ethan Shen 2024-01-02 22:38:46 +08:00 committed by GitHub
parent 8dcbbb50ec
commit 78c4977275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 48 deletions

View File

@ -15,62 +15,69 @@ module.exports = async (ctx) => {
const $ = cheerio.load(response);
let items = $('a.logStore, a[aid], div.title a, div.news-header a')
let items = $('a')
.toArray()
.map((item) => {
.reduce((acc, item) => {
item = $(item);
return {
title: item.text(),
link: item.prop('href'),
};
})
.filter((item) => item.link && !item.link.startsWith(new URL('special', rootUrl).href));
const href = item.prop('href');
const link = href ? (href.startsWith('/') ? new URL(href, rootUrl).href : href) : undefined;
if (link && /\/(article|video)\/\w+\.html/.test(link)) {
acc[link] = {
title: item.text(),
link,
};
}
return acc;
}, {});
items = await Promise.all(
items.slice(0, limit).map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: detailResponse } = await got(item.link);
Object.values(items)
.slice(0, limit)
.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: detailResponse } = await got(item.link);
const content = cheerio.load(detailResponse);
const image = content('div.article-img img').first();
const video = content('#video-player').first();
const content = cheerio.load(detailResponse);
const image = content('div.article-img img').first();
const video = content('#video-player').first();
item.title = content('div.article-header h1').text();
item.description = art(path.join(__dirname, 'templates/description.art'), {
image: image
? {
src: image.prop('src'),
alt: image.next('p').text() || item.title,
}
: undefined,
video: video
? {
src: video.prop('data-url'),
poster: video.prop('data-poster'),
width: video.prop('width'),
height: video.prop('height'),
}
: undefined,
intro: content('div.article-header p').text(),
description: content('div.article-content').html(),
});
item.author = content('span.author')
.first()
.find('a')
.toArray()
.map((a) => content(a).text())
.join('/');
item.category = content('meta.meta-container a')
.toArray()
.map((c) => content(c).text());
item.pubDate = parseDate(content('div.article-info span[data-article-publish-time]').prop('data-article-publish-time'), 'X');
item.upvotes = content('span.opt-praise__count').text() ? parseInt(content('span.opt-praise__count').text(), 10) : 0;
item.comments = content('span.opt-comment__count').text() ? parseInt(content('span.opt-comment__count').text(), 10) : 0;
item.title = content('div.article-header h1').text();
item.description = art(path.join(__dirname, 'templates/description.art'), {
image: image
? {
src: image.prop('src'),
alt: image.next('p').text() || item.title,
}
: undefined,
video: video
? {
src: video.prop('data-url'),
poster: video.prop('data-poster'),
width: video.prop('width'),
height: video.prop('height'),
}
: undefined,
intro: content('div.article-header p').text(),
description: content('div.article-content').html(),
});
item.author = content('span.author')
.first()
.find('a')
.toArray()
.map((a) => content(a).text())
.join('/');
item.category = content('meta.meta-container a')
.toArray()
.map((c) => content(c).text());
item.pubDate = parseDate(content('div.article-info span[data-article-publish-time]').prop('data-article-publish-time'), 'X');
item.upvotes = content('span.opt-praise__count').text() ? parseInt(content('span.opt-praise__count').text(), 10) : 0;
item.comments = content('span.opt-comment__count').text() ? parseInt(content('span.opt-comment__count').text(), 10) : 0;
return item;
})
)
return item;
})
)
);
const title = $('title').text();