Skip to content

Commit

Permalink
fix(route): IEEE journal routes (DIYgod#15720)
Browse files Browse the repository at this point in the history
* fix: IEEE journal routes

* feat: add feed image

* chore: preprint -> earlyAccess

* chore: remove cookie jar
  • Loading branch information
HenryQW committed May 30, 2024
1 parent b72d35f commit db0ae9d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 275 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

0 comments on commit db0ae9d

Please sign in to comment.