feat(route): 公視新聞網 (#10236)

This commit is contained in:
Ethan Shen 2022-07-17 20:24:20 +08:00 committed by GitHub
parent 16e2606b17
commit 4dc3362e41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 205 additions and 14 deletions

View File

@ -878,6 +878,45 @@ Type 栏目:
<Route author="nczitzk" example="/pts/dailynews" path="/pts/dailynews"/>
### 專題策展
<Route author="nczitzk" example="/pts/curations" path="/pts/curations"/>
### 觀點
<Route author="nczitzk" example="/pts/opinion" path="/pts/opinion"/>
### 數位敘事
<Route author="nczitzk" example="/pts/projects" path="/pts/projects"/>
### 深度報導
<Route author="nczitzk" example="/pts/report" path="/pts/report"/>
### 分類
<Route author="nczitzk" example="/pts/category/9" path="/pts/category/:id" :paramsDesc="['分類 id见下表可在对应分類页 URL 中找到']">
| 名称 | 编号 |
| ---- | -- |
| 政治 | 1 |
| 社會 | 7 |
| 全球 | 4 |
| 生活 | 5 |
| 兩岸 | 9 |
| 地方 | 11 |
| 產經 | 10 |
| 文教科技 | 6 |
| 環境 | 3 |
| 社福人權 | 12 |
</Route>
### 標籤
<Route author="nczitzk" example="/pts/tag/230" path="/pts/tag/:id" :paramsDesc="['標籤 id可在对应標籤页 URL 中找到']"/>
## 共同网
### 最新报道

44
lib/v2/pts/curations.js Normal file
View File

@ -0,0 +1,44 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const rootUrl = 'https://news.pts.org.tw';
const currentUrl = `${rootUrl}/curations`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const items = $('.project-intro')
.last()
.find('h3 a')
.toArray()
.map((item) => {
item = $(item);
const projectDiv = item.parent().parent();
return {
title: item.text(),
link: item.attr('href'),
pubDate: parseDate(projectDiv.find('time').text()),
description: art(path.join(__dirname, 'templates/description.art'), {
image: projectDiv.parent().find('.cover-fit').attr('src'),
}),
};
});
ctx.state.data = {
title: $('title')
.text()
.replace(/第\d+頁 /, ''),
link: currentUrl,
item: items,
};
};

View File

@ -2,10 +2,12 @@ const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const rootUrl = 'https://news.pts.org.tw';
const currentUrl = `${rootUrl}/dailynews`;
const currentUrl = `${rootUrl}${ctx.path === '/' ? '/dailynews' : ctx.path}`;
const response = await got({
method: 'get',
@ -14,19 +16,20 @@ module.exports = async (ctx) => {
const $ = cheerio.load(response.data);
const list = $('h2 a')
.map((_, item) => {
let items = $('h2 a')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 30)
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
})
.get();
});
const items = await Promise.all(
list.map((item) =>
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
@ -35,17 +38,27 @@ module.exports = async (ctx) => {
const content = cheerio.load(detailResponse.data);
item.author = content('meta[property="article:author"]').attr('content');
item.author = content('meta[property="article:author"]').attr('content').split(' / ')[0];
item.pubDate = timezone(parseDate(content('meta[property="article:published_time"]').attr('content')), +8);
item.description = `<img src="${content('meta[property="og:image"]').attr('content')}">${content('.post-article').html()}`;
item.category = content('.tag-list')
.first()
.find('.blue-tag')
.toArray()
.map((t) => content(t).text())
.filter((t) => t !== '...');
item.description = art(path.join(__dirname, 'templates/description.art'), {
image: content('meta[property="og:image"]').attr('content'),
description: content('.post-article').html(),
});
return item;
})
)
);
ctx.state.data = {
title: '即時 公視新聞網 PNN',
title: $('title')
.text()
.replace(/第\d+頁 /, ''),
link: currentUrl,
item: items,
};

View File

@ -1,3 +1,9 @@
module.exports = {
'/category/:id': ['nczitzk'],
'/curations': ['nczitzk'],
'/dailynews': ['nczitzk'],
'/opinion': ['nczitzk'],
'/projects': ['nczitzk'],
'/report': ['nczitzk'],
'/tag/:id': ['nczitzk'],
};

44
lib/v2/pts/projects.js Normal file
View File

@ -0,0 +1,44 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const rootUrl = 'https://news.pts.org.tw';
const currentUrl = `${rootUrl}/projects`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const items = $('h3 a')
.toArray()
.map((item) => {
item = $(item);
const projectDiv = item.parent().parent();
const description = projectDiv.find('p').text();
return {
title: item.text(),
link: item.attr('href'),
pubDate: parseDate(projectDiv.find('time').text()),
description: art(path.join(__dirname, 'templates/description.art'), {
image: projectDiv.parent().find('.cover-fit')?.attr('src') ?? projectDiv.parent().parent().find('.cover-fit').attr('src'),
description: description ? `<p>${description}</p>` : '',
}),
};
});
ctx.state.data = {
title: $('title')
.text()
.replace(/第\d+頁 /, ''),
link: currentUrl,
item: items,
};
};

View File

@ -3,11 +3,47 @@ module.exports = {
_name: '公視新聞網 PNN',
news: [
{
title: '即時',
title: '即時新聞',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-shi-xin-wen-wang-ji-shi-xin-wen',
source: ['/dailynews'],
source: ['/dailynews', '/'],
target: '/pts/dailynews',
},
{
title: '專題策展',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-shi-xin-wen-wang-zhuan-ti-ce-zhan',
source: ['/curations', '/'],
target: '/pts/curations',
},
{
title: '觀點',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-shi-xin-wen-wang-guan-dian',
source: ['/opinion', '/'],
target: '/pts/opinion',
},
{
title: '數位敘事',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-shi-xin-wen-wang-shu-wei-xu-shi',
source: ['/projects', '/'],
target: '/pts/projects',
},
{
title: '深度報導',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-shi-xin-wen-wang-shen-du-bao-dao',
source: ['/report', '/'],
target: '/pts/report',
},
{
title: '分類',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-shi-xin-wen-wang-fen-lei',
source: ['/category/:id', '/'],
target: '/pts/category/:id',
},
{
title: '標籤',
docs: 'https://docs.rsshub.app/traditional-media.html#gong-shi-xin-wen-wang-biao-qian',
source: ['/tag/:id', '/'],
target: '/pts/tag/:id',
},
],
},
};

View File

@ -1,3 +1,6 @@
module.exports = function (router) {
router.get('/dailynews', require('./dailynews'));
router.get('/curations', require('./curations'));
router.get('/projects', require('./projects'));
router.get(/([\w-/]+)?/, require('./index'));
router.get('', require('./index'));
};

View File

@ -0,0 +1,6 @@
{{ if image }}
<img src="{{ image }}">
{{ /if }}
{{ if description }}
{{@ description }}
{{ /if }}