From 4f678bd7974919fe99c0402ef6331af1986baa5b Mon Sep 17 00:00:00 2001 From: Felix Hsu Date: Sun, 21 Apr 2024 22:32:42 +0800 Subject: [PATCH] fix(route): fix the gov.cn stats using the ofetch.raw (#15317) --- lib/routes/gov/stats/index.ts | 47 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/routes/gov/stats/index.ts b/lib/routes/gov/stats/index.ts index 8533961ca7c1dd..a3c36cb41ea8f5 100644 --- a/lib/routes/gov/stats/index.ts +++ b/lib/routes/gov/stats/index.ts @@ -4,7 +4,7 @@ const __dirname = getCurrentPath(import.meta.url); import { getSubPath } from '@/utils/common-utils'; import cache from '@/utils/cache'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import timezone from '@/utils/timezone'; import { parseDate } from '@/utils/parse-date'; @@ -29,9 +29,13 @@ export const route: Route = { description: `::: tip 路径处填写对应页面 URL 中 \`http://www.stats.gov.cn/\` 后的字段。下面是一个例子。 - 若订阅 [数据 > 数据解读](http://www.stats.gov.cn/sj/sjjd/) 则将对应页面 URL \`http://www.stats.gov.cn/sj/sjjd/\` 中 \`http://www.stats.gov.cn/\` 后的字段 \`sj/sjjd\` 作为路径填入。此时路由为 [\`/gov/stats/sj/sjjd\`](https://rsshub.app/gov/stats/sj/sjjd) + 若订阅 [数据 > 数据解读](http://www.stats.gov.cn/sj/sjjd/) + 则将对应页面 URL \`http://www.stats.gov.cn/sj/sjjd/\` 中 \`http://www.stats.gov.cn/\` 后的字段 \`sj/sjjd\` 作为路径填入。 + 此时路由为 [\`/gov/stats/sj/sjjd\`](https://rsshub.app/gov/stats/sj/sjjd) - 若订阅 [新闻 > 时政要闻 > 中央精神](http://www.stats.gov.cn/xw/szyw/zyjs/) 则将对应页面 URL \`http://www.stats.gov.cn/xw/szyw/zyjs/\` 中 \`http://www.stats.gov.cn/\` 后的字段 \`xw/szyw/zyjs\` 作为路径填入。此时路由为 [\`/gov/stats/xw/szyw/zyjs\`](https://rsshub.app/gov/stats/xw/szyw/zyjs) + 若订阅 [新闻 > 时政要闻 > 中央精神](http://www.stats.gov.cn/xw/szyw/zyjs/) + 则将对应页面 URL \`http://www.stats.gov.cn/xw/szyw/zyjs/\` 中 \`http://www.stats.gov.cn/\` + 后的字段 \`xw/szyw/zyjs\` 作为路径填入。此时路由为 [\`/gov/stats/xw/szyw/zyjs\`](https://rsshub.app/gov/stats/xw/szyw/zyjs) :::`, }; @@ -39,25 +43,23 @@ async function handler(ctx) { const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 15; const rootUrl = 'http://www.stats.gov.cn'; + const { headers } = await ofetch.raw(rootUrl); + const sid = headers + ?.getSetCookie() + .find((s) => s.startsWith('wzws_sessionid=')) + ?.split(';')[0] as string; + const pathname = getSubPath(ctx) === '/stats' ? '/sj/zxfb/' : getSubPath(ctx).replace(/^\/stats(.*)/, '$1'); const currentUrl = `${rootUrl}${pathname.endsWith('/') ? pathname : pathname + '/'}`; - let response = await got({ - method: 'get', - url: rootUrl, - }); - - const headers = { - cookie: response.headers['set-cookie'].join(' ').match(/(wzws_sessionid=.*?);/)[1], - }; - - response = await got({ - method: 'get', - url: currentUrl, - headers, + const response = await ofetch(currentUrl, { + headers: { + Cookie: sid, + Referer: currentUrl, + }, }); - const $ = load(response.data); + const $ = load(response); let items = $($('a.pchide').length === 0 ? 'a[title]' : '.list-content a.pchide') .slice(0, limit) @@ -74,13 +76,14 @@ async function handler(ctx) { items = await Promise.all( items.map((item) => cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - headers, + const detailResponse = await ofetch(item.link, { + headers: { + Cookie: sid, + Referer: rootUrl, + }, }); - const content = load(detailResponse.data); + const content = load(detailResponse); // articles from www.news.cn or www.gov.cn