forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #836 from DIYgod/master
[pull] master from diygod:master
- Loading branch information
Showing
10 changed files
with
142 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const cheerio = require('cheerio'); | ||
const got = require('@/utils/got'); | ||
const { parseDate } = require('@/utils/parse-date'); | ||
const { art } = require('@/utils/render'); | ||
const path = require('path'); | ||
const { setCookies } = require('@/utils/puppeteer-utils'); | ||
const { CookieJar } = require('tough-cookie'); | ||
const logger = require('@/utils/logger'); | ||
|
||
module.exports = async (ctx) => { | ||
const baseUrl = 'https://www.pnas.org'; | ||
const { topicPath } = ctx.params; | ||
const link = `${baseUrl}/${topicPath ? topicPath : 'latest'}`; | ||
|
||
let cookieJar = await ctx.cache.get('pnas:cookieJar'); | ||
const cacheMiss = !cookieJar; | ||
cookieJar = cacheMiss ? new CookieJar() : CookieJar.fromJSON(cookieJar); | ||
const { data: res } = await got(link, { | ||
cookieJar, | ||
}); | ||
if (cacheMiss) { | ||
await ctx.cache.set('pnas:cookieJar', cookieJar.toJSON()); | ||
} | ||
|
||
const $ = cheerio.load(res); | ||
const list = $('.card--row-reversed .card-content') | ||
.toArray() | ||
.map((item) => { | ||
item = $(item); | ||
const a = item.find('.article-title a'); | ||
return { | ||
title: a.text(), | ||
link: new URL(a.attr('href'), baseUrl).href, | ||
pubDate: parseDate(item.find('.card__meta__date').text()), | ||
}; | ||
}); | ||
|
||
const browser = await require('@/utils/puppeteer')(); | ||
|
||
const out = await Promise.all( | ||
list.map((item) => | ||
ctx.cache.tryGet(item.link, async () => { | ||
const page = await browser.newPage(); | ||
await setCookies(page, await cookieJar.getCookieString(item.link), '.pnas.org'); | ||
await page.setRequestInterception(true); | ||
page.on('request', (request) => { | ||
request.resourceType() === 'document' ? request.continue() : request.abort(); | ||
}); | ||
logger.debug(`Requesting ${item.link}`); | ||
await page.goto(item.link, { | ||
waitUntil: 'domcontentloaded', | ||
referer: link, | ||
}); | ||
await page.waitForSelector('.core-container'); | ||
|
||
const res = await page.evaluate(() => document.documentElement.innerHTML); | ||
await page.close(); | ||
|
||
const $ = cheerio.load(res); | ||
const PNASdataLayer = JSON.parse( | ||
$('script') | ||
.text() | ||
.match(/PNASdataLayer =(.*?);/)[1] | ||
); | ||
|
||
$('.signup-alert-ad, .citations-truncation button').remove(); | ||
|
||
const { keywords, topic } = PNASdataLayer.page.attributes; | ||
|
||
item.category = [...keywords, topic]; | ||
item.author = PNASdataLayer.page.pageInfo.author; | ||
item.doi = PNASdataLayer.page.pageInfo.DOI; | ||
item.description = art(path.join(__dirname, 'templates', 'article.art'), { | ||
access: PNASdataLayer.user.access === 'yes', | ||
// | ||
abstracts: $('#abstracts .core-container').html(), | ||
// | ||
articleBody: $('[property=articleBody]').html(), | ||
// | ||
dataAvailability: $('#data-availability').html(), | ||
acknowledgments: $('#acknowledgments').html(), | ||
supplementaryMaterials: $('#supplementary-materials').html(), | ||
bibliography: $('#bibliography').html(), | ||
}); | ||
|
||
return item; | ||
}) | ||
) | ||
); | ||
|
||
browser.close(); | ||
|
||
ctx.state.data = { | ||
title: `${$('.banner-widget__content h1').text()} - PNAS`, | ||
description: $('.banner-widget__content p').text(), | ||
image: 'https://www.pnas.org/favicon.ico', | ||
language: 'en-US', | ||
link, | ||
item: out, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'/:topicPath*': ['emdoe', 'y9c'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
'pnas.org': { | ||
_name: 'Proceedings of the National Academy of Sciences', | ||
'.': [ | ||
{ | ||
title: '期刊', | ||
docs: 'https://docs.rsshub.app/journal.html#proceedings-of-the-national-academy-of-sciences', | ||
source: ['/*topicPath'], | ||
target: '/pnas/:topicPath', | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = (router) => { | ||
router.get('/:topicPath*', require('./index')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{{ if abstracts }}{{@ abstracts }}{{ /if }} | ||
|
||
{{ if access }}{{@ articleBody }}{{ /if }} | ||
|
||
{{ if dataAvailability }}{{@ dataAvailability }}{{ /if }} | ||
{{ if acknowledgments }}{{@ acknowledgments }}{{ /if }} | ||
{{ if supplementaryMaterials }}{{@ supplementaryMaterials }}{{ /if }} | ||
{{ if bibliography }}{{@ bibliography }}{{ /if }} |
0b30714
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
rsshub-master – ./
rsshub-master-auto-bot-ty.vercel.app
rsshub-master.vercel.app
rsshub-master-git-master-auto-bot-ty.vercel.app