Skip to content

Commit

Permalink
feat(route): add 上海市发展和改革委员会 (#15759)
Browse files Browse the repository at this point in the history
  • Loading branch information
nczitzk authored May 29, 2024
1 parent af0199f commit c731982
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 10 deletions.
155 changes: 155 additions & 0 deletions lib/routes/gov/sh/fgw/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);

import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import timezone from '@/utils/timezone';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';

export const handler = async (ctx) => {
const { category = 'fgw_zxxxgk' } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20;

const rootUrl = 'https://fgw.sh.gov.cn';
const currentUrl = new URL(`${category}/index.html`, rootUrl).href;

const { data: response } = await got(currentUrl);

const $ = load(response);

const language = $('html').prop('lang');

let items = $('ul.nowrapli li')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);

return {
title: item.find('a').prop('title'),
pubDate: parseDate(item.find('span.time').text()),
link: new URL(item.find('a').prop('href'), rootUrl).href,
language,
};
});

items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
if (!item.link.endsWith('.html')) {
item.enclosure_url = item.link;
item.enclosure_type = item.link ? `application/${item.link.split(/\./).pop()}` : undefined;
item.enclosure_title = item.title;

return item;
}

const { data: detailResponse } = await got(item.link);

const $$ = load(detailResponse);

const title = $$('meta[name="ArticleTitle"]').prop('content');
const image = $$('div.pdf-content img').first().prop('src');
const description = art(path.join(__dirname, 'templates/description.art'), {
images: image
? [
{
src: image,
alt: title,
},
]
: undefined,
description: $$('div#ivs_content').html(),
});

item.title = title;
item.description = description;
item.pubDate = timezone(parseDate($$('meta[name="PubDate"]').prop('content')), +8);
item.category = [...new Set([$$('meta[name="ColumnName"]').prop('content'), $$('meta[name="ColumnKeywords"]').prop('content')])].filter(Boolean);
item.author = $$('meta[name="ContentSource"]').prop('content');
item.content = {
html: description,
text: $$('div#ivs_content').text(),
};
item.image = image;
item.banner = image;
item.language = language;

const enclosureUrl = $$('div.pdf-content a, div.xgfj a').first().prop('href');

item.enclosure_url = enclosureUrl ? new URL(enclosureUrl, rootUrl).href : undefined;
item.enclosure_type = enclosureUrl ? `application/${enclosureUrl.split(/\./).pop()}` : undefined;
item.enclosure_title = title;

return item;
})
)
);

const author = $('meta[name="SiteName"]').prop('content');
const image = $('span.logo-icon img').prop('src');

return {
title: `${author} - ${$('meta[name="ColumnName"]').prop('content')}`,
description: $('meta[name="ColumnDescription"]').prop('content'),
link: currentUrl,
item: items,
allowEmpty: true,
image,
author,
language,
};
};

export const route: Route = {
path: '/sh/fgw/:category{.+}?',
name: '上海市发展和改革委员会',
url: 'fgw.sh.gov.cn',
maintainers: ['nczitzk'],
handler,
example: '/gov/sh/fgw/fgw_zxxxgk',
parameters: { category: '分类,默认为 `fgw_zxxxgk`,即最新信息公开,可在对应分类页 URL 中找到' },
description: `:::tip
若订阅 [最新信息公开](https://fgw.sh.gov.cn/fgw_zxxxgk/index.html),网址为 \`https://fgw.sh.gov.cn/fgw_zxxxgk/index.html\`。截取 \`https://fgw.sh.gov.cn/\` 到末尾 \`/index.html\` 的部分 \`fgw_zxxxgk\` 作为参数填入,此时路由为 [\`/gov/sh/fgw/fgw_zxxxgk\`](https://rsshub.app/gov/sh/fgw/fgw_zxxxgk)。
:::
| 最新信息公开 | 要闻动态 |
| ------------ | ---------- |
| fgw_zxxxgk | fgw_fzggdt |
`,
categories: ['government'],

features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['fgw.sh.gov.cn/:category'],
target: (params) => {
const category = params.category.replace(/\/index\.html/, '');

return `/gov/sh/fgw${category ? `/${category}` : ''}`;
},
},
{
title: '最新信息公开',
source: ['fgw.sh.gov.cn/fgw_zxxxgk/index.html'],
target: '/sh/fgw/fgw_zxxxgk',
},
{
title: '要闻动态',
source: ['fgw.sh.gov.cn/fgw_fzggdt/index.html'],
target: '/sh/fgw/fgw_fzggdt',
},
],
};
17 changes: 17 additions & 0 deletions lib/routes/gov/sh/fgw/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{ if images }}
{{ each images image }}
{{ if image?.src }}
<figure>
<img
{{ if image.alt }}
alt="{{ image.alt }}"
{{ /if }}
src="{{ image.src }}">
</figure>
{{ /if }}
{{ /each }}
{{ /if }}

{{ if description }}
{{@ description }}
{{ /if }}
8 changes: 8 additions & 0 deletions lib/routes/gov/sh/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '上海市人民政府',
url: 'sh.gov.cn',
categories: ['government'],
description: '',
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import path from 'node:path';
const rootUrl = 'http://www.rsj.sh.gov.cn';

export const route: Route = {
path: '/shanghai/rsj/ksxm',
path: '/sh/rsj/ksxm',
categories: ['government'],
example: '/gov/shanghai/rsj/ksxm',
example: '/gov/sh/rsj/ksxm',
parameters: {},
features: {
requireConfig: false,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { art } from '@/utils/render';
import path from 'node:path';

export const route: Route = {
path: '/shanghai/wgj/:page?',
path: '/sh/wgj/:page?',
categories: ['government'],
example: '/gov/shanghai/wgj',
example: '/gov/sh/wgj',
parameters: { page: '页数,默认第 1 页' },
features: {
requireConfig: false,
Expand All @@ -25,7 +25,7 @@ export const route: Route = {
radar: [
{
source: ['wsbs.wgj.sh.gov.cn/'],
target: '/shanghai/wgj',
target: '/sh/wgj',
},
],
name: '上海市文旅局审批公告',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/shanghai/wsjkw/yqtb',
path: '/sh/wsjkw/yqtb',
categories: ['government'],
example: '/gov/shanghai/wsjkw/yqtb',
example: '/gov/sh/wsjkw/yqtb',
parameters: {},
features: {
requireConfig: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import timezone from '@/utils/timezone';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/shanghai/yjj/*',
path: '/sh/yjj/*',
name: 'Unknown',
maintainers: [],
handler,
};

async function handler(ctx) {
const params = getSubPath(ctx) === '/shanghai/yjj' ? '/shanghai/yjj/zx-ylqx' : getSubPath(ctx);
const params = getSubPath(ctx) === '/sh/yjj' ? '/sh/yjj/zx-ylqx' : getSubPath(ctx);

const rootUrl = 'https://yjj.sh.gov.cn';
const currentUrl = `${rootUrl}${params.replace(/^\/shanghai\/yjj/, '')}/index.html`;
const currentUrl = `${rootUrl}${params.replace(/^\/sh\/yjj/, '')}/index.html`;

const response = await got({
method: 'get',
Expand Down

0 comments on commit c731982

Please sign in to comment.