fix: 每日一图根本没有图的问题 (#1672)

#1653 PR 质量实在是堪忧。。。
This commit is contained in:
Henry Wang
2019-03-04 19:17:22 +08:00
committed by DIYgod
parent 2aff7a8e1c
commit e938ff9d71
2 changed files with 33 additions and 29 deletions
+2 -2
View File
@@ -3077,7 +3077,7 @@ type 为 all 时,category 参数不支持 cost 和 free
</route>
### instapaper
### Instapaper
<route name="个人分享" author="LogicJake" example="/instapaper/person/viridiano" path="/instapaper/person"/>
@@ -3093,7 +3093,7 @@ type 为 all 时,category 参数不支持 cost 和 free
### 北京天文馆
<route name="每日一图" author="radaiming" example="/bjp/apod" path="/bjp/apod"/>
<route name="每日一图" author="HenryQW" example="/bjp/apod" path="/bjp/apod"/>
### 洛谷
+31 -27
View File
@@ -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 = `<img referrerpolicy="no-referrer" src="${baseUrl}${content.find('img').attr('src')}">`;
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);
})
);