From 4dc3362e41bb19bb883ea126af1aa20ea809043d Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sun, 17 Jul 2022 20:24:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E5=85=AC=E8=A6=96=E6=96=B0?= =?UTF-8?q?=E8=81=9E=E7=B6=B2=20(#10236)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/traditional-media.md | 39 ++++++++++++++++++++++++ lib/v2/pts/curations.js | 44 +++++++++++++++++++++++++++ lib/v2/pts/{dailynews.js => index.js} | 35 ++++++++++++++------- lib/v2/pts/maintainer.js | 6 ++++ lib/v2/pts/projects.js | 44 +++++++++++++++++++++++++++ lib/v2/pts/radar.js | 40 ++++++++++++++++++++++-- lib/v2/pts/router.js | 5 ++- lib/v2/pts/templates/description.art | 6 ++++ 8 files changed, 205 insertions(+), 14 deletions(-) create mode 100644 lib/v2/pts/curations.js rename lib/v2/pts/{dailynews.js => index.js} (52%) create mode 100644 lib/v2/pts/projects.js create mode 100644 lib/v2/pts/templates/description.art diff --git a/docs/traditional-media.md b/docs/traditional-media.md index fe90d4473..1ed858d7d 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -878,6 +878,45 @@ Type 栏目: +### 專題策展 + + + +### 觀點 + + + +### 數位敘事 + + + +### 深度報導 + + + +### 分類 + + + +| 名称 | 编号 | +| ---- | -- | +| 政治 | 1 | +| 社會 | 7 | +| 全球 | 4 | +| 生活 | 5 | +| 兩岸 | 9 | +| 地方 | 11 | +| 產經 | 10 | +| 文教科技 | 6 | +| 環境 | 3 | +| 社福人權 | 12 | + + + +### 標籤 + + + ## 共同网 ### 最新报道 diff --git a/lib/v2/pts/curations.js b/lib/v2/pts/curations.js new file mode 100644 index 000000000..c48cc3fca --- /dev/null +++ b/lib/v2/pts/curations.js @@ -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, + }; +}; diff --git a/lib/v2/pts/dailynews.js b/lib/v2/pts/index.js similarity index 52% rename from lib/v2/pts/dailynews.js rename to lib/v2/pts/index.js index 8eb7d47f8..f74e17e75 100644 --- a/lib/v2/pts/dailynews.js +++ b/lib/v2/pts/index.js @@ -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 = `${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, }; diff --git a/lib/v2/pts/maintainer.js b/lib/v2/pts/maintainer.js index 8eaa4e035..0441ed933 100644 --- a/lib/v2/pts/maintainer.js +++ b/lib/v2/pts/maintainer.js @@ -1,3 +1,9 @@ module.exports = { + '/category/:id': ['nczitzk'], + '/curations': ['nczitzk'], '/dailynews': ['nczitzk'], + '/opinion': ['nczitzk'], + '/projects': ['nczitzk'], + '/report': ['nczitzk'], + '/tag/:id': ['nczitzk'], }; diff --git a/lib/v2/pts/projects.js b/lib/v2/pts/projects.js new file mode 100644 index 000000000..93198627d --- /dev/null +++ b/lib/v2/pts/projects.js @@ -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 ? `

${description}

` : '', + }), + }; + }); + + ctx.state.data = { + title: $('title') + .text() + .replace(/第\d+頁 | /, ''), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/pts/radar.js b/lib/v2/pts/radar.js index a5ff8e2bd..557aaa0c1 100644 --- a/lib/v2/pts/radar.js +++ b/lib/v2/pts/radar.js @@ -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', + }, ], }, }; diff --git a/lib/v2/pts/router.js b/lib/v2/pts/router.js index e2bb9b13d..41f845414 100644 --- a/lib/v2/pts/router.js +++ b/lib/v2/pts/router.js @@ -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')); }; diff --git a/lib/v2/pts/templates/description.art b/lib/v2/pts/templates/description.art new file mode 100644 index 000000000..69e93d20c --- /dev/null +++ b/lib/v2/pts/templates/description.art @@ -0,0 +1,6 @@ +{{ if image }} + +{{ /if }} +{{ if description }} +{{@ description }} +{{ /if }} \ No newline at end of file