From 9d926d32ed8788fcfee8bb090a92caa28e70ab4b Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sun, 9 Apr 2023 02:42:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E7=95=99=E5=9B=AD?= =?UTF-8?q?=E7=BD=91=E6=96=B0=E9=97=BB=20(#12265)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 留园网新闻 * fix: add filter for links * fix: maintainer and docs tag --------- --- docs/new-media.md | 27 +++++++++++ lib/router.js | 2 +- lib/{routes => v2}/6park/index.js | 32 ++++++++----- lib/v2/6park/maintainer.js | 8 ++++ lib/v2/6park/news.js | 76 +++++++++++++++++++++++++++++++ lib/v2/6park/radar.js | 65 ++++++++++++++++++++++++++ lib/v2/6park/router.js | 4 ++ 7 files changed, 201 insertions(+), 13 deletions(-) rename lib/{routes => v2}/6park/index.js (67%) create mode 100644 lib/v2/6park/maintainer.js create mode 100644 lib/v2/6park/news.js create mode 100644 lib/v2/6park/radar.js create mode 100644 lib/v2/6park/router.js diff --git a/docs/new-media.md b/docs/new-media.md index 7380c2351..28c87b430 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2997,6 +2997,33 @@ column 为 third 时可选的 category: +### 新闻栏目 + + + +分站 + +| newspark | local | +| -------- | ----- | + +::: tip 提示 + +若订阅 [时政](https://www.6parknews.com/newspark/index.php?type=1),其网址为 ,其中 `newspark` 为分站,`1` 为栏目 id。 + +若订阅 [美国](https://local.6parknews.com/index.php?type_id=1),其网址为 ,其中 `local` 为分站,`1` 为栏目 id。 + +::: + + + +### 头条精选 + + + +### 新闻搜索 + + + ## 隆众资讯 ### 资讯 diff --git a/lib/router.js b/lib/router.js index e5388b0a8..95c6291ec 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3636,7 +3636,7 @@ router.get('/voa/day-photos', lazyloadRouteHandler('./routes/voa/day-photos')); router.get('/voa/:language/:channel?', lazyloadRouteHandler('./routes/voa/index')); // 留园网 -router.get('/6park/:id?/:type?/:keyword?', lazyloadRouteHandler('./routes/6park/index')); +// router.get('/6park/:id?/:type?/:keyword?', lazyloadRouteHandler('./routes/6park/index')); // 哔嘀影视 // router.get('/mp4er/:type?/:caty?/:area?/:year?/:order?', lazyloadRouteHandler('./routes/mp4er/index')); diff --git a/lib/routes/6park/index.js b/lib/v2/6park/index.js similarity index 67% rename from lib/routes/6park/index.js rename to lib/v2/6park/index.js index a6e3375e9..3fe1d1cbe 100644 --- a/lib/routes/6park/index.js +++ b/lib/v2/6park/index.js @@ -1,10 +1,14 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { - const id = ctx.params.id || 'chan1'; - const type = ctx.params.type || ''; - const keyword = ctx.params.keyword || ''; + const id = ctx.params.id ?? 'chan1'; + const type = ctx.params.type ?? ''; + const keyword = ctx.params.keyword ?? ''; + + const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 50; const rootUrl = 'https://club.6parkbbs.com'; const indexUrl = `${rootUrl}/${id}/index.php`; @@ -17,31 +21,35 @@ module.exports = async (ctx) => { const $ = cheerio.load(response.data); - const list = $('#d_list ul li, #thread_list li, .t_l .t_subject') - .slice(0, 10) - .map((_, item) => { + let items = $('#d_list ul li, #thread_list li, .t_l .t_subject') + .toArray() + .slice(0, limit) + .map((item) => { item = $(item); - const a = item.find('a').eq(0); + + const a = item.find('a').first(); + return { link: `${rootUrl}/${id}/${a.attr('href')}`, }; - }) - .get(); + }); - const items = await Promise.all( - list.map((item) => + items = await Promise.all( + items.map((item) => ctx.cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', url: item.link, }); + const content = cheerio.load(detailResponse.data); item.title = content('title').text().replace(' -6park.com', ''); item.author = detailResponse.data.match(/送交者: .*>(.*)<.*\[/)[1]; - item.pubDate = new Date(detailResponse.data.match(/于 (.*) 已读/)[1]).toUTCString(); + item.pubDate = timezone(parseDate(detailResponse.data.match(/于 (.*) 已读/)[1], 'YYYY-MM-DD h:m'), +8); item.description = content('pre') .html() + .replace(/

<\/p>/g, '') .replace(/6park.com<\/font>/g, ''); return item; diff --git a/lib/v2/6park/maintainer.js b/lib/v2/6park/maintainer.js new file mode 100644 index 000000000..dbee4fcfb --- /dev/null +++ b/lib/v2/6park/maintainer.js @@ -0,0 +1,8 @@ +module.exports = { + '/:id?': ['nczitzk'], + '/:id/gold': ['nczitzk'], + '/:id/keywords/:keyword?': ['nczitzk'], + '/news/:site?/:id?': ['nczitzk'], + '/news/newspark/gold': ['nczitzk'], + '/news/newspark/keywords/:keyword?': ['nczitzk'], +}; diff --git a/lib/v2/6park/news.js b/lib/v2/6park/news.js new file mode 100644 index 000000000..9261816e5 --- /dev/null +++ b/lib/v2/6park/news.js @@ -0,0 +1,76 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const site = ctx.params.site ?? 'newspark'; + const id = ctx.params.id ?? ''; + const keyword = ctx.params.keyword ?? ''; + + const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 50; + + const isLocal = site === 'local'; + + const rootUrl = `https://${isLocal ? site : 'www'}.6parknews.com`; + const indexUrl = `${rootUrl}${isLocal ? '' : '/newspark'}/index.php`; + const currentUrl = `${indexUrl}${keyword ? `?act=newssearch&app=news&keywords=${keyword}&submit=查询` : id ? (isNaN(id) ? `?act=${id}` : isLocal ? `?type_id=${id}` : `?type=${id}`) : ''}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('#d_list ul li, #thread_list li, .t_l .t_subject') + .toArray() + .slice(0, limit) + .map((item) => { + item = $(item); + + const a = item.find('a').first(); + const link = a.attr('href'); + + return { + title: a.text(), + link: /^http/.test(link) ? link : `${rootUrl}/${/^view/.test(link) ? `newspark/${link}` : link}`, + }; + }); + + items = await Promise.all( + items + .filter((item) => /6parknews\.com/.test(item.link)) + .map((item) => + ctx.cache.tryGet(item.link, async () => { + try { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + const matches = detailResponse.data.match(/新闻来源:(.*?)于.*(\d{4}-\d{2}-\d{2} \d{1,2}:\d{1,2}:\d{1,2})/); + + item.title = content('h2').text(); + item.author = matches[1].trim(); + item.pubDate = timezone(parseDate(matches[2], 'YYYY-MM-DD h:m'), +8); + item.description = content('#shownewsc') + .html() + .replace(/

<\/p>/g, ''); + } catch (e) { + // no-empty + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/6park/radar.js b/lib/v2/6park/radar.js new file mode 100644 index 000000000..30febe7e3 --- /dev/null +++ b/lib/v2/6park/radar.js @@ -0,0 +1,65 @@ +module.exports = { + '6parkbbs.com': { + _name: '留园网', + club: [ + { + title: '分站', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/:id/index.php', '/'], + target: '/6park/:id?', + }, + { + title: '精华区', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/:id/index.php', '/'], + target: '/6park/:id/gold', + }, + { + title: '搜索关键字', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/:id/index.php', '/'], + target: (params, url) => `/6park/:id/keywords/${new URL(url).searchParams.get('keywords')}`, + }, + ], + local: [ + { + title: '新闻栏目', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/index.php', '/'], + target: (params, url) => `/6park/news/local/${new URL(url).searchParams.get('type_id')}`, + }, + { + title: '头条精选', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/index.php', '/'], + target: '/6park/news/newspark/gold', + }, + { + title: '新闻搜索', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/index.php', '/'], + target: (params, url) => `/6park/news/newspark/keywords/${new URL(url).searchParams.get('keywords')}`, + }, + ], + newspark: [ + { + title: '新闻栏目', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/newspark/index.php', '/'], + target: (params, url) => `/6park/news/newspark/${new URL(url).searchParams.get('type')}`, + }, + { + title: '头条精选', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/newspark/index.php', '/'], + target: '/6park/news/newspark/gold', + }, + { + title: '新闻搜索', + docs: 'https://docs.rsshub.app/new-media.html#liu-yuan-wang', + source: ['/newspark/index.php', '/'], + target: (params, url) => `/6park/news/newspark/keywords/${new URL(url).searchParams.get('keywords')}`, + }, + ], + }, +}; diff --git a/lib/v2/6park/router.js b/lib/v2/6park/router.js new file mode 100644 index 000000000..050975422 --- /dev/null +++ b/lib/v2/6park/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get('/news/:site?/:id?/:keyword?', require('./news')); + router.get('/:id?/:type?/:keyword?', require('./')); +};