From bb5a2c76400cacef0f7330eaa7a3ce5acbe3c79e Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Wed, 1 May 2024 00:37:10 +0800 Subject: [PATCH] feat(route): Create ttv (#15380) * feat(route): Create ttv. * . * Update index.ts * Update index.ts --- lib/routes/ttv/index.ts | 78 +++++++++++++++++++++++++++++++++++++ lib/routes/ttv/namespace.ts | 6 +++ 2 files changed, 84 insertions(+) create mode 100644 lib/routes/ttv/index.ts create mode 100644 lib/routes/ttv/namespace.ts diff --git a/lib/routes/ttv/index.ts b/lib/routes/ttv/index.ts new file mode 100644 index 000000000..5c867b587 --- /dev/null +++ b/lib/routes/ttv/index.ts @@ -0,0 +1,78 @@ +import { Route } from '@/types'; + +import got from '@/utils/got'; +import timezone from '@/utils/timezone'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; + +export const route: Route = { + path: '/:category?', + categories: ['traditional-media'], + example: '/ttv', + parameters: { category: '分类' }, + name: '分类', + maintainers: ['dzx-dzx'], + radar: [ + { + source: ['news.ttv.com.tw/:category'] + }, + ], + handler, +}; + +async function handler(ctx) { + const rootUrl = 'https://news.ttv.com.tw'; + const category = ctx.req.param('category') ?? 'realtime'; + const currentUrl = `${rootUrl}/${['realtime', 'focus'].includes(category) ? category : `category/${category}`}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = load(response.data); + + let items = $('div.news-list li') + .slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.query.limit) : 30) + .toArray() + .map((item) => { + item = $(item); + + return { + link: $(item).find('a').attr('href'), + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = load(detailResponse.data); + + item.title = content('title').text(); + item.pubDate = timezone(parseDate(content('meta[property="article:published_time"]').attr('content')), +8); + item.category = content('div.article-body ul.tag') + .find('a') + .toArray() + .map((t) => content(t).text()); + const section = content("meta[property='article:section']").attr('content'); + if (!item.category.includes(section)) { + item.category.push(section); + } + item.description = content('#newscontent').html(); + return item; + }) + ) + ); + + return { + title: $('title').text(), + link: currentUrl, + item: items, + }; +} diff --git a/lib/routes/ttv/namespace.ts b/lib/routes/ttv/namespace.ts new file mode 100644 index 000000000..95ba16c62 --- /dev/null +++ b/lib/routes/ttv/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '台視新聞網', + url: 'news.ttv.com.tw', +};