From e938ff9d71726c5f78b50738c5c6bba38abf74ba Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Mon, 4 Mar 2019 11:17:22 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=AF=8F=E6=97=A5=E4=B8=80=E5=9B=BE?= =?UTF-8?q?=E6=A0=B9=E6=9C=AC=E6=B2=A1=E6=9C=89=E5=9B=BE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#1672)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #1653 PR 质量实在是堪忧。。。 --- docs/README.md | 4 +-- lib/routes/bjp/apod.js | 58 ++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/docs/README.md b/docs/README.md index f8dc0bd85a..ca33b6dfee 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3077,7 +3077,7 @@ type 为 all 时,category 参数不支持 cost 和 free -### instapaper +### Instapaper @@ -3093,7 +3093,7 @@ type 为 all 时,category 参数不支持 cost 和 free ### 北京天文馆 - + ### 洛谷 diff --git a/lib/routes/bjp/apod.js b/lib/routes/bjp/apod.js index 2e86997dd1..488a980971 100644 --- a/lib/routes/bjp/apod.js +++ b/lib/routes/bjp/apod.js @@ -5,41 +5,45 @@ module.exports = async (ctx) => { const baseUrl = 'http://www.bjp.org.cn'; const indexUrl = baseUrl + '/col/col89/index.html'; - let limit = parseInt(ctx.query.limit); - if (isNaN(limit) || limit === 0) { - limit = 5; - } const res = await axios.get(indexUrl); - const data = res.data; - const $ = cheerio.load(data); - const list = $('b > a'); - const urls = []; - const titles = []; - for (let i = 0; i < limit && i < list.length; i++) { - urls.push(baseUrl + list[i].attribs.href); - titles.push(list[i].attribs.title); - } + + const $ = cheerio.load(res.data); + + const list = $('b > a') + .slice(0, 10) + .get() + .map((e) => $(e)); const items = await Promise.all( - urls.map(async (url, i) => { - const cache = await ctx.cache.get(url); + list.map(async (e) => { + const link = `${baseUrl}${e.attr('href')}`; + + const cache = await ctx.cache.get(link); if (cache) { return Promise.resolve(JSON.parse(cache)); } - const data = (await axios.get(url)).data; - const $ = cheerio.load(data); - const time = $('meta[name=pubDate]').attr('content'); - const title = titles[i]; + + const data = await axios.get(link); + const $ = cheerio.load(data.data); + + const content = $($('body > table')[1]); + + const image = ``; + const item = { - title, - pubDate: new Date(time).toUTCString(), - description: $('body > table > tbody') - .eq(1) - .html(), - link: url, - guid: url, + title: $(e).attr('title'), + pubDate: new Date($('meta[name=pubDate]').attr('content')).toUTCString(), + description: + image + + content + .find('table') + .last() + .html(), + link, + guid: link, }; - ctx.cache.set(url, JSON.stringify(item), limit * 24 * 60 * 60); + ctx.cache.set(link, JSON.stringify(item), 5 * 24 * 60 * 60); + return Promise.resolve(item); }) );