Skip to content

Instantly share code, notes, and snippets.

@tgs
Forked from hubgit/index.html
Last active November 13, 2017 21:27
Show Gist options
  • Save tgs/7d583e05310a3c951718d9bc0ce3af85 to your computer and use it in GitHub Desktop.
Save tgs/7d583e05310a3c951718d9bc0ce3af85 to your computer and use it in GitHub Desktop.
PubMed search with jQuery
<!doctype html>
<meta charset="utf-8">
<title>PubMed Search</title>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<ul id="output"></ul>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="pubmed-search.js"></script>
searchPubMed('FOXP3 AND 2013/01:2014/01[Publication Date]')
.then(fetchResults)
.then(parseResults)
.then(displayResults);
function searchPubMed(term) {
return $.ajax({
url: 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi',
data: {
db: 'pubmed',
usehistory: 'y',
term: term,
retmode: 'json',
retmax: 0
}
});
}
function fetchResults(response) {
return $.ajax({
url: 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi',
data: {
db: 'pubmed',
usehistory: 'y',
webenv: response.esearchresult.webenv,
query_key: response.esearchresult.querykey,
retmode: 'xml',
retmax: 10 // how many items to return
}
});
}
function parseResults(response) {
var nodes = response.querySelectorAll('DocSum');
return $.map(nodes, function(node) {
var pmidNode = node.querySelector('Id');
var doiNode = node.querySelector('Item[Name=DOI]');
var titleNode = node.querySelector('Item[Name=Title]');
var sourceNode = node.querySelector('Item[Name=Source]');
var epubDateNode = node.querySelector('Item[Name=EPubDate]');
var pubDateNode = node.querySelector('Item[Name=PubDate]');
var authorNodes = node.querySelectorAll('Item[Name=AuthorList] > Item[Name=Author]');
return {
title: titleNode ? titleNode.textContent : null,
source: sourceNode ? sourceNode.textContent : null,
authors: $.map(authorNodes, function(authorNode) {
return authorNode.textContent;
}),
url: doiNode ? 'https://dx.doi.org/' + encodeURIComponent(doiNode.textContent) : 'http://pubmed.gov/' + pmidNode.textContent,
date: epubDateNode && epubDateNode.textContent ? epubDateNode.textContent : pubDateNode.textContent,
};
});
}
function displayResults(articles) {
var output = $('#output');
$.each(articles, function (i, article) {
var item = $('<li/>').appendTo(output);
var container = $('<div/>').appendTo(item);
$('<a/>', {
href: article.url,
text: article.title
}).appendTo(container);
$('<div/>', {
text: article.authors.join(', ')
}).appendTo(item);
$('<div/>', {
text: article.date + ' · ' + article.source
}).appendTo(item);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment