fix(route)(nikkei-cn): imprudent slice; selector lack; duplicated article (#9337)

Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com>
This commit is contained in:
Rongrong
2022-03-18 21:32:09 +08:00
committed by GitHub
parent 2ed9fd5c5f
commit 933080a1f5
+10 -5
View File
@@ -14,6 +14,8 @@ const cleanContent = (language, content) => {
}
};
const getPubDate = (link) => timezone(parseDate(link.match(/(\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2})/)[1], 'YYYY-MM-DD-HH-mm-ss'), +9);
module.exports = async (ctx) => {
const language = ctx.params.language ?? 'cn';
const category = ctx.params.category ?? '';
@@ -33,9 +35,9 @@ module.exports = async (ctx) => {
category === 'rss'
? response.data.match(/<link>(http:\/\/.*\.html)<\/link>/g).map((l) => ({
link: l.replace(/<(\/)?link>/g, '').replace(/http:/g, 'https:'),
pubDate: getPubDate(l),
}))
: $('dt a')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 25)
: $('dt a, p.newsDetailTtl a, ul.listStyle01 li a, table.sliderContent3 td a')
.toArray()
.map((item) => {
item = $(item);
@@ -43,9 +45,13 @@ module.exports = async (ctx) => {
const link = item.attr('href');
return {
link: `${link.indexOf('http') === 0 ? '' : rootUrl}${link}`,
link: `${link.startsWith('http') ? '' : rootUrl}${link}`,
pubDate: getPubDate(link),
};
});
})
.sort((a, b) => b.pubDate - a.pubDate) // desc
.reduce((prev, cur) => (prev.length && prev[prev.length - 1].link === cur.link ? prev : [...prev, cur]), []) // deduplicate
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 25);
items = await Promise.all(
items.map((item) =>
@@ -62,7 +68,6 @@ module.exports = async (ctx) => {
item.author = content('meta[name="author"]').attr('content');
item.title = content('meta[name="twitter:title"]').attr('content');
item.description = cleanContent(language, content('#contentDiv').html());
item.pubDate = timezone(parseDate(item.link.match(/(\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2})/)[1], 'YYYY-MM-DD-HH-mm-ss'), +9);
return item;
})