From bb74dad38733e6133c26312431649db4872d0d5c Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Mon, 14 Oct 2024 02:31:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=9B=BD=E9=97=A8?= =?UTF-8?q?=E4=BC=A0=E5=AA=92=E5=9C=A8=E7=BA=BF=E4=B8=AD=E5=9B=BD=E6=B5=B7?= =?UTF-8?q?=E5=85=B3=20(#17093)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 国门传媒在线中国海关 * fix: categories --------- --- lib/routes/gmcmonline/chinacustoms.ts | 130 ++++++++++++++++++++++++++ lib/routes/gmcmonline/namespace.ts | 8 ++ 2 files changed, 138 insertions(+) create mode 100644 lib/routes/gmcmonline/chinacustoms.ts create mode 100644 lib/routes/gmcmonline/namespace.ts diff --git a/lib/routes/gmcmonline/chinacustoms.ts b/lib/routes/gmcmonline/chinacustoms.ts new file mode 100644 index 00000000000000..9386d1579edc8f --- /dev/null +++ b/lib/routes/gmcmonline/chinacustoms.ts @@ -0,0 +1,130 @@ +import { Route } from '@/types'; + +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; + +export const handler = async (ctx) => { + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 10; + + const rootUrl = 'http://chinacustoms.gmcmonline.com'; + const magRootUrl = 'http://manager.gmcmonline.com'; + + const { data: response } = await got(rootUrl); + + const $ = load(response); + + const author = $('p.copyright a').text(); + const language = $('html').prop('lang'); + + let items = $('ul.booklist li a') + .toArray() + .slice(0, limit) + .map((item) => { + item = $(item); + + const title = item.find('p.txt').text(); + const image = new URL(item.find('img').prop('src'), rootUrl).href; + + return { + title, + link: new URL(item.prop('href'), rootUrl).href, + pubDate: parseDate(title, 'YYYY年MM期'), + author, + image, + banner: image, + language, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const { data: detailResponse } = await got(item.link); + + const $$ = load(detailResponse); + + $$('ol.breadcrumb li a').first().remove(); + + const current = $$('ol.breadcrumb li a').first().text(); + const pubDate = parseDate($$('div.title').text(), 'YYYY年MM月DD日'); + const image = new URL($$('div.coverline img').prop('src'), rootUrl).href; + + return $$('a.txt') + .toArray() + .map((i) => { + i = $(i); + + const id = i.prop('href').match(/c\/(\d+)\.shtml/)?.[1] ?? undefined; + + if (!id) { + return; + } + + const title = i.prop('title') || i.text(); + const guid = `gmcmonline-chinacustoms-${id}`; + + return { + title, + pubDate, + link: new URL(i.prop('href'), item.link).href, + category: [current, i.closest('div.class-box').find('div.title-box span').text().replaceAll(/【|】/g, '') || undefined].filter(Boolean), + author, + guid, + id: guid, + image, + banner: image, + language, + enclosure_url: new URL(`front/article/${id}/pdf?magazineID=2`, magRootUrl).href, + enclosure_type: 'application/pdf', + enclosure_title: title, + }; + }) + .filter(Boolean); + }) + ) + ); + + const title = $('title').text(); + const image = new URL($('div.img-box img').prop('src'), rootUrl).href; + + return { + title, + description: title, + link: rootUrl, + item: items.flat(), + allowEmpty: true, + image, + author: title, + language, + }; +}; + +export const route: Route = { + path: '/chinacustoms', + name: '中国海关', + url: 'chinacustoms.gmcmonline.com', + maintainers: ['nczitzk'], + handler, + example: '/gmcmonline/chinacustoms', + parameters: undefined, + description: undefined, + categories: ['reading'], + + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['chinacustoms.gmcmonline.com'], + target: '/chinacustoms', + }, + ], +}; diff --git a/lib/routes/gmcmonline/namespace.ts b/lib/routes/gmcmonline/namespace.ts new file mode 100644 index 00000000000000..973f04bf92bb4c --- /dev/null +++ b/lib/routes/gmcmonline/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '国门传媒在线', + url: 'gmcmonline.com', + categories: ['journal'], + description: '', +};