Skip to content

Commit

Permalink
fix(route): tencent author (DIYgod#14241)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Jan 14, 2024
1 parent 7a94c42 commit a1c0e75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/v2/tencent/news/author.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,19 +19,34 @@ 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;

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,
Expand Down
4 changes: 4 additions & 0 deletions lib/v2/tencent/templates/news/image.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ if attribute?.startsWith('IMG') && originAttribute[attribute] }}
{{ set image = originAttribute[attribute] }}
<img src="{{ image.bigOrigUrl }}" style="{{ image.style }}" >
{{ /if }}

0 comments on commit a1c0e75

Please sign in to comment.