fix(route): 修复掘金文章详情获取 (#8160)

Co-authored-by: SettingDust <settingdust@gmail.com>
This commit is contained in:
tgly307 2021-09-08 13:58:35 +08:00 committed by GitHub
parent d98cc18725
commit 62a01c8ea1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 23 deletions

View File

@ -1,30 +1,22 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const cheerio = require('cheerio');
const md = require('markdown-it')({
html:true
});
// 加载文章页
async function load(link) {
async function load(id) {
const response = await got({
method: 'get',
url: link,
headers: {
Referer: link,
method: 'post',
url: 'https://api.juejin.cn/content_api/v1/article/detail',
json: {
article_id: id,
},
});
const $ = cheerio.load(response.data);
// 解析日期
// const date = new Date($('time').attr('datetime'));
// const timeZone = 8 * 60;
// const serverOffset = date.getTimezoneOffset();
// const pubDate = new Date(date.getTime() + 60 * 1000 * (timeZone + serverOffset)).toUTCString();
// 提取内容
const description = $('.article-content')
.html()
.replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')
.replace(/(<img.*?)(data-src)(.*?>)/g, '$1src$3');
let description;
if (response.data.data) {
description = md.render(response.data.data.article_info.mark_content);
}
return { description };
}
@ -37,7 +29,7 @@ const ProcessFeed = (list, caches) =>
// 列表上提取到的信息
const single = {
title: item.article_info.title,
description: `${(item.content || item.summaryInfo || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')}`,
description: `${(item.article_info.brief_content || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')}`,
pubDate,
author: item.author_user_info.user_name,
link,
@ -45,9 +37,9 @@ const ProcessFeed = (list, caches) =>
// 使用tryGet方法从缓存获取内容。
// 当缓存中无法获取到链接内容的时候则使用load方法加载文章内容。
const other = await caches.tryGet(link, () => load(link));
const other = await caches.tryGet(link, () => load(item.article_id));
// 合并解析后的结果集作为该篇文章最终的输出结果
return Promise.resolve(Object.assign({}, single, other));
return { ...single, ...other };
})
);