fix: 知识分子资讯 (#6804)

This commit is contained in:
Ethan Shen 2021-01-30 13:27:05 +08:00 committed by GitHub
parent 3783375530
commit e942cefde8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 50 deletions

View File

@ -1,67 +1,67 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const cheerio = require('cheerio');
const got = require('@/utils/got');
const date = require('@/utils/date');
const base = 'http://zhishifenzi.com';
const titles = {
multiple: '综合',
biology: '生物',
medicine: '医药',
ai: '人工智能',
physics: '物理',
chymistry: '化学',
astronomy: '天文',
other: '其他',
};
module.exports = async (ctx) => {
let link = `${base}/news/`;
const type = ctx.params.type;
// type can be on of:
// ["biology", "medicine", "ai", "physics", "chymistry", "astronomy", "others"]
if (typeof type !== 'undefined') {
link = `${base}/news/${type}`;
}
const response = await got.get(encodeURI(link));
const pageCapture = cheerio.load(response.data);
// ["biology", "medicine", "ai", "physics", "chymistry", "astronomy", "other"]
const type = ctx.params.type || 'multiple';
let title = pageCapture('div.all_categories > ul > li.category').text().split('').pop();
if (title === '') {
title = '全部資訊';
} else {
title = `資訊「${title}`;
}
const list = pageCapture('div.inner_news_list > ul > li.clearfix').get();
const out = await Promise.all(
list.map(async (elem) => {
const $ = cheerio.load(elem);
const title = $('div > h5 > a').text();
const partial = $('div > h5 > a').attr('href');
const address = url.resolve(base, partial);
const pubDate = date($('span').first().text());
const rootUrl = 'http://zhishifenzi.com';
const currentUrl = url.resolve(rootUrl, '/news/ajaxarticles');
const response = await got({
method: 'post',
url: currentUrl,
form: {
category: type,
},
});
// const keywords = $('span')
// .last()
// .html();
const item = {
title,
pubDate,
link: encodeURI(address),
const $ = cheerio.load(response.data);
const list = $('h5 a')
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: url.resolve(rootUrl, item.attr('href')),
};
const value = await ctx.cache.get(address);
if (value) {
item.description = value;
} else {
const detail = await got.get(item.link);
const detailCapture = cheerio.load(detail.data);
item.description = detailCapture('.main div.inner_content')
.html()
.replace(/src="\//g, `src="${base}/`);
ctx.cache.set(address, item.description);
}
return Promise.resolve(item);
})
.get();
const out = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.description = content('.inner_content').html();
item.pubDate = date(content('.inner_view_from').text());
return item;
})
)
);
ctx.state.data = {
title: `知識分子 | ${title}`,
title: `知識分子 | 資訊「${titles[type] || '综合'}`,
description: `知識分子 | 科學 文明 智慧`,
link: link,
link: url.resolve(rootUrl, '/news'),
item: out,
};
};