diff --git a/lib/router.js b/lib/router.js index 2b5bf36a2..82924afe5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/x-mol/news.js b/lib/routes/x-mol/news.js deleted file mode 100644 index 519cb4271..000000000 --- a/lib/routes/x-mol/news.js +++ /dev/null @@ -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, - }; -}; diff --git a/lib/routes/x-mol/paper.js b/lib/routes/x-mol/paper.js deleted file mode 100644 index 86a919d86..000000000 --- a/lib/routes/x-mol/paper.js +++ /dev/null @@ -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, - }; -}; diff --git a/lib/routes/x-mol/utils.js b/lib/routes/x-mol/utils.js deleted file mode 100644 index dbe61a473..000000000 --- a/lib/routes/x-mol/utils.js +++ /dev/null @@ -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) => `
${text}
`, -}; - -module.exports = XmolUtils; diff --git a/lib/v2/x-mol/maintainer.js b/lib/v2/x-mol/maintainer.js new file mode 100644 index 000000000..faef32780 --- /dev/null +++ b/lib/v2/x-mol/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/news/:tag?': ['cssxsh'], + '/paper/:type/:magazine': ['cssxsh'], +}; diff --git a/lib/v2/x-mol/news.js b/lib/v2/x-mol/news.js new file mode 100644 index 000000000..d7d5fcfa8 --- /dev/null +++ b/lib/v2/x-mol/news.js @@ -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, + }; +}; diff --git a/lib/v2/x-mol/paper.js b/lib/v2/x-mol/paper.js new file mode 100644 index 000000000..7e9701b36 --- /dev/null +++ b/lib/v2/x-mol/paper.js @@ -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, + }; +}; diff --git a/lib/v2/x-mol/radar.js b/lib/v2/x-mol/radar.js new file mode 100644 index 000000000..0e6d7af16 --- /dev/null +++ b/lib/v2/x-mol/radar.js @@ -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', + }, + ], + }, +}; diff --git a/lib/v2/x-mol/router.js b/lib/v2/x-mol/router.js new file mode 100644 index 000000000..5d5588faa --- /dev/null +++ b/lib/v2/x-mol/router.js @@ -0,0 +1,4 @@ +module.exports = (router) => { + router.get('/news/:tag?', require('./news')); + router.get('/paper/:type/:magazine', require('./paper')); +}; diff --git a/lib/v2/x-mol/templates/description.art b/lib/v2/x-mol/templates/description.art new file mode 100644 index 000000000..e18d50115 --- /dev/null +++ b/lib/v2/x-mol/templates/description.art @@ -0,0 +1,6 @@ +{{ if image }} +{{ text }}
+{{ /if }} diff --git a/lib/v2/x-mol/utils.js b/lib/v2/x-mol/utils.js new file mode 100644 index 000000000..4455bd56c --- /dev/null +++ b/lib/v2/x-mol/utils.js @@ -0,0 +1,3 @@ +module.exports = { + host: 'https://www.x-mol.com', +}; diff --git a/website/docs/routes/journal.md b/website/docs/routes/journal.md index 62cfba551..1fbbd40bc 100644 --- a/website/docs/routes/journal.md +++ b/website/docs/routes/journal.md @@ -553,11 +553,11 @@ Return results from 2020 -## X-MOL Platform {#x-mol-platform} +## X-MOL {#x-mol} -### Journal {#x-mol-platform-journal} +### Journal {#x-mol-journal} -