From 0888a2c2592f50223ba013ecd7a0a79a0b41fcb6 Mon Sep 17 00:00:00 2001 From: ueiu <39592269+ueiu@users.noreply.github.com> Date: Sat, 24 Aug 2024 22:08:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(route/eastmoney):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=9C=E6=96=B9=E8=B4=A2=E5=AF=8C=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E4=B8=BA=E4=B8=AA=E8=82=A1=E7=A0=94=E6=8A=A5=E7=9A=84?= =?UTF-8?q?=E7=A0=94=E7=A9=B6=E6=8A=A5=E5=91=8A=20(#16526)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route/eastmoney): 增加支持东方财富类型为个股研报的研究报告 * feat(route/eastmoney): 个股研报标题增加公司名 * feat(route/eastmoney): 东方财富个股研报获取收益/市盈率 方法合并 --- lib/routes/eastmoney/report/index.ts | 78 +++++++++++++++---- .../eastmoney/templates/stock_description.art | 36 +++++++++ lib/routes/eastmoney/utils.ts | 35 +++++++++ 3 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 lib/routes/eastmoney/templates/stock_description.art create mode 100644 lib/routes/eastmoney/utils.ts diff --git a/lib/routes/eastmoney/report/index.ts b/lib/routes/eastmoney/report/index.ts index 7160b8e4a..2c33e0f7f 100644 --- a/lib/routes/eastmoney/report/index.ts +++ b/lib/routes/eastmoney/report/index.ts @@ -1,8 +1,14 @@ import { Route } from '@/types'; +import { getCurrentPath } from '@/utils/helpers'; import cache from '@/utils/cache'; import got from '@/utils/got'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; +import { art } from '@/utils/render'; +import path from 'node:path'; +import { getRatingChangeStr, getEpsOrPeStr } from '../utils'; + +const __dirname = getCurrentPath(import.meta.url); export const route: Route = { path: '/report/:category', @@ -25,9 +31,9 @@ export const route: Route = { name: '研究报告', maintainers: ['syzq'], handler, - description: `| 策略报告 | 宏观研究 | 券商晨报 | 行业研究 | - | -------------- | ----------- | ------------ | -------- | - | strategyreport | macresearch | brokerreport | industry |`, + description: `| 策略报告 | 宏观研究 | 券商晨报 | 行业研究 | 个股研报 | + | -------------- | ----------- | ------------ | -------- | -------- | + | strategyreport | macresearch | brokerreport | industry | stock |`, }; async function handler(ctx) { @@ -39,12 +45,14 @@ async function handler(ctx) { industry: '行业研报', macresearch: '宏观研究', strategyreport: '策略报告', + stock: '个股研报', }; const linkType = { brokerreport: 'zw_brokerreport', industry: 'zw_industry', macresearch: 'zw_macresearch', strategyreport: 'zw_strategy', + stock: 'info', }; const res = await got(`${baseUrl}/report/${category}`); @@ -56,27 +64,67 @@ async function handler(ctx) { .match(/var initdata(.=?)(.*?);/)[2] ); - const list = initData.data.map((item) => ({ - title: `[${item.orgSName}]${item.title}`, - link: `${baseUrl}/report/${linkType[category]}.jshtml?encodeUrl=${item.encodeUrl}`, - pubDate: parseDate(item.publishDate), - author: item.researcher, - })); + const list = initData.data.map((item) => { + const stockName = category === 'stock' ? `[${item.stockName}]` : ''; + return { + title: `[${item.orgSName}]${stockName}${item.title}`, + link: `${baseUrl}/report/${linkType[category]}` + (category === 'stock' ? `/${item.infoCode}.html` : `.jshtml?encodeUrl=${item.encodeUrl}`), + pubDate: parseDate(item.publishDate), + author: item.researcher, + originItem: item, // temp use + }; + }); const items = await Promise.all( - list.map((item) => - cache.tryGet(item.link, async () => { + list.map((item) => { + const tempOriginItem = item.originItem; + delete item.originItem; // temp use + + return cache.tryGet(item.link, async () => { try { const { data: response } = await got(item.link); const $ = load(response); - item.link = $('.pdf-link').attr('href'); - item.description = $('.ctx-content').text(); + + if (category === 'stock') { + const { title, stockName, stockCode, emRatingName, ratingChange, orgSName, indvInduName } = tempOriginItem; + const ratingChangeStr = getRatingChangeStr(ratingChange); + const currentYear = new Date().getFullYear(); + const nextYear = currentYear + 1; + const description = $('.newsContent').html(); + const enclosureUrl = $('#ContentBody .rightlab').attr('href'); + const predictThisYearEps = getEpsOrPeStr(tempOriginItem.predictThisYearEps, 3); + const predictThisYearPe = getEpsOrPeStr(tempOriginItem.predictThisYearPe, 2); + const predictNextYearEps = getEpsOrPeStr(tempOriginItem.predictNextYearEps, 3); + const predictNextYearPe = getEpsOrPeStr(tempOriginItem.predictNextYearPe, 2); + + item.enclosure_url = enclosureUrl; + item.description = art(path.join(__dirname, '../templates/stock_description.art'), { + title, + stockName, + stockCode, + emRatingName, + ratingChangeStr, + description, + orgSName, + predictThisYearEps, + predictThisYearPe, + predictNextYearEps, + predictNextYearPe, + indvInduName, + currentYear, + nextYear, + enclosureUrl, + }); + } else { + item.link = $('.pdf-link').attr('href'); + item.description = $('.ctx-content').text(); + } return item; } catch { return item; } - }) - ) + }); + }) ); return { diff --git a/lib/routes/eastmoney/templates/stock_description.art b/lib/routes/eastmoney/templates/stock_description.art new file mode 100644 index 000000000..45ff4a8b8 --- /dev/null +++ b/lib/routes/eastmoney/templates/stock_description.art @@ -0,0 +1,36 @@ +
| 股票代码 | +股票简称 | +报告名称 | +东财评级 | +评级变动 | +{{ currentYear }}盈利预测 | +{{ nextYear }}盈利预测 | +行业 | +||
|---|---|---|---|---|---|---|---|---|---|
| 收益 | +市盈率 | +收益 | +市盈率 | +||||||
| {{ stockCode }} | +{{ stockName }} | +{{ title }} | +{{ emRatingName }} | +{{ ratingChangeStr }} | +{{ predictThisYearEps }} | +{{ predictThisYearPe }} | +{{ predictNextYearEps }} | +{{ predictNextYearPe }} | +{{ indvInduName }} | +