add 谷雨 (#1844)

closes #1841
This commit is contained in:
Chenyang Shi
2019-03-29 16:05:00 +08:00
committed by DIYgod
parent 9088a339be
commit fb92f06190
3 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
const axios = require('../../../utils/axios');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const map = new Map([
['lab', { title: '谷雨实验室', link: 'https://news.qq.com/guyu/huizong/hz_lab.htm', channelid: 498 }],
['report', { title: '谷雨报道', link: 'https://news.qq.com/guyu/huizong/hz_report.htm', channelid: 500 }],
['story', { title: '谷雨故事', link: 'https://gy.qq.com/hz_story.html', channelid: 332 }],
['shalong', { title: '谷雨沙龙', link: 'https://news.qq.com/guyu/huizong/hz_shalong.htm', channelid: 542 }],
]);
module.exports = async (ctx) => {
const type = ctx.params.name;
const title = map.get(type).title;
const channelid = map.get(type).channelid;
const link = map.get(type).link;
const api = `https://i.match.qq.com/ninjayc/guyu?action=guyu&channelid=${channelid}&p=0&num=10`;
const response = await axios.get(api);
const list = response.data.data;
const out = await Promise.all(
list.map(async (info) => {
const title = info.n_short_title;
const date = info.n_publishtime;
const itemUrl = info.n_url;
const author = info.name;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await axios.get(itemUrl, {
responseType: 'arraybuffer',
});
response.data = iconv.decode(response.data, 'gbk');
const $ = cheerio.load(response.data);
const description = $('div.articleContent')
.html()
.replace(/src="/g, `src="https:`);
const single = {
title: title,
link: itemUrl,
description: description,
author: author,
pubDate: new Date(date).toUTCString(),
};
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: title,
link: link,
item: out,
};
};