修复知乎专栏和开源中国 (#1321)

怎么还顺便改了一些其他路由的格式……eslint抽了?
This commit is contained in:
tgly307
2018-12-31 00:58:37 +08:00
committed by DIYgod
parent fc26d5dbb4
commit 0e9a5de669
3 changed files with 53 additions and 12 deletions
+2 -2
View File
@@ -25,8 +25,7 @@ module.exports = async (ctx) => {
description: each.find('p', '.description').text(),
link: originalUrl,
};
if (/^\/news\/.*$/.test(originalUrl)) {
item.link = 'https://www.oschina.net' + originalUrl;
if (/^https:\/\/www.oschina.net\/news\/.*$/.test(originalUrl)) {
const key = 'oschina' + item.link;
const value = await ctx.cache.get(key);
@@ -41,6 +40,7 @@ module.exports = async (ctx) => {
},
});
const content = cheerio.load(detail.data);
content('.ad-wrap').remove();
item.description = content('#articleContent').html();
ctx.cache.set(key, item.description, 24 * 60 * 60);
}
+7 -1
View File
@@ -8,13 +8,19 @@ module.exports = {
ProcessImage: function(content) {
const $ = cheerio.load(content, { xmlMode: true });
$('img.content_image, img.origin_image, img.content-image').each((i, e) => {
$('img.content_image, img.origin_image, img.content-image, img.data-actualsrc').each((i, e) => {
if (e.attribs['data-original']) {
$(e).attr({
src: e.attribs['data-original'],
width: null,
height: null,
});
} else if (!e.attribs['data-original'] && e.attribs['data-actualsrc']) {
$(e).attr({
src: e.attribs['data-actualsrc'],
width: null,
height: null,
});
} else {
$(e).attr({
src: e.attribs.src.replace('_b.jpg', '_r.jpg'),
+44 -9
View File
@@ -1,12 +1,13 @@
const axios = require('../../utils/axios');
const utils = require('./utils');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const id = ctx.params.id;
const listRes = await axios({
method: 'get',
url: `https://zhuanlan.zhihu.com/api/columns/${id}/posts?limit=20`,
url: `https://zhuanlan.zhihu.com/api2/columns/${id}/articles`,
headers: {
...utils.header,
Referer: `https://zhuanlan.zhihu.com/${id}`,
@@ -29,22 +30,56 @@ module.exports = async (ctx) => {
throw 'info resource api returned status code ' + infoRes.status;
}
const list = listRes.data;
const list = listRes.data.data;
const info = infoRes.data;
const articles = await Promise.all(
list.map(async (article) => {
const url = article.url;
const item = {
title: article.title,
description: '',
link: article.url,
};
const key = 'zhuanlan' + article.url;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
const articleOriginal = await axios({
method: 'get',
url: url,
headers: {
Referer: url,
},
});
item.description = utils.ProcessImage(
cheerio
.load(articleOriginal.data)('.Post-RichText')
.html()
);
ctx.cache.set(key, item.description, 24 * 60 * 60);
}
return Promise.resolve(item);
})
);
if (!Array.isArray(list)) {
throw JSON.stringify(list);
}
ctx.state.data = {
title: `知乎专栏-${info.name}`,
title: `知乎专栏-${info.title}`,
link: `https://zhuanlan.zhihu.com/${id}`,
description: info.description,
item: list.map((item) => ({
title: item.title,
description: item.content.replace(/ src="/g, ' referrerpolicy="no-referrer" src="https://pic4.zhimg.com/'),
pubDate: new Date(item.publishedTime).toUTCString(),
link: `https://zhuanlan.zhihu.com${item.url}`,
})),
item: articles,
// list.map((item) => ({
// title: item.title,
// description: item.content.replace(/ src="/g, ' referrerpolicy="no-referrer" src="https://pic4.zhimg.com/'),
// pubDate: new Date(item.publishedTime).toUTCString(),
// link: `https://zhuanlan.zhihu.com${item.url}`,
// })),
};
};