From a1c0e755420a9057bfdb763ca2910e6c48b4870a Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 14 Jan 2024 18:11:00 +0000 Subject: [PATCH] fix(route): tencent author (#14241) --- lib/v2/tencent/news/author.js | 24 ++++++++++++++++++++---- lib/v2/tencent/templates/news/image.art | 4 ++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lib/v2/tencent/templates/news/image.art diff --git a/lib/v2/tencent/news/author.js b/lib/v2/tencent/news/author.js index 6921419346..d5fb5bb160 100644 --- a/lib/v2/tencent/news/author.js +++ b/lib/v2/tencent/news/author.js @@ -1,8 +1,9 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const { parseDate } = require('@/utils/parse-date'); -const timezone = require('@/utils/timezone'); const config = require('@/config').value; +const { art } = require('@/utils/render'); +const { join } = require('path'); module.exports = async (ctx) => { const mid = ctx.params.mid; @@ -18,7 +19,7 @@ module.exports = async (ctx) => { const items = await Promise.all( news.map((item) => { const title = item.title; - const pubDate = timezone(parseDate(item.time), +8); + const pubDate = parseDate(item.timestamp, 'X'); const itemUrl = item.url; const author = item.source; const abstract = item.abstract; @@ -26,11 +27,26 @@ module.exports = async (ctx) => { return ctx.cache.tryGet(itemUrl, async () => { const response = await got(itemUrl); const $ = cheerio.load(response.data); - const article = $('#ArticleContent'); + const data = JSON.parse( + $('script:contains("window.DATA")') + .text() + .match(/window\.DATA = (\{.+\});/)[1] + ); + const $data = cheerio.load(data.originContent.text, null, false); + + $data('*') + .contents() + .filter((_, elem) => elem.type === 'comment') + .replaceWith((_, elem) => + art(join(__dirname, '../templates/news/image.art'), { + attribute: elem.data.trim(), + originAttribute: data.originAttribute, + }) + ); return { title, - description: article.html() || abstract, + description: $data.html() || abstract, link: itemUrl, author, pubDate, diff --git a/lib/v2/tencent/templates/news/image.art b/lib/v2/tencent/templates/news/image.art new file mode 100644 index 0000000000..04120bbbfe --- /dev/null +++ b/lib/v2/tencent/templates/news/image.art @@ -0,0 +1,4 @@ +{{ if attribute?.startsWith('IMG') && originAttribute[attribute] }} + {{ set image = originAttribute[attribute] }} + +{{ /if }}