Skip to content

Commit

Permalink
feat(route): tfc-taiwan (DIYgod#13231)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Sep 7, 2023
1 parent 58a052e commit 30cfd44
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/v2/tfc-taiwan/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { baseUrl, parseList, parseItems } = require('./utils');

module.exports = async (ctx) => {
const requestPath = ctx.request.path;
const isTopic = requestPath.startsWith('/topic/');
let link = baseUrl;

if (isTopic) {
link += `/topic/${ctx.params.id}`;
} else if (requestPath === '/') {
link += `/articles/report`;
} else {
link += `/articles${requestPath}`;
}

const { data: response } = await got(link);
const $ = cheerio.load(response);

const list = $(`${isTopic ? '.view-grouping' : '.pane-clone-of-article'} .views-row-inner`)
.toArray()
.map((item) => parseList($(item)));

const items = await parseItems(list, ctx.cache.tryGet);

ctx.state.data = {
title: $('head title').text(),
description: $('head meta[name="description"]').attr('content'),
image: $('head meta[property="og:image"]').attr('content'),
logo: $('head link[rel="shortcut icon"]').attr('href'),
icon: $('head link[rel="shortcut icon"]').attr('href'),
link,
item: items,
};
};
10 changes: 10 additions & 0 deletions lib/v2/tfc-taiwan/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
'': ['TonyRL'],
'/': ['TonyRL'],
'/:type?': ['TonyRL'], // /info and /report
'/:type/:id+': ['TonyRL'], // /category/:id+ and /topic/:id
'/category/:id+': ['TonyRL'],
'/info': ['TonyRL'],
'/report': ['TonyRL'],
'/topic/:id': ['TonyRL'],
};
31 changes: 31 additions & 0 deletions lib/v2/tfc-taiwan/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
'tfc-taiwan.org.tw': {
_name: '台灣事實查核中心',
'.': [
{
title: '專題',
docs: 'https://docs.rsshub.app/routes/other#tai-wan-shi-shi-cha-he-zhong-xin',
source: '/articles/category/:id+',
target: '/tfc-taiwan/category/:id',
},
{
title: '最新相關資訊',
docs: 'https://docs.rsshub.app/routes/other#tai-wan-shi-shi-cha-he-zhong-xin',
source: ['/articles/info', '/'],
target: '/tfc-taiwan/info',
},
{
title: '最新查核報告',
docs: 'https://docs.rsshub.app/routes/other#tai-wan-shi-shi-cha-he-zhong-xin',
source: ['/articles/report', '/'],
target: '/tfc-taiwan/report',
},
{
title: '重點專區',
docs: 'https://docs.rsshub.app/routes/other#tai-wan-shi-shi-cha-he-zhong-xin',
source: '/topic/:id',
target: '/tfc-taiwan/topic/:id',
},
],
},
};
7 changes: 7 additions & 0 deletions lib/v2/tfc-taiwan/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (router) => {
router.get('/', require('./index'));
router.get('/category/:id+', require('./index'));
router.get('/info', require('./index'));
router.get('/report', require('./index'));
router.get('/topic/:id', require('./index'));
};
7 changes: 7 additions & 0 deletions lib/v2/tfc-taiwan/templates/article.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ if headerImage }}
<img src="{{ headerImage }}"><br>
{{ /if }}

{{ if content }}
{{@ content }}
{{ /if }}
56 changes: 56 additions & 0 deletions lib/v2/tfc-taiwan/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { join } = require('path');
const { art } = require('@/utils/render');
const asyncPool = require('tiny-async-pool');
const { parseDate } = require('@/utils/parse-date');

const asyncPoolAll = async (...args) => {
const results = [];
for await (const result of asyncPool(...args)) {
results.push(result);
}
return results;
};

const baseUrl = 'https://tfc-taiwan.org.tw';

const parseList = (item) => {
const a = item.find('.entity-list-title a');
return {
title: a.text(),
description: item.find('.entity-list-body').text(),
link: new URL(a.attr('href'), baseUrl).href,
pubDate: item.find('.post-date').length ? parseDate(item.find('.post-date').text(), 'YYYY-MM-DD') : undefined,
image: item.find('.entity-list-img img').attr('src').split('?')[0],
};
};

const parseItems = (list, tryGet) =>
asyncPoolAll(10, list, (item) =>
tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = cheerio.load(response);

$('.field-name-field-addthis, #fb-root, .fb-comments, .likecoin-embed, style[type="text/css"]').remove();

item.description = art(join(__dirname, 'templates/article.art'), {
headerImage: item.image,
content: $('#block-system-main .node-content').html(),
});

item.pubDate = $('meta[property="article:published_time"]').attr('content');
item.updated = $('meta[property="article:modified_time"]').attr('content');
item.category = $('.node-tags .field-item')
.toArray()
.map((item) => $(item).text());

return item;
})
);

module.exports = {
baseUrl,
parseList,
parseItems,
};
22 changes: 22 additions & 0 deletions website/docs/routes/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,28 @@ Refer to [the list of supported currencies](https://wise.com/tools/exchange-rate

<Route author="xyqfer" example="/sogou/doodles" path="/sogou/doodles"/>

## 台灣事實查核中心 {#tai-wan-shi-shi-cha-he-zhong-xin}

### 最新相關資訊 / 最新查核報告 {#tai-wan-shi-shi-cha-he-zhong-xin-zui-xin-xiang-guan-zi-xun-%2F-zui-xin-cha-he-bao-gao}

<Route author="TonyRL" example="/tfc-taiwan" path="/tfc-taiwan/:type?" paramsDesc={['分類,見下表,預設為 `report`']} radar="1" rssbud="1">

| 最新相關資訊 | 最新查核報告 |
| ---------- | ---------- |
| info | report |

</Route>

### 專題 / 重點專區 {#tai-wan-shi-shi-cha-he-zhong-xin-zhuan-ti-%2F-zhong-dian-zhuan-qu}

<Route author="TonyRL" example="/tfc-taiwan" path="/tfc-taiwan/:type/:id+" paramsDesc={['專題 / 重點專區,見下表', '專題 / 重點專區 ID']} radar="1" rssbud="1">

| 專題 | 重點專區 |
| -------- | ------- |
| category | topic |

</Route>

## 腾讯吐个槽 {#teng-xun-tu-ge-cao}

### 吐槽新帖 {#teng-xun-tu-ge-cao-tu-cao-xin-tie}
Expand Down

0 comments on commit 30cfd44

Please sign in to comment.