Skip to content

Commit

Permalink
fix(route): fix the gov.cn stats using the ofetch.raw (#15317)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfei authored Apr 21, 2024
1 parent 5991ecb commit 4f678bd
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions lib/routes/gov/stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -29,35 +29,37 @@ 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)
:::`,
};

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)
Expand All @@ -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

Expand Down

0 comments on commit 4f678bd

Please sign in to comment.