Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add Cool Papers #14129

Merged
merged 4 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions lib/v2/papers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');

module.exports = async (ctx) => {
const { category = 'arxiv/cs.CL' } = ctx.params;
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30;

const rootUrl = 'https://papers.cool';
const currentUrl = new URL(category, rootUrl).href;
const apiKimiUrl = new URL(`${category.split(/\//)[0]}/kimi/`, rootUrl).href;

const { data: response } = await got(currentUrl);

const $ = cheerio.load(response);

const pubDate = parseDate(
$('p.info')
.first()
.text()
.match(/(\d+\s\w+\s\d{4})/)[1],
['DD MMM YYYY', 'D MMM YYYY']
);

let items = $('div.panel')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);

const doi = item.prop('id');
const enclosureUrl =
item
.find('a.pdf-preview')
.prop('onclick')
.match(/'(http.*?)'/)?.[1] ?? undefined;

return {
title: item.find('span[id]').first().text(),
link: item.find('a').first().prop('href'),
description: art(path.join(__dirname, 'templates/description.art'), {
summary: item.find('p.summary').text(),
}),
author: item
.find('p.authors a')
.toArray()
.map((a) => $(a).text())
.join('; '),
guid: `${currentUrl}#${doi}`,
pubDate,
enclosure_url: enclosureUrl,
enclosure_type: enclosureUrl ? 'application/pdf' : undefined,
doi,
Copy link
Collaborator

Choose a reason for hiding this comment

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

doi always starts with prefix 10.
The actual doi can be found from https://arxiv.org/abs/2312.16171

};
});

items = await Promise.all(
items.map(async (item) => {
const kimiURL = new URL(item.doi, apiKimiUrl).href;

const cache = await ctx.cache.get(kimiURL);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}

try {
const { data: detailResponse } = await got(kimiURL, {
timeout: {
request: 5000,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

This value is controlled by

timeout: config.requestTimeout,

It would be better to not overwrite it.

});

// Another cache with content by Kiwi Chat.

item.guid = `${item.guid}?kiwi`;
item.description += art(path.join(__dirname, 'templates/description.art'), {
kimi: detailResponse,
});

ctx.cache.set(kimiURL, JSON.stringify(item));
} catch (e) {
// no-empty
}

return Promise.resolve(item);
})
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think tryGet is fine here.

Suggested change
items = await Promise.all(
items.map(async (item) => {
const kimiURL = new URL(item.doi, apiKimiUrl).href;
const cache = await ctx.cache.get(kimiURL);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
try {
const { data: detailResponse } = await got(kimiURL, {
timeout: {
request: 5000,
},
});
// Another cache with content by Kiwi Chat.
item.guid = `${item.guid}?kiwi`;
item.description += art(path.join(__dirname, 'templates/description.art'), {
kimi: detailResponse,
});
ctx.cache.set(kimiURL, JSON.stringify(item));
} catch (e) {
// no-empty
}
return Promise.resolve(item);
})
);
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.guid, async () => {
const kimiURL = new URL(item.doi, apiKimiUrl).href;
try {
const { data: detailResponse } = await got(kimiURL);
// Another cache with content by Kiwi Chat.
item.description += art(path.join(__dirname, 'templates/description.art'), {
kimi: detailResponse,
});
} catch (e) {
// no-empty
}
return item;
})
)
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. Since the content from kimi chat is returned in a stream, I'll forgo fetching it and instead place a link to it in the description. 😊


const title = $('title').text();
const icon = new URL('favicon.ico', rootUrl).href;

ctx.state.data = {
item: items,
title: title.split(/-/)[0].trim(),
link: currentUrl,
description: title,
icon,
logo: icon,
subtitle: $('h1').first().text(),
};
};
3 changes: 3 additions & 0 deletions lib/v2/papers/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/:category?': ['nczitzk'],
};
17 changes: 17 additions & 0 deletions lib/v2/papers/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
'papers.cool': {
_name: 'Cool Papers',
'.': [
{
title: 'Category',
docs: 'https://docs.rsshub.app/routes/journal#cool-papers-category',
source: ['/:category*'],
target: (params) => {
const category = params.category;

return `/papers${category ? `/${category}` : ''}`;
},
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/papers/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/:category*', require('./'));
};
7 changes: 7 additions & 0 deletions lib/v2/papers/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ if summary }}
<p>{{ summary }}</p>
{{ /if }}

{{ if kimi }}
{{@ kimi }}
{{ /if }}
15 changes: 15 additions & 0 deletions website/docs/routes/journal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@
Including 'cell', 'cancer-cell', 'cell-chemical-biology', 'cell-host-microbe', 'cell-metabolism', 'cell-reports', 'cell-reports-physical-science', 'cell-stem-cell', 'cell-systems', 'chem', 'current-biology', 'developmental-cell', 'immunity', 'joule', 'matter', 'molecular-cell', 'neuron', 'one-earth' and 'structure'.
</Route>

## Cool Papers {#cool-papers}

### Category {#cool-papers-category}

<Route author="nczitzk" example="/papers" path="/papers/:category?" paramsDesc={['Category, see below, `arxiv/cs.CL` by default']} radar="1" supportBT="1" supportScihub="1">
| Category | id |
| ----------------------------------------------------- | ----------- |
| Arxiv Computation and Language (cs.CL) | arxiv/cs.CL |
| Arxiv Machine Learning (cs.LG) | arxiv/cs.CL |
| Arxiv Artificial Intelligence (cs.AI) | arxiv/cs.CL |
| Arxiv Information Retrieval (cs.IR) | arxiv/cs.CL |
| Arxiv Computer Vision and Pattern Recognition (cs.CV) | arxiv/cs.CL |
| Arxiv Machine Learning (stat.ML) | arxiv/cs.CL |
nczitzk marked this conversation as resolved.
Show resolved Hide resolved
</Route>

## Deloitte {#deloitte}

### Articles {#deloitte-articles}
Expand Down