From 288f1d1fd745a72f0bdcce5a596d74cea2738d75 Mon Sep 17 00:00:00 2001 From: Enoch Ma Date: Tue, 26 Mar 2024 15:33:48 +0100 Subject: [PATCH] feat(route): support category and fix time (twreporter) (#14974) * support category and fix time(twreporter) * html -> text * simplification * update list of entries --------- --- lib/routes/twreporter/category.ts | 63 ++++++++++++++++++++++++++ lib/routes/twreporter/fetch-article.ts | 14 +++--- 2 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 lib/routes/twreporter/category.ts diff --git a/lib/routes/twreporter/category.ts b/lib/routes/twreporter/category.ts new file mode 100644 index 0000000000..c09e8b92dc --- /dev/null +++ b/lib/routes/twreporter/category.ts @@ -0,0 +1,63 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; +import got from '@/utils/got'; + +import fetch from './fetch-article'; + +export const route: Route = { + path: '/category/:category', + categories: ['new-media'], + example: '/twreporter/category/world', + parameters: { category: 'Category' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['twreporter.org/:category'], + }, + ], + name: '分類', + maintainers: ['emdoe'], + handler, + url: 'twreporter.org/', +}; + +async function handler(ctx) { + const baseURL = 'https://www.twreporter.org'; + const url = baseURL + `/categories/${ctx.req.param('category')}`; + const res = await got(url); + const $ = load(res.data); + + const regexp = /^window\.__REDUX_STATE__=(.*);$/gm; + const raw = $('script[charset="UTF-8"]').text().replaceAll(regexp, '$1'); + const posts = JSON.parse(raw).entities.posts; + + const list = posts.allIds.map((id) => ({ + link: baseURL + '/a/' + posts.byId[id].slug, + title: posts.byId[id].title, + })); + const category = posts.byId[posts.allIds[0]].category_set[0].category.name; + + const out = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const single = await fetch(item.link); + single.title = item.title; + return single; + }) + ) + ); + + return { + title: `報導者 | ${category}`, + link: url, + item: out, + }; +} diff --git a/lib/routes/twreporter/fetch-article.ts b/lib/routes/twreporter/fetch-article.ts index ea635bbd23..a9bca8d4c0 100644 --- a/lib/routes/twreporter/fetch-article.ts +++ b/lib/routes/twreporter/fetch-article.ts @@ -7,6 +7,12 @@ export default async function fetch(address) { const capture = load(res.data); capture('.gIMvvS').remove(); + let time = capture('.gzaDTq').text(); + // For `photography` + if (!time) { + time = capture('.fEZfRw').text(); + } + let metaInfoBox = capture('.ffAPnj') .filter((index) => index === 0) .get(); @@ -19,12 +25,6 @@ export default async function fetch(address) { } const acquire = load(metaInfoBox); - let time = acquire('.gimsRe').text(); - - // For `photography` - if (!time) { - time = acquire('.kHluJP').text(); - } // # Author(s) of the article // // There exists two formats for this section. @@ -62,6 +62,6 @@ export default async function fetch(address) { description: contents, link: address, guid: address, - pubDate: parseDate(time, 'M/D/YYYY'), + pubDate: parseDate(time, 'YYYY/M/D'), }; }