Skip to content

Commit

Permalink
Merge pull request #836 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored Feb 22, 2023
2 parents 526fbc0 + d986c15 commit 0b30714
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 82 deletions.
15 changes: 6 additions & 9 deletions docs/en/journal.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,21 +331,18 @@ Return results from 2020

<RouteEn author="Fatpandac" example="/oup/journals/adaptation" path="/oup/journals/:name" :paramsDesc="['short name for a journal, can be found in URL']" anticrawler="1"/>

## Proceedings of The National Academy of Sciences (PNAS)
## Proceedings of The National Academy of Sciences

### Latest Articles - Articles by Topic

### Proceedings of The National Academy of Sciences (PNAS) - Latest Articles
### Journal

<RouteEn author="emdoe" example="/pnas/Applied Mathematics" path="/pnas/:topic" :paramsDesc="['topic name', 'obtain it from pnas.org (new research in ...)']" />
<RouteEn author="emdoe HenryQW y9c" example="/pnas/latest" path="/pnas/:topicPath*" :paramsDesc="['Topic path, support **Featured Topics**, **Articles By Topic** and [**Collected Papers**](https://www.pnas.org/about/collected-papers), `latest` by default']" radar="1" anticrawler="1" puppeteer="1" supportScihub="1">

- Using router (`/pnas/` + Topic of Interest) to query latest research paper for a certain topic from PNAS journal.
If the `:topic` parameter is blank, or equal to 'latest', then all the latest papers will return.
::: tip Tips
Some topics require adding `topic/` to `topicPath` like [`/pnas/topic/app-math`](https://rsshub.app/pnas/topic/app-math) and some don't like [`/pnas/biophysics-and-computational-biology`](https://rsshub.app/pnas/biophysics-and-computational-biology)
:::

</RouteEn>

<RouteEn author="emdoe HenryQW" example="/pnas/Applied Mathematics" path="/pnas/:topic" :paramsDesc="['topic name', 'obtain it from pnas.org (new research in ...). `Latest` will include all.']" supportScihub="1"/>

## PubMed

### Trending articles
Expand Down
11 changes: 6 additions & 5 deletions docs/journal.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,15 @@ You can get all short name of a journal from <https://www.nature.com/siteindex>

<Route author="Fatpandac" example="/oup/journals/adaptation" path="/oup/journals/:name" :paramsDesc="['期刊名称缩写,可以在网址中得到']" anticrawler="1"/>

## PNAS
## Proceedings of The National Academy of Sciences

### 最新文章(可筛选领域)
### 期刊

<Route author="emdoe y9c" example="/pnas/Applied Mathematics" path="/pnas/:topic" :paramsDesc="['领域名称','可从 pnas.org 获得']" supportScihub="1"/>
<Route author="emdoe HenryQW y9c" example="/pnas/latest" path="/pnas/:topicPath*" :paramsDesc="['领域路径,支持 **Featured Topics**、**Articles By Topic** 及 [**Collected Papers**](https://www.pnas.org/about/collected-papers), 预设为 `latest`']" radar="1" anticrawler="1" puppeteer="1" supportScihub="1">

- 通过 `/pnas/` + “领域名称” 来获取对应 “领域” 的最新文章(Latest Research)。
若参数置空(`/pnas`)或为 latest(`/pnas/latest`),则默认获取全部文章。
::: tip Tips
有些领域需要在 `topicPath` 中添加 `topic/`,如 [`/pnas/topic/app-math`](https://rsshub.app/pnas/topic/app-math),有些则不需要,如 [`/pnas/biophysics-and-computational-biology`](https://rsshub.app/pnas/biophysics-and-computational-biology)
:::

</Route>

Expand Down
2 changes: 1 addition & 1 deletion docs/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ type 为 all 时,category 参数不支持 cost 和 free

### 今日油价查询

<Route author="TonyRL" example="/qiyoujiage/shanghai" path="/qiyoujiage/:path*" :paramsDesc="['路径']">
<Route author="TonyRL" example="/qiyoujiage/shanghai" path="/qiyoujiage/:path+" :paramsDesc="['路径']">

::: tip 提示

Expand Down
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ router.get('/elife/:tid', lazyloadRouteHandler('./routes/elife/index'));
router.get('/ieee/author/:aid/:sortType/:count?', lazyloadRouteHandler('./routes/ieee/author'));

// PNAS [Sci Journal]
router.get('/pnas/:topic?', lazyloadRouteHandler('./routes/pnas/index'));
// router.get('/pnas/:topic?', lazyloadRouteHandler('./routes/pnas/index'));

// cell [Sci Journal]
router.get('/cell/cell/:category', lazyloadRouteHandler('./routes/cell/cell/index'));
Expand Down
66 changes: 0 additions & 66 deletions lib/routes/pnas/index.js

This file was deleted.

101 changes: 101 additions & 0 deletions lib/v2/pnas/index.js
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,
};
};
3 changes: 3 additions & 0 deletions lib/v2/pnas/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/:topicPath*': ['emdoe', 'y9c'],
};
13 changes: 13 additions & 0 deletions lib/v2/pnas/radar.js
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',
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/pnas/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/:topicPath*', require('./index'));
};
8 changes: 8 additions & 0 deletions lib/v2/pnas/templates/article.art
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 }}

1 comment on commit 0b30714

@vercel
Copy link

@vercel vercel bot commented on 0b30714 Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.