diff --git a/lib/router.js b/lib/router.js index 114c1aa47..1a14d00ca 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2866,7 +2866,7 @@ router.get('/ptpress/book/:type?', lazyloadRouteHandler('./routes/ptpress/book') router.get('/uniqlo/stylingbook/:category?', lazyloadRouteHandler('./routes/uniqlo/stylingbook')); // 本地宝焦点资讯 -router.get('/bendibao/news/:city', lazyloadRouteHandler('./routes/bendibao/news')); +// router.get('/bendibao/news/:city', lazyloadRouteHandler('./routes/bendibao/news')); // unit-image router.get('/unit-image/films/:type?', lazyloadRouteHandler('./routes/unit-image/films')); diff --git a/lib/v2/bendibao/maintainer.js b/lib/v2/bendibao/maintainer.js new file mode 100644 index 000000000..560f08825 --- /dev/null +++ b/lib/v2/bendibao/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news/:city': ['nczitzk'], +}; diff --git a/lib/routes/bendibao/news.js b/lib/v2/bendibao/news.js similarity index 56% rename from lib/routes/bendibao/news.js rename to lib/v2/bendibao/news.js index b9c440e99..1fe7a93f8 100644 --- a/lib/routes/bendibao/news.js +++ b/lib/v2/bendibao/news.js @@ -1,12 +1,16 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const city = ctx.params.city; + const rootUrl = `http://${city}.bendibao.com`; + let response = await got({ method: 'get', - url: `http://${city}.bendibao.com/`, + url: rootUrl, }); let $ = cheerio.load(response.data); @@ -15,15 +19,18 @@ module.exports = async (ctx) => { .text() .replace(/-爱上本地宝,生活会更好/, '') + `焦点资讯`; - let list = $('ul.focus-news li') - .map((_, item) => { + let items = $('ul.focus-news li') + .toArray() + .map((item) => { item = $(item).find('a'); + + const link = item.attr('href'); + return { title: item.text(), - link: item.attr('href'), + link: link.indexOf('http') === 0 ? link : `${rootUrl}${link}`, }; - }) - .get(); + }); // Cities share 2 sets of ui. // @@ -32,7 +39,7 @@ module.exports = async (ctx) => { // // Go to /news to fetch data for the eg2 case. - if (!list.length) { + if (!items.length) { response = await got({ method: 'get', url: `http://${city}.bendibao.com/news`, @@ -40,37 +47,47 @@ module.exports = async (ctx) => { $ = cheerio.load(response.data); - list = $('#listNewsTimeLy div.info') - .map((_, item) => { + items = $('#listNewsTimeLy div.info') + .toArray() + .map((item) => { item = $(item).find('a'); + + const link = item.attr('href'); + return { title: item.text(), - link: item.attr('href'), + link: link.indexOf('http') === 0 ? link : `${rootUrl}${link}`, }; - }) - .get(); + }); } - const items = await Promise.all( - list.map((item) => + items = await Promise.all( + items.map((item) => ctx.cache.tryGet(item.link, async () => { try { - const detailResponse = await got({ method: 'get', url: item.link }); + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); // Some links lead to mobile-view pages. // eg. http://m.bj.bendibao.com/news/273517.html // Divs for contents are different from which in desktop-view pages. - item.description = content('div.content').html() || content('div.content-box').html(); + item.description = content('div.content').html() ?? content('div.content-box').html(); // Spans for publish dates are the same cases as above. - item.pubDate = new Date( - (content('span.time') - .text() - .replace(/发布时间:/, '') || content('span.public_time').text()) + ' GMT+8' - ).toUTCString(); + item.pubDate = timezone( + parseDate( + content('span.time') + .text() + .replace(/发布时间:/, '') ?? content('span.public_time').text() + ), + +8 + ); return item; } catch (e) { @@ -82,7 +99,7 @@ module.exports = async (ctx) => { ctx.state.data = { title, - link: `http://${city}.bendibao.com/`, + link: rootUrl, item: items, }; }; diff --git a/lib/v2/bendibao/radar.js b/lib/v2/bendibao/radar.js new file mode 100644 index 000000000..fa69e7598 --- /dev/null +++ b/lib/v2/bendibao/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'bendibao.com': { + _name: '本地宝', + '.': [ + { + title: '焦点资讯', + docs: 'https://docs.rsshub.app/new-media.html#ben-di-bao-jiao-dian-zi-xun', + source: '/', + target: '/bendibao/news/:city', + }, + ], + }, +}; diff --git a/lib/v2/bendibao/router.js b/lib/v2/bendibao/router.js new file mode 100644 index 000000000..81f7d3965 --- /dev/null +++ b/lib/v2/bendibao/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/news/:city', require('./news')); +};