From 69210f7cadf7e7e00adeb22e0ac3e124c40a4aa2 Mon Sep 17 00:00:00 2001 From: Jigsaw Date: Sat, 9 Apr 2022 19:06:03 +0800 Subject: [PATCH] fix(route)(twreporter): title,author,pubDate (#9502) * fix(route): twreporter title,author,pubDate * refactor: migrate to v2 * fix: missing maintainer --- lib/router.js | 8 +-- lib/routes/twreporter/photography.js | 57 ------------------- lib/v2/miris/maintainer.js | 3 + lib/v2/stratechery/maintainer.js | 3 + lib/{routes => v2}/twreporter/category.js | 19 +++---- .../twreporter/fetch_article.js | 9 +-- lib/v2/twreporter/maintainer.js | 5 ++ lib/{routes => v2}/twreporter/newest.js | 21 +++---- lib/v2/twreporter/photography.js | 46 +++++++++++++++ lib/v2/twreporter/radar.js | 25 ++++++++ lib/v2/twreporter/router.js | 5 ++ lib/v2/xueqiu/maintainer.js | 10 ++++ 12 files changed, 121 insertions(+), 90 deletions(-) delete mode 100644 lib/routes/twreporter/photography.js create mode 100644 lib/v2/miris/maintainer.js create mode 100644 lib/v2/stratechery/maintainer.js rename lib/{routes => v2}/twreporter/category.js (63%) rename lib/{routes => v2}/twreporter/fetch_article.js (89%) create mode 100644 lib/v2/twreporter/maintainer.js rename lib/{routes => v2}/twreporter/newest.js (53%) create mode 100644 lib/v2/twreporter/photography.js create mode 100644 lib/v2/twreporter/radar.js create mode 100644 lib/v2/twreporter/router.js create mode 100644 lib/v2/xueqiu/maintainer.js diff --git a/lib/router.js b/lib/router.js index 0b5afe58a..fee055237 100644 --- a/lib/router.js +++ b/lib/router.js @@ -42,10 +42,10 @@ router.get('/bangumi/group/:id', lazyloadRouteHandler('./routes/bangumi/group/to router.get('/bangumi/subject/:id', lazyloadRouteHandler('./routes/bangumi/subject')); router.get('/bangumi/user/blog/:id', lazyloadRouteHandler('./routes/bangumi/user/blog')); -// 報導者 -router.get('/twreporter/newest', lazyloadRouteHandler('./routes/twreporter/newest')); -router.get('/twreporter/photography', lazyloadRouteHandler('./routes/twreporter/photography')); -router.get('/twreporter/category/:cid', lazyloadRouteHandler('./routes/twreporter/category')); +// 報導者 migrated to v2 +// router.get('/twreporter/newest', lazyloadRouteHandler('./routes/twreporter/newest')); +// router.get('/twreporter/photography', lazyloadRouteHandler('./routes/twreporter/photography')); +// router.get('/twreporter/category/:cid', lazyloadRouteHandler('./routes/twreporter/category')); // 微博 router.get('/weibo/user/:uid/:routeParams?', lazyloadRouteHandler('./routes/weibo/user')); diff --git a/lib/routes/twreporter/photography.js b/lib/routes/twreporter/photography.js deleted file mode 100644 index ebcee258f..000000000 --- a/lib/routes/twreporter/photography.js +++ /dev/null @@ -1,57 +0,0 @@ -const cheerio = require('cheerio'); -const got = require('@/utils/got'); - -const fetch = require('./fetch_article'); - -module.exports = async (ctx) => { - const baseURL = 'https://www.twreporter.org'; - const url = baseURL + `/photography`; - const res = await got.get(url); - const $ = cheerio.load(res.data); - const coverList = $('.WPJvn').get(); - const commonList = $('.eVNsZf').get(); - - const coverView = await Promise.all( - coverList.map(async (item) => { - const $ = cheerio.load(item); - const address = baseURL + $('li > a').attr('href'); - const title = $('.gRCDdm').text(); - - const cache = await ctx.cache.get(address); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - const single = await fetch(address); - single.title = title; - - ctx.cache.set(address, JSON.stringify(single)); - return Promise.resolve(single); - }) - ); - - const listView = await Promise.all( - commonList.map(async (item) => { - const $ = cheerio.load(item); - const address = baseURL + $('li > a').attr('href'); - const title = $('.etJLWI').text(); - - const cache = await ctx.cache.get(address); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - const single = await fetch(address); - single.title = title; - - ctx.cache.set(address, JSON.stringify(single)); - return Promise.resolve(single); - }) - ); - - ctx.state.data = { - title: `報導者 | 影像`, - link: url, - item: coverView.concat(listView), - }; -}; diff --git a/lib/v2/miris/maintainer.js b/lib/v2/miris/maintainer.js new file mode 100644 index 000000000..6e3d95d5e --- /dev/null +++ b/lib/v2/miris/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/blog': ['chazeon'], +}; diff --git a/lib/v2/stratechery/maintainer.js b/lib/v2/stratechery/maintainer.js new file mode 100644 index 000000000..5be377b1f --- /dev/null +++ b/lib/v2/stratechery/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/': ['chazeon'], +}; diff --git a/lib/routes/twreporter/category.js b/lib/v2/twreporter/category.js similarity index 63% rename from lib/routes/twreporter/category.js rename to lib/v2/twreporter/category.js index a55ca06e1..0947f5422 100644 --- a/lib/routes/twreporter/category.js +++ b/lib/v2/twreporter/category.js @@ -6,26 +6,21 @@ const fetch = require('./fetch_article'); module.exports = async (ctx) => { const baseURL = 'https://www.twreporter.org'; const url = baseURL + `/categories/${ctx.params.cid}`; - const res = await got.get(url); + const res = await got(url); const $ = cheerio.load(res.data); const list = $('.lnKPLr').get(); const category = $('.kCfkTU').text(); const out = await Promise.all( - list.map(async (item) => { + list.map((item) => { const $ = cheerio.load(item); const address = baseURL + $('a').attr('href'); const title = $('.list-item__Title-sc-1dx5lew-5').text(); - const cache = await ctx.cache.get(address); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - const single = await fetch(address); - single.title = title; - - ctx.cache.set(address, JSON.stringify(single)); - return Promise.resolve(single); + return ctx.cache.tryGet(address, async () => { + const single = await fetch(address); + single.title = title; + return single; + }); }) ); ctx.state.data = { diff --git a/lib/routes/twreporter/fetch_article.js b/lib/v2/twreporter/fetch_article.js similarity index 89% rename from lib/routes/twreporter/fetch_article.js rename to lib/v2/twreporter/fetch_article.js index 9c7782c70..5cb77443f 100644 --- a/lib/routes/twreporter/fetch_article.js +++ b/lib/v2/twreporter/fetch_article.js @@ -1,18 +1,19 @@ const cheerio = require('cheerio'); const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async function fetch(address) { - const res = await got.get(address); + const res = await got(address); const capture = cheerio.load(res.data); capture('.gIMvvS').remove(); - let metaInfoBox = capture('.iRdvCH') + let metaInfoBox = capture('.ffAPnj') .filter((index) => index === 0) .get(); // For photography if (metaInfoBox.length === 0) { - metaInfoBox = capture('.dPQBaW') + metaInfoBox = capture('.deNvJY') .filter((index) => index === 0) .get(); } @@ -61,6 +62,6 @@ module.exports = async function fetch(address) { description: contents, link: address, guid: address, - pubDate: new Date(time).toUTCString(), + pubDate: parseDate(time, 'M/D/YYYY'), }; }; diff --git a/lib/v2/twreporter/maintainer.js b/lib/v2/twreporter/maintainer.js new file mode 100644 index 000000000..d01baa76a --- /dev/null +++ b/lib/v2/twreporter/maintainer.js @@ -0,0 +1,5 @@ +module.exports = { + '/category/:cid': ['emdoe'], + '/newest': ['emdoe'], + '/photography': ['emdoe'], +}; diff --git a/lib/routes/twreporter/newest.js b/lib/v2/twreporter/newest.js similarity index 53% rename from lib/routes/twreporter/newest.js rename to lib/v2/twreporter/newest.js index fb35d8383..f3e8685c4 100644 --- a/lib/routes/twreporter/newest.js +++ b/lib/v2/twreporter/newest.js @@ -5,25 +5,20 @@ const fetch = require('./fetch_article'); module.exports = async (ctx) => { const url = 'https://www.twreporter.org'; - const res = await got.get(url); + const res = await got(url); const $ = cheerio.load(res.data); const list = $('.gKMjSz').get(); const out = await Promise.all( - list.map(async (item) => { + list.map((item) => { const $ = cheerio.load(item); const address = url + $('a').attr('href'); - const title = $('.dpNivU').text(); - const cache = await ctx.cache.get(address); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - const single = await fetch(address); - single.title = title; - - ctx.cache.set(address, JSON.stringify(single)); - return Promise.resolve(single); + const title = $('.latest-section__Title-hzxpx3-6').text(); + return ctx.cache.tryGet(address, async () => { + const single = await fetch(address); + single.title = title; + return single; + }); }) ); ctx.state.data = { diff --git a/lib/v2/twreporter/photography.js b/lib/v2/twreporter/photography.js new file mode 100644 index 000000000..0687d8633 --- /dev/null +++ b/lib/v2/twreporter/photography.js @@ -0,0 +1,46 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +const fetch = require('./fetch_article'); + +module.exports = async (ctx) => { + const baseURL = 'https://www.twreporter.org'; + const url = baseURL + `/photography`; + const res = await got(url); + const $ = cheerio.load(res.data); + const coverList = $('.WPJvn').get(); + const commonList = $('.eVNsZf').get(); + + const coverView = await Promise.all( + coverList.map((item) => { + const $ = cheerio.load(item); + const address = baseURL + $('li > a').attr('href'); + const title = $('.sc-1aojo4z-4').text(); + return ctx.cache.tryGet(address, async () => { + const single = await fetch(address); + single.title = title; + return single; + }); + }) + ); + + const listView = await Promise.all( + commonList.map((item) => { + const $ = cheerio.load(item); + const address = baseURL + $('li > a').attr('href'); + const title = $('.ii0887-4').text(); + + return ctx.cache.tryGet(address, async () => { + const single = await fetch(address); + single.title = title; + return single; + }); + }) + ); + + ctx.state.data = { + title: `報導者 | 影像`, + link: url, + item: coverView.concat(listView), + }; +}; diff --git a/lib/v2/twreporter/radar.js b/lib/v2/twreporter/radar.js new file mode 100644 index 000000000..efa451883 --- /dev/null +++ b/lib/v2/twreporter/radar.js @@ -0,0 +1,25 @@ +module.exports = { + 'twreporter.org': { + _name: '報導者', + '.': [ + { + title: '最新', + docs: 'https://docs.rsshub.app/new-media.html#bao-dao-zhe', + source: ['/'], + target: '/twreporter/newest', + }, + { + title: '摄影', + docs: 'https://docs.rsshub.app/new-media.html#bao-dao-zhe', + source: ['/photography'], + target: '/twreporter', + }, + { + title: '分类', + docs: 'https://docs.rsshub.app/new-media.html#bao-dao-zhe', + source: ['/categories/:tid'], + target: '/twreporter/category/:tid', + }, + ], + }, +}; diff --git a/lib/v2/twreporter/router.js b/lib/v2/twreporter/router.js new file mode 100644 index 000000000..dc7357793 --- /dev/null +++ b/lib/v2/twreporter/router.js @@ -0,0 +1,5 @@ +module.exports = (router) => { + router.get('/category/:cid', require('./category')); + router.get('/newest', require('./newest')); + router.get('/photography', require('./photography')); +}; diff --git a/lib/v2/xueqiu/maintainer.js b/lib/v2/xueqiu/maintainer.js new file mode 100644 index 000000000..cc863f20a --- /dev/null +++ b/lib/v2/xueqiu/maintainer.js @@ -0,0 +1,10 @@ +module.exports = { + '/favorite/:id': ['imlonghao'], + '/fund/:id': ['HenryQW', 'NathanDai'], + '/hots': ['hillerliao'], + '/snb/:id': ['ZhishanZhang'], + '/stock_comments/:id/:titleLength?': ['zytomorrow'], + '/stock_info/:id/:type?': ['YuYang'], + '/user/:id/:type?': ['imlonghao'], + '/user_stock/:id': ['hillerliao'], +};