From 674fa0f7b0f616198528dc4e1f3c0d8806018d22 Mon Sep 17 00:00:00 2001 From: equt <17521736+equt@users.noreply.github.com> Date: Mon, 1 Jul 2024 21:47:15 +0800 Subject: [PATCH] feat(route): introduce `tsdm39/bd` (#16038) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): introduce `tsdm39/bd` * feat(route): deadbydaylight.com latest blog (#16037) * feat(route): deadbydaylight.com latest blog * addressing comments * Update lib/routes/deadbydaylight/index.ts * doc(route): fix incorrect MD syntax (#16040) * fix(route): 中国投资者网返回空结果 (#16044) * fix: review * fix: review * fix: review * fix: review * fix: throw if cookie not found * fix: remove unnecessary cache --------- --- lib/config.ts | 6 ++ lib/routes/tsdm39/bd.ts | 101 +++++++++++++++++++++++++++++++++ lib/routes/tsdm39/namespace.ts | 7 +++ 3 files changed, 114 insertions(+) create mode 100644 lib/routes/tsdm39/bd.ts create mode 100644 lib/routes/tsdm39/namespace.ts diff --git a/lib/config.ts b/lib/config.ts index 9c9d09fc3..80c726999 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -258,6 +258,9 @@ export type Config = { tophub: { cookie?: string; }; + tsdm39: { + cookie: string; + }; twitter: { username?: string[]; password?: string[]; @@ -622,6 +625,9 @@ const calculateValue = () => { tophub: { cookie: envs.TOPHUB_COOKIE, }, + tsdm39: { + cookie: envs.TSDM39_COOKIES, + }, twitter: { username: envs.TWITTER_USERNAME?.split(','), password: envs.TWITTER_PASSWORD?.split(','), diff --git a/lib/routes/tsdm39/bd.ts b/lib/routes/tsdm39/bd.ts new file mode 100644 index 000000000..2f65bda68 --- /dev/null +++ b/lib/routes/tsdm39/bd.ts @@ -0,0 +1,101 @@ +import type { DataItem, Route } from '@/types'; +import { config } from '@/config'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import ConfigNotFoundError from '@/errors/types/config-not-found'; + +// type id => display name +type Mapping = Record; + +const TYPE: Mapping = { + '403': '720P', + '404': '1080P', + '405': 'BDMV', + '4130': '4K', + '5815': 'AV1', +}; + +// render into MD table +const mkTable = (mapping: Mapping): string => { + const heading: string[] = [], + separator: string[] = [], + body: string[] = []; + + for (const key in mapping) { + heading.push(mapping[key]); + separator.push(':--:'); + body.push(key); + } + + return [heading.join(' | '), separator.join(' | '), body.join(' | ')].map((s) => `| ${s} |`).join('\n'); +}; + +const handler: Route['handler'] = async (ctx) => { + const { type } = ctx.req.param(); + + const cookie = config.tsdm39.cookie; + if (!cookie) { + throw new ConfigNotFoundError('缺少 TSDM39 用户登录后的 Cookie 值 TSDM 相关路由'); + } + + const html = await ofetch(`https://www.tsdm39.com/forum.php?mod=forumdisplay&fid=85${type ? `&filter=typeid&typeid=${type}` : ''}`, { + headers: { + Cookie: cookie, + }, + }); + + const $ = load(html); + + const item = $('tbody.tsdm_normalthread') + .toArray() + .map((item) => { + const $ = load(item); + + const title = $('a.xst').text(); + const price = $('span.xw1').last().text(); + const link = $('a.xst').attr('href'); + const date = $('td.by em').first().text().trim(); + + return { + title, + description: `价格:${price}`, + link, + pubDate: parseDate(date), + }; + }); + + return { + title: '天使动漫论坛 - BD', + link: 'https://www.tsdm39.com/forum.php?mod=forumdisplay&fid=85', + language: 'zh-Hans', + item, + }; +}; + +export const route: Route = { + path: '/bd/:type?', + name: 'BD', + categories: ['anime'], + maintainers: ['equt'], + example: '/tsdm39/bd', + parameters: { + type: 'BD type, checkout the table below for details', + }, + features: { + requireConfig: [ + { + name: 'TSDM39_COOKIES', + optional: false, + description: '天使动漫论坛登陆后的 cookie 值,可在浏览器控制台通过 `document.cookie` 获取。', + }, + ], + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + handler, + description: [TYPE].map((el) => mkTable(el)).join('\n\n'), +}; diff --git a/lib/routes/tsdm39/namespace.ts b/lib/routes/tsdm39/namespace.ts new file mode 100644 index 000000000..83084d4c8 --- /dev/null +++ b/lib/routes/tsdm39/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '天使动漫论坛', + url: 'www.tsdm39.com', + categories: ['anime'], +};