feat(route): support category and fix time (twreporter) (#14974)

* support category and fix time(twreporter)

* html -> text

* simplification

* update list of entries

---------
This commit is contained in:
Enoch Ma
2024-03-26 22:33:48 +08:00
committed by GitHub
parent 808c1f2d82
commit 288f1d1fd7
2 changed files with 70 additions and 7 deletions
+63
View File
@@ -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,
};
}
+7 -7
View File
@@ -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'),
};
}