feat: Add China Dialogue (#3748)

This commit is contained in:
zoenglinghou
2020-01-16 21:12:52 +08:00
committed by DIYgod
parent db809f7920
commit fbe26fe883
5 changed files with 162 additions and 1 deletions
+4
View File
@@ -2142,4 +2142,8 @@ router.get('/apnews/topics/:topic', require('./routes/apnews/topics'));
// discuz
router.get('/discuz/:link', require('./routes/discuz/discuz'));
// China Dialogue 中外对话
router.get('/chinadialogue/topics/:topic', require('./routes/chinadialogue/topics'));
router.get('/chinadialogue/:column', require('./routes/chinadialogue/column'));
module.exports = router;
+58
View File
@@ -0,0 +1,58 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const column = ctx.params.column;
const response = await got({
method: 'get',
url: `https://www.chinadialogue.net/${column}`,
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('article.main-listing');
let itemPicUrl;
let title;
let description;
ctx.state.data = {
title: $('title').text(),
link: `https://www.chinadialogue.net/${column}`,
item:
list &&
list
.map((index, item) => {
item = $(item);
itemPicUrl = item.find('img.img-responsive').attr('src');
title = [];
description = [];
item.find('h1').each(function(index, element) {
title.push(
$(element)
.text()
.replace(/(\r\n|\n|\r)/gm, '')
);
});
item.find('p').each(function(index, element) {
description.push(
$(element)
.text()
.replace(/(\r\n|\n|\r)/gm, '')
);
});
return {
title: title.join(' | '),
description: `<p>${description.join('</p><p>')}</p><img src="${itemPicUrl}">`,
pubDate: item.find('time.blog').attr('datetime'),
link: item.find('a').attr('href'),
};
})
.get(),
};
};
+55
View File
@@ -0,0 +1,55 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const topic = ctx.params.topic;
const response = await got({
method: 'get',
url: `https://www.chinadialogue.net/topics/${topic}`,
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('article.excerpt.col-md-6');
let itemPicUrl;
let title;
let description;
ctx.state.data = {
title: $('title')
.text()
.replace(/( 主题 >| Topics >)/gm, ''),
link: `https://www.chinadialogue.net/topics/${topic}`,
item:
list &&
list
.map((index, item) => {
item = $(item);
itemPicUrl = item.find('img.img-responsive').attr('src');
title = [];
description = [];
item.find('h1').each(function(index, element) {
title.push(
$(element)
.text()
.replace(/(\r\n|\n|\r)/gm, '')
);
});
item.find('p').each(function(index, element) {
description.push($(element).text());
});
return {
title: title.join(' | '),
description: `<p>${description.join('</p><p>')}</p><img src="${itemPicUrl}">`,
link: item.find('a').attr('href'),
};
})
.get(),
};
};