fix(route): x-mol (#13428)

This commit is contained in:
Tony
2023-09-28 04:22:21 +08:00
committed by GitHub
parent 5897d2fd74
commit 13a65b9118
12 changed files with 183 additions and 147 deletions
+2 -2
View File
@@ -1269,8 +1269,8 @@ router.get('/wineyun/:category', lazyloadRouteHandler('./routes/wineyun'));
router.get('/kzfeed/topic/:id', lazyloadRouteHandler('./routes/kzfeed/topic'));
// X-MOL化学资讯平台
router.get('/x-mol/news/:tag?', lazyloadRouteHandler('./routes/x-mol/news.js'));
router.get('/x-mol/paper/:type/:magazine', lazyloadRouteHandler('./routes/x-mol/paper'));
// router.get('/x-mol/news/:tag?', lazyloadRouteHandler('./routes/x-mol/news.js'));
// router.get('/x-mol/paper/:type/:magazine', lazyloadRouteHandler('./routes/x-mol/paper'));
// 知识分子
router.get('/zhishifenzi/news/:type?', lazyloadRouteHandler('./routes/zhishifenzi/news'));
-48
View File
@@ -1,48 +0,0 @@
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',
prefixUrl: 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,
link,
description,
author,
pubDate,
};
return single;
})
.get();
ctx.state.data = {
title,
link: response.url,
description,
item,
};
};
-78
View File
@@ -1,78 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const utils = require('./utils');
const queryString = require('query-string');
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',
prefixUrl: 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 picId = news.find('.magazine-pic').attr('id');
const noPic = utils.host + '/css/images/nothesispic.jpg';
let imageUrl = noPic;
if (picId) {
const imageId = picId.substring(9);
const getLink = utils.host + '/attachment/getPaperImgUrl';
imageUrl =
(await ctx.cache.tryGet(getLink + imageId, async () => {
const result = await got.get(getLink, {
headers: { 'X-Requested-With': 'XMLHttpRequest' },
searchParams: queryString.stringify({
attachmentId: imageId,
}),
});
return result.data;
})) || noPic;
}
const image = imageUrl;
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,
link,
description,
author,
pubDate,
};
return Promise.resolve(single);
})
.get()
);
ctx.state.data = {
title,
link: response.url,
description,
item,
};
};
-16
View File
@@ -1,16 +0,0 @@
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;
+4
View File
@@ -0,0 +1,4 @@
module.exports = {
'/news/:tag?': ['cssxsh'],
'/paper/:type/:magazine': ['cssxsh'],
};
+63
View File
@@ -0,0 +1,63 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const utils = require('./utils');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const { art } = require('@/utils/render');
const { join } = require('path');
module.exports = async (ctx) => {
const tag = ctx.params.tag;
const path = tag ? `news/tag/${tag}` : 'news/index';
const link = new URL(path, utils.host).href;
const response = await got(link);
const data = response.data;
const $ = cheerio.load(data);
const newsitem = $('.newsitem')
.toArray()
.map((element) => {
element = $(element);
const a = element.find('h3 a');
const span = element.find('.space-right-m30');
const author = span.text().replace('来源:', '').trim();
return {
title: a.text(),
link: new URL(a.attr('href'), utils.host).href,
description: art(join(__dirname, 'templates/description.art'), {
image: element.find('img').attr('src').split('?')[0],
text: element.find('.thsis-div a').text().trim(),
}),
author,
pubDate: span.next().length ? timezone(parseDate(span.next().text().trim()), 8) : undefined,
};
});
const item = await Promise.all(
newsitem.map((item) =>
ctx.cache.tryGet(item.link, async () => {
if (item.link.includes('outLinkByIdAndCode')) {
return item;
}
const response = await got(item.link);
const $ = cheerio.load(response.data);
const description = $('.newscontent');
description.find('.detitemtit, .detposttiau').remove();
item.description = description.html();
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: response.url,
description: $('meta[name="description"]').attr('content'),
item,
};
};
+73
View File
@@ -0,0 +1,73 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const utils = require('./utils');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const asyncPool = require('tiny-async-pool');
module.exports = async (ctx) => {
const { type, magazine } = ctx.params;
const path = `paper/${type}/${magazine}`;
const link = new URL(path, utils.host).href;
const response = await got(link, {
headers: {
Cookie: 'journalIndexViewType=grid',
},
});
const data = response.data;
const $ = cheerio.load(data);
const newsItem = $('.magazine-model-content-new li')
.toArray()
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit, 10) : 20)
.map((item) => {
item = $(item);
return {
title: item.find('.magazine-text-title a').text().trim(),
link: new URL(item.find('.magazine-model-btn a').first().attr('href'), utils.host).href,
pubDate: timezone(
parseDate(
item
.find('.magazine-text-atten')
.text()
.match(/\d{4}-\d{2}-\d{2}/)[0],
8
)
),
};
});
const asyncPoolAll = async (...args) => {
const results = [];
for await (const result of asyncPool(...args)) {
results.push(result);
}
return results;
};
const item = await asyncPoolAll(2, newsItem, (element) =>
ctx.cache.tryGet(element.link, async () => {
const response = await got(element.link);
const $ = cheerio.load(response.data);
const description = $('.maga-content');
element.doi = description.find('.itsmblue').eq(1).text().trim();
description.find('.itgaryfirst').remove();
description.find('span').eq(0).remove();
element.author = description.find('span').eq(0).text().trim();
description.find('span').eq(0).remove();
element.description = description.html();
return element;
})
);
ctx.state.data = {
title: $('title').text(),
link: response.url,
description: $('meta[name="description"]').attr('content'),
item,
};
};
+25
View File
@@ -0,0 +1,25 @@
module.exports = {
'x-mol.com': {
_name: 'X-MOL',
'.': [
{
title: 'News',
docs: 'https://docs.rsshub.app/study#x-mol',
source: ['/news/:area/tag/:tag'],
target: '/x-mol/news/:tag',
},
{
title: 'News Index',
docs: 'https://docs.rsshub.app/study#x-mol',
source: ['/news/index'],
target: '/x-mol/news',
},
{
title: 'Journal',
docs: 'https://docs.rsshub.app/journal#x-mol',
source: ['/paper/:area/tag/:type/journal/:magazine'],
target: '/x-mol/paper/geo/:type/:magazine',
},
],
},
};
+4
View File
@@ -0,0 +1,4 @@
module.exports = (router) => {
router.get('/news/:tag?', require('./news'));
router.get('/paper/:type/:magazine', require('./paper'));
};
+6
View File
@@ -0,0 +1,6 @@
{{ if image }}
<img src="{{ image }}"><br>
{{ /if }}
{{ if text }}
<p>{{ text }}</p>
{{ /if }}
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
host: 'https://www.x-mol.com',
};
+3 -3
View File
@@ -553,11 +553,11 @@ Return results from 2020
</Route>
## X-MOL Platform {#x-mol-platform}
## X-MOL {#x-mol}
### Journal {#x-mol-platform-journal}
### Journal {#x-mol-journal}
<Route author="cssxsh" example="/x-mol/paper/0/9" path="/x-mol/paper/:type/:magazine" paramsDesc={['type','magazine']} />
<Route author="cssxsh" example="/x-mol/paper/0/9" path="/x-mol/paper/:type/:magazine" paramsDesc={['type','magazine']} anticrawler="1"/>
## 管理世界 {#guan-li-shi-jie}