Skip to content

Commit

Permalink
Merge pull request #4 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 May 31, 2024
2 parents 0204aab + 1f619e4 commit 3d79db1
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 386 deletions.
100 changes: 0 additions & 100 deletions lib/routes/ieee/earlyaccess.ts

This file was deleted.

117 changes: 50 additions & 67 deletions lib/routes/ieee/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,72 @@ import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);

import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import path from 'node:path';
import { art } from '@/utils/render';

import { CookieJar } from 'tough-cookie';
const cookieJar = new CookieJar();
const ieeeHost = 'https://ieeexplore.ieee.org';

export const route: Route = {
path: ['/:journal/latest/vol/:sortType?', '/journal/:journal/:sortType?'],
name: 'Unknown',
maintainers: [],
name: 'IEEE Journal Articles',
maintainers: ['HenryQW'],
categories: ['journal'],
path: '/journal/:punumber/:earlyAccess?',
parameters: {
punumber: 'Publication Number, look for `punumber` in the URL',
earlyAccess: 'Optional, set any value to get early access articles',
},
example: '/ieee/journal/6287639/preprint',
handler,
};

async function handler(ctx) {
const punumber = ctx.req.param('journal');
const sortType = ctx.req.param('sortType') ?? 'vol-only-seq';
const host = 'https://ieeexplore.ieee.org';
const jrnlUrl = `${host}/xpl/mostRecentIssue.jsp?punumber=${punumber}`;
const publicationNumber = ctx.req.param('punumber');
const earlyAccess = !!ctx.req.param('earlyAccess');

const response = await got(`${host}/rest/publication/home/metadata?pubid=${punumber}`, {
cookieJar,
}).json();
const volume = response.currentIssue.volume;
const isnumber = response.currentIssue.issueNumber;
const jrnlName = response.displayTitle;
const metadata = await fetchMetadata(publicationNumber);
const { displayTitle, currentIssue, preprintIssue, coverImagePath } = metadata;
const { issueNumber, volume } = earlyAccess ? preprintIssue : currentIssue;

const response2 = await got
.post(`${host}/rest/search/pub/${punumber}/issue/${isnumber}/toc`, {
cookieJar,
json: {
punumber,
isnumber,
sortType,
rowsPerPage: '100',
},
})
.json();
let list = response2.records.map((item) => {
const $2 = load(item.articleTitle);
const title = $2.text();
const link = item.htmlLink;
const doi = item.doi;
let authors = 'Do not have author';
if (Object.hasOwn(item, 'authors')) {
authors = item.authors.map((itemAuth) => itemAuth.preferredName).join('; ');
}
let abstract = '';
Object.hasOwn(item, 'abstract') ? (abstract = item.abstract) : (abstract = '');
return {
title,
link,
authors,
doi,
volume,
abstract,
};
});
const tocData = await fetchTOCData(publicationNumber, issueNumber);
const list = tocData.records.map((item) => {
const mappedItem = mapRecordToItem(volume)(item);

const renderDesc = (item) =>
art(path.join(__dirname, 'templates/description.art'), {
item,
mappedItem.description = art(path.join(__dirname, 'templates/description.art'), {
item: mappedItem,
});
list = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
if (item.abstract !== '') {
const response3 = await got(`${host}${item.link}`);
const { abstract } = JSON.parse(response3.body.match(/metadata=(.*);/)[1]);
const $3 = load(abstract);
item.abstract = $3.text();
item.description = renderDesc(item);
}
return item;
})
)
);

return mappedItem;
});

return {
title: jrnlName,
link: jrnlUrl,
title: displayTitle,
link: `${ieeeHost}/xpl/tocresult.jsp?isnumber=${issueNumber}`,
item: list,
image: `${ieeeHost}${coverImagePath}`,
};
}

async function fetchMetadata(punumber) {
const response = await got(`${ieeeHost}/rest/publication/home/metadata?pubid=${punumber}`);
return response.data;
}

async function fetchTOCData(punumber, isnumber) {
const response = await got.post(`${ieeeHost}/rest/search/pub/${punumber}/issue/${isnumber}/toc`, {
json: { punumber, isnumber, rowsPerPage: '100' },
});
return response.data;
}

function mapRecordToItem(volume) {
return (item) => ({
abstract: item.abstract || '',
authors: item.authors ? item.authors.map((author) => author.preferredName).join('; ') : '',
description: '',
doi: item.doi,
link: item.htmlLink,
title: item.articleTitle || '',
volume,
});
}
107 changes: 0 additions & 107 deletions lib/routes/ieee/recent.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/routes/ieee/templates/description.art
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</p>
<p>
<span><small><i>{{ item.authors }}</i></small></span><br>
<span><small><i>https://doi.org/{{ item.doi }}</i></small></span><br>
<span><small><i><a href="https://doi.org/{{ item.doi }}">https://doi.org/{{ item.doi }}</a></i></small></span><br>
<span><small><i>Volume {{ item.volume }}</i></small></span><br>
</p>
<p>
Expand Down
6 changes: 4 additions & 2 deletions lib/routes/oschina/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import got from '@/utils/got';
import { parseDate, parseRelativeDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { load } from 'cheerio';
import { fetchArticle } from '@/utils/wechat-mp';

const configs = {
all: {
Expand Down Expand Up @@ -111,8 +112,7 @@ async function handler(ctx) {

item.description = content('.article-detail').html();
item.author = content('.article-box__meta .item').first().text();
}
if (/^https?:\/\/gitee.com\/.*$/.test(item.link)) {
} else if (/^https?:\/\/gitee\.com\/.*$/.test(item.link)) {
const detail = await got(item.link, {
headers: {
Referer: config.link,
Expand All @@ -121,6 +121,8 @@ async function handler(ctx) {
const content = load(detail.data);

item.description = content('.file_content').html();
} else if (/^https?:\/\/osc\.cool\/.*$/.test(item.link)) {
return fetchArticle(item.link, true);
}
return item;
})
Expand Down
Loading

0 comments on commit 3d79db1

Please sign in to comment.