From 558207e40788ff034ae65fa80772f674fe167c2b Mon Sep 17 00:00:00 2001 From: J Date: Sun, 22 Sep 2024 10:30:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20route=20of=20=E6=84=8F?= =?UTF-8?q?=E6=9E=97=20(#16831)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add route of 意林 * fix: solve review * fix: solve review * fix: solve review * fix: solve review --- lib/routes/yilinzazhi/index.ts | 58 ++++++++++++++++ lib/routes/yilinzazhi/latest.ts | 103 +++++++++++++++++++++++++++++ lib/routes/yilinzazhi/namespace.ts | 8 +++ 3 files changed, 169 insertions(+) create mode 100644 lib/routes/yilinzazhi/index.ts create mode 100644 lib/routes/yilinzazhi/latest.ts create mode 100644 lib/routes/yilinzazhi/namespace.ts diff --git a/lib/routes/yilinzazhi/index.ts b/lib/routes/yilinzazhi/index.ts new file mode 100644 index 00000000000000..08b90b0011d20b --- /dev/null +++ b/lib/routes/yilinzazhi/index.ts @@ -0,0 +1,58 @@ +import { Data, DataItem, Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; + +export const route: Route = { + path: '/', + categories: ['reading'], + example: '/yilinzazhi', + radar: [ + { + source: ['www.yilinzazhi.com'], + target: '/', + }, + ], + name: '文章列表', + maintainers: ['g0ngjie'], + handler, + url: 'www.yilinzazhi.com', +}; + +async function handler(): Promise { + const baseUrl = 'https://www.yilinzazhi.com/'; + const response = await got(baseUrl); + const $ = load(response.data); + const contents: DataItem[] = $('section.content') + .find('li') + .map((_, target) => { + const li = $(target); + + const aTag = li.find('a'); + const title = aTag.text(); + const link = baseUrl + aTag.attr('href'); + + return { + title, + link, + description: '', + }; + }) + .toArray(); + + const items = (await Promise.all( + contents.map((content) => + cache.tryGet(content.link!, async () => { + const childRes = await got(content.link); + const $$ = load(childRes.data); + content.description = $$('.maglistbox').html()!; + return content; + }) + ) + )) as DataItem[]; + return { + title: '意林杂志网', + link: baseUrl, + item: items, + }; +} diff --git a/lib/routes/yilinzazhi/latest.ts b/lib/routes/yilinzazhi/latest.ts new file mode 100644 index 00000000000000..c01d3283d77778 --- /dev/null +++ b/lib/routes/yilinzazhi/latest.ts @@ -0,0 +1,103 @@ +import { Data, DataItem, Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import dayjs from 'dayjs'; + +export const route: Route = { + path: '/latest', + categories: ['reading'], + example: '/yilinzazhi/latest', + radar: [ + { + source: ['www.yilinzazhi.com'], + target: '/', + }, + ], + name: '近期文章汇总', + maintainers: ['g0ngjie'], + handler, + url: 'www.yilinzazhi.com', + description: '最近一期的文章汇总', +}; + +type Stage = { + link: string; + title: string; +}; + +type Catalog = { + title: string; + tables: Data[]; +}; + +async function handler(): Promise { + const baseUrl = 'https://www.yilinzazhi.com/'; + const response = await got(baseUrl); + const $ = load(response.data); + + const currentYear = dayjs().year(); + const yearSection = $('.year-section') + .toArray() + .find((el) => + $(el) + .find('.year-title') + .text() + .includes(currentYear + '') + ); + + const stage = $(yearSection!) + .find('a') + .map(function () { + const aTag = $(this); + const link = baseUrl + aTag.attr('href'); + const title = aTag.text(); + return { link, title }; + }) + .toArray()[0]; + + const catalogs = (await cache.tryGet(stage.link, async () => { + const stageRes = await got(stage.link); + const $$ = load(stageRes.data); + const catalogsEl = $$('.maglistbox dl').toArray(); + const children = catalogsEl.map((catalog) => { + const title = $$(catalog).find('dt span').text(); + const tables = $$(catalog) + .find('a') + .toArray() + .map((aTag) => { + const href = $$(aTag).attr('href')!; + const yearType = currentYear + href.substring(4, 5); + return { + title: $$(aTag).text(), + link: `${baseUrl}${currentYear}/yl${yearType}/${href}`, + }; + }); + return { title, tables }; + }); + return children; + })) as Catalog[]; + + const contents: Data[] = catalogs.flatMap((catalog) => catalog.tables); + + const items = (await Promise.all( + contents.map( + async (target) => + await cache.tryGet(target.link!, async () => { + const detailRes = await got(target.link); + const $$ = load(detailRes.data); + const detailContainer = $$('.blkContainerSblk.collectionContainer'); + + target.description = detailContainer.html()!; + + return target; + }) + ) + )) as DataItem[]; + + return { + title: '意林 - 近期文章汇总', + link: stage.link, + item: items, + }; +} diff --git a/lib/routes/yilinzazhi/namespace.ts b/lib/routes/yilinzazhi/namespace.ts new file mode 100644 index 00000000000000..0aa77f133eb5aa --- /dev/null +++ b/lib/routes/yilinzazhi/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '意林杂志', + url: 'www.yilinzazhi.com', + categories: ['reading'], + description: '', +};