修复雪球基金遗漏的 guid, 添加缓存 (#845)

This commit is contained in:
Henry Wang 2018-10-09 03:28:34 +01:00 committed by DIYgod
parent 018520480f
commit 9877fdecb1
1 changed files with 62 additions and 53 deletions

View File

@ -2,60 +2,69 @@ const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const url = `https://xueqiu.com/S/F${ctx.params.id}`;
const guid = `xueqiu/fund ${ctx.params.id}`;
const cache = await ctx.cache.get(guid);
let fundName = await axios({
method: 'get',
url,
headers: {
Referer: 'https://xueqiu.com/',
},
});
if (cache) {
return JSON.parse(cache);
} else {
const url = `https://xueqiu.com/S/F${ctx.params.id}`;
const $ = cheerio.load(fundName.data);
fundName = $('.stock-name').text();
const fundNameShort = fundName.slice(0, -10);
const response = await axios({
method: 'get',
url: `https://fund.xueqiu.com/dj/open/fund/growth/${ctx.params.id}?day=7`,
headers: {
Referer: url,
},
});
const data = response.data.data.fund_nav_growth.pop();
let description = `${fundNameShort} <br> 最新净值 ${data.nav} <br> 今日`;
let title = `${fundNameShort} ${data.date.substring(5)} `;
const value = parseFloat(data.value);
if (value > 0) {
description += '涨幅';
title += `📈 ${data.percentage}%`;
} else if (value < 0) {
description += '跌幅';
title += `📉 ${data.percentage}%`;
} else if (value === 0) {
description += '无波动';
title += '持平';
}
description += ` ${data.percentage}%
¥${data.value}`;
ctx.state.data = {
title: fundName,
link: url,
description: `${fundName} 净值更新`,
item: [
{
title,
description,
pubDate: new Date(data.date).toUTCString(),
link: url,
let fundName = await axios({
method: 'get',
url,
headers: {
Referer: 'https://xueqiu.com/',
},
],
};
});
const $ = cheerio.load(fundName.data);
fundName = $('.stock-name').text();
const fundNameShort = fundName.slice(0, -10);
const response = await axios({
method: 'get',
url: `https://fund.xueqiu.com/dj/open/fund/growth/${ctx.params.id}?day=7`,
headers: {
Referer: url,
},
});
const data = response.data.data.fund_nav_growth.pop();
let description = `${fundNameShort} <br> 最新净值 ${data.nav} <br> 今日`;
let title = `${fundNameShort} ${data.date.substring(5)} `;
const value = parseFloat(data.value);
if (value > 0) {
description += '涨幅';
title += `📈 ${data.percentage}%`;
} else if (value < 0) {
description += '跌幅';
title += `📉 ${data.percentage}%`;
} else if (value === 0) {
description += '无波动';
title += '持平';
}
description += ` ${data.percentage}%(¥${data.value}`;
const single = {
title,
description,
pubDate: new Date().toUTCString(),
guid: `${guid} ${data.date}`,
link: url,
};
ctx.state.data = {
title: fundName,
link: url,
description: `${fundName} 净值更新`,
item: [single],
};
ctx.cache.set(guid, JSON.stringify(ctx.state.data), 2 * 60 * 60);
}
};