Add juejin posts 增加掘金专栏文章 (#1596)

* feat: add juejin post digest

* refactor: extract utils.js

* style: reformat

* feat: add juejin posts full text

* style: fix arrow-body-style

* style: fix prettier style
This commit is contained in:
Maecenas Li
2019-02-22 10:54:41 +08:00
committed by DIYgod
parent 606644123d
commit 26eacd7417
5 changed files with 86 additions and 36 deletions

View File

@@ -0,0 +1,54 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
// 加载文章页
async function load(link) {
const response = await axios({
method: 'get',
url: link,
headers: {
Referer: link,
},
});
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');
return { description, pubDate };
}
const ProcessFeed = async (list, caches) =>
await Promise.all(
list.map(async (item) => {
// 列表上提取到的信息
const single = {
title: item.title,
description: `${(item.content || item.summaryInfo || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')}`,
pubDate: new Date(item.createdAt).toUTCString(),
author: item.user.username,
link: item.originalUrl,
guid: item.originalUrl,
};
// 使用tryGet方法从缓存获取内容。
// 当缓存中无法获取到链接内容的时候则使用load方法加载文章内容。
const other = await caches.tryGet(item.originalUrl, async () => await load(item.originalUrl));
// 合并解析后的结果集作为该篇文章最终的输出结果
return Promise.resolve(Object.assign({}, single, other));
})
);
module.exports = {
ProcessFeed,
};