feat: add X-mol 平台 (#3279)

This commit is contained in:
cssxsh
2019-10-18 11:57:52 +08:00
committed by DIYgod
parent 7f5acf57e3
commit 36b870543c
5 changed files with 164 additions and 0 deletions

View File

@@ -110,3 +110,13 @@ pageClass: routes
### 最新
<Route author="xyqfer" example="/icourse163/newest" path="/icourse163/newest" />
## X-MOL 平台
### 新闻
<Route author="cssxsh" example="/x-mol/news/3" path="/x-mol/news/:tag?" :paramsDesc="['数字编号可从新闻列表URL得到。为空时从新闻主页获取新闻。']" />
### 期刊
<Route author="cssxsh" example="/x-mol/paper/0/9" path="/x-mol/paper/:type/:magazine" :paramsDesc="['类别','机构两个参数都可从期刊URL获取。']" />

View File

@@ -1833,6 +1833,10 @@ router.get('/kzfeed/topic/:id', require('./routes/kzfeed/topic'));
// 腾讯新闻较真查证平台
router.get('/factcheck', require('./routes/tencent/factcheck'));
// X-MOL化学资讯平台
router.get('/x-mol/news/:tag?', require('./routes/x-mol/news.js'));
router.get('/x-mol/paper/:type/:magazine', require('./routes/x-mol/paper'));
// 電撃Online
router.get('/dengekionline/:type?', require('./routes/dengekionline/new'));

58
lib/routes/x-mol/news.js Normal file
View File

@@ -0,0 +1,58 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const utils = require('./utils');
module.exports = async (ctx) => {
const tag = ctx.params.tag;
const path = tag ? `/news/tag/${tag}` : '/news/index';
const response = await got(path, {
method: 'GET',
baseUrl: utils.host,
});
const data = response.data;
const $ = cheerio.load(data);
const title = $('title').text();
const description = $('meta[name="description"]').attr('content');
const newsitem = $('.newsitem');
const item = newsitem
.map((index, element) => {
const title = $(element)
.find('h3')
.find('a')
.text();
const a = $(element)
.find('p')
.find('a');
const link = utils.host + a.attr('href');
const image = $(element)
.find('img')
.attr('src');
const description = utils.setDesc(image, a.text());
const span = $(element).find('.space-right-m30');
const author = span
.text()
.replace('来源:', '')
.trim();
const date = utils.getDate(span.next().text());
const pubDate = utils.transDate(date);
const single = {
title: title,
link: link,
description: description,
author: author,
pubDate: pubDate,
};
return single;
})
.get();
ctx.state.data = {
title: title,
link: response.url,
description: description,
item: item,
};
};

76
lib/routes/x-mol/paper.js Normal file
View File

@@ -0,0 +1,76 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const utils = require('./utils');
module.exports = async (ctx) => {
const type = ctx.params.type;
const magazine = ctx.params.magazine;
const path = `/paper/${type}/${magazine}`;
const response = await got(path, {
method: 'GET',
baseUrl: utils.host,
headers: {
Cookie: 'closeFloatWindow=true; journalIndexViewType=list; journalSort=publishDate',
},
});
const data = response.data;
const $ = cheerio.load(data);
const title = $('title').text();
const description = $('meta[name="description"]').attr('content');
const newsitem = $('.magazine-text');
const item = await Promise.all(
newsitem
.map(async (index, element) => {
const news = $(element);
const a = news.find('.magazine-text-title').find('a');
const title = a.text();
const link = utils.host + a.attr('href');
const imageId = news
.find('.magazine-pic')
.attr('id')
.substring(9);
const getLink = utils.host + '/attachment/getImgUrl';
const noPic = utils.host + '/css/images/nothesispic.jpg';
const imageUrl = await ctx.cache.tryGet(getLink, async () => {
const result = await got.get(getLink, {
params: {
attachmentId: imageId,
},
});
return result.data;
});
const image = imageUrl || noPic;
const text = $(element)
.find('.magazine-description')
.text();
const description = utils.setDesc(image, text);
const span = news.find('.magazine-text-atten');
const arr = span.map((index, element) => $(element).text()).get();
const author = arr[1];
const date = utils.getDate(arr[0]);
const pubDate = utils.transDate(date);
const single = {
title: title,
link: link,
description: description,
author: author,
pubDate: pubDate,
};
return Promise.resolve(single);
})
.get()
);
ctx.state.data = {
title: title,
link: response.url,
description: description,
item: item,
};
};

16
lib/routes/x-mol/utils.js Normal file
View File

@@ -0,0 +1,16 @@
const XmolUtils = {
host: 'https://www.x-mol.com',
transDate: (date) => new Date(`${date} GMT+0800`).toUTCString(),
getDate: (text) => {
const reg = /[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/;
if (typeof text === 'string') {
const arr = text.match(reg);
return arr && text.match(reg)[0];
} else {
return null;
}
},
setDesc: (image, text) => `<p><img src="${image}"></p><p>${text}</p>`,
};
module.exports = XmolUtils;