-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.mjs
More file actions
84 lines (72 loc) · 2.65 KB
/
main.mjs
File metadata and controls
84 lines (72 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import fs from 'fs';
async function fetchAllPages(query, maxpages, searchYear, affiliation) {
let fullQuery = `${query} AND PUB_YEAR:${searchYear}`;
if (affiliation) fullQuery += ` AND AFF:${affiliation}`;
try{
const params = new URLSearchParams({
query: fullQuery,
format: 'json',
pageSize: 25,
resultType: 'core'
});
let url = `https://www.ebi.ac.uk/europepmc/webservices/rest/search?${params}`;
const allItems = [];
let pages = 0;
while (url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
allItems.push(...data.resultList.result.map(filterResults));
pages++;
if(maxpages) {
if(pages >= maxpages) break;
}
url = data.nextPageUrl;
}
return allItems;
} catch (error){
console.error('Failed:' , error.name);
console.error(error.message);
return [];
}
}
function filterResults(item, url){
const link = getLink(item, url);
return {
title: item.title,
authors: item.authorString,
journal: item.journalTitle,
hasFullText: item.inPMC === 'Y' || item.hasPDF === 'Y',
link: link?? "not found",
//abstract: item.abstractText //uncomment if you want abstract in result
};
}
function getLink(item, url){
if(item.pmcid)
return `https://europepmc.org/article/PMC/${item.pmcid}`;
const urls = item.fullTextUrlList?.fullTextUrl;
if(url?.length)
return urls[0].url;
if(item.doi)
return `https://doi.org/${item.doi}`;
return item.id? `https://europepmc.org/article/MED/${item.id}` : null;
}
function toCSV(items){
if(items.length === 0) return '';
const headers = Object.keys(items[0]);
const rows = items.map(item =>
headers.map(h => {
const val = String(item[h] ?? '');
return val.includes(',') || val.includes('"') || val.includes('\n') ?
`"${val.replace(/"/g, '""')}"` : val;
}).join(',')
);
return [headers.join(','), ...rows].join('\n');
}
async function main() {
const result = await fetchAllPages('example', null, null, null); //change parameters to desired
fs.writeFileSync('results.json', JSON.stringify(result, null, 2)); //json results w json formattin
//fs.writeFileSync('results.csv', toCSV(result)); //all in csv file flattened
//console.log(`Tot articles: ${result.length}`); //for n. of total articles
}
main();