-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscrapePubmed.js
37 lines (30 loc) · 1.11 KB
/
scrapePubmed.js
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
const pubmedAPIlink = "https://pubmed-api-q1u2.onrender.com";
async function scrapePubmed(pubMedPaperLink) {
pubMedPaperLink = modifyNcbiLink(pubMedPaperLink);
console.log(`In scrapePubmed, fetch sent to = ${pubmedAPIlink}?link=${pubMedPaperLink}`)
return fetch(`${pubmedAPIlink}?link=${pubMedPaperLink}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(err => {
console.error('Error:', err);
return {};
});
}
function modifyNcbiLink(originalLink) {
let url = new URL(originalLink);
// Check if the hostname includes "ncbi.nlm.nih.gov"
if (url.hostname.includes("ncbi.nlm.nih.gov")) {
let list_uids = url.searchParams.get("list_uids");
// Ensure list_uids param exists and is a valid number
if (list_uids && !isNaN(list_uids)) {
// Construct the new link
return `https://www.pubmed.ncbi.nlm.nih.gov/${list_uids}`;
}
}
// If conditions aren't met, return the original link
return originalLink;
}