diff --git a/routes/xueqiu/fund.js b/routes/xueqiu/fund.js index 41642f681..35d9367dd 100644 --- a/routes/xueqiu/fund.js +++ b/routes/xueqiu/fund.js @@ -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}
最新净值 ${data.nav}
今日`; - 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}
最新净值 ${data.nav}
今日`; + 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); + } };