fix date and cache

This commit is contained in:
Fan Shen
2019-05-12 16:09:51 +08:00
parent f82fb61bef
commit be94b58483
+9 -5
View File
@@ -1,5 +1,6 @@
const cheerio = require('cheerio');
const axios = require('../../utils/axios');
const date = require('../../utils/date');
const url = require('url');
const host = 'https://user.guancha.cn';
@@ -21,9 +22,15 @@ module.exports = async (ctx) => {
list.map(async (itemUrl) => {
// 将相对链接替换为绝对链接
const absoluteUrl = url.resolve(host, itemUrl);
const cache = await ctx.cache.get(absoluteUrl);
// 判断缓存
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await axios.get(absoluteUrl);
const $ = cheerio.load(res.data);
const dateStr = $('.user-main .time1').text();
const item = {
title: $('.article-content h1').text(),
author: $('.user-main h4')
@@ -32,12 +39,9 @@ module.exports = async (ctx) => {
.text(),
link: absoluteUrl,
description: $('.article-txt-content').html(),
pubDate: $('.user-main .time1').text(),
// 格式化日期
pubDate: date(dateStr),
};
const cache = await ctx.cache.get(absoluteUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
ctx.cache.set(absoluteUrl, JSON.stringify(item), 24 * 60 * 60);
return Promise.resolve(item);