mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-16 07:44:44 +08:00
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:
@@ -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,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'),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user