diff --git a/lib/v2/nikkei-cn/index.js b/lib/v2/nikkei-cn/index.js index f9f73944bc..37db642014 100644 --- a/lib/v2/nikkei-cn/index.js +++ b/lib/v2/nikkei-cn/index.js @@ -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(/(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; })