diff --git a/docs/new-media.md b/docs/new-media.md index 67b7583a87..71dc92f1a3 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -336,7 +336,7 @@ Supported sub-sites: ### 焦点资讯 - + | 城市名 | 缩写 | | ------ | ---- | @@ -347,6 +347,8 @@ Supported sub-sites: 更多城市请参见 [这里](http://www.bendibao.com/city.htm) +> **香港特别行政区** 和 **澳门特别行政区** 的本地宝城市页面不更新资讯。 + ## 币世界 diff --git a/lib/routes/bendibao/news.js b/lib/routes/bendibao/news.js index cf10d8baf4..9c91c2372e 100644 --- a/lib/routes/bendibao/news.js +++ b/lib/routes/bendibao/news.js @@ -4,13 +4,18 @@ const cheerio = require('cheerio'); module.exports = async (ctx) => { const city = ctx.params.city; - const response = await got({ + let response = await got({ method: 'get', url: `http://${city}.bendibao.com/`, }); - const $ = cheerio.load(response.data); - const list = $('ul.focus-news li') + let $ = cheerio.load(response.data); + const title = + $('title') + .text() + .replace(/-爱上本地宝,生活会更好/, '') + `焦点资讯`; + + let list = $('ul.focus-news li') .map((_, item) => { item = $(item).find('a'); return { @@ -20,6 +25,32 @@ module.exports = async (ctx) => { }) .get(); + // Cities share 2 sets of ui. + // + // eg1. http://bj.bendibao.com/ + // eg2. http://kel.bendibao.com/ + // + // Go to /news to fetch data for the eg2 case. + + if (!list.length) { + response = await got({ + method: 'get', + url: `http://${city}.bendibao.com/news`, + }); + + $ = cheerio.load(response.data); + + list = $('#listNewsTimeLy div.info') + .map((_, item) => { + item = $(item).find('a'); + return { + title: item.text(), + link: item.attr('href'), + }; + }) + .get(); + } + const items = await Promise.all( list.map( async (item) => @@ -42,10 +73,7 @@ module.exports = async (ctx) => { ); ctx.state.data = { - title: - $('title') - .text() - .replace(/-爱上本地宝,生活会更好/, '') + `焦点资讯`, + title: title, link: `http://${city}.bendibao.com/`, item: items, };