Skip to content

Commit

Permalink
NASA ADS update (#1561)
Browse files Browse the repository at this point in the history
  • Loading branch information
zuphilip authored and adam3smith committed Feb 27, 2018
1 parent 5d0e02a commit 1c91c9b
Showing 1 changed file with 113 additions and 67 deletions.
180 changes: 113 additions & 67 deletions NASA ADS.js
@@ -1,84 +1,131 @@
{
"translatorID": "7987b420-e8cb-4bea-8ef7-61c2377cd686",
"label": "NASA ADS",
"creator": "Asa Kusuma and Ramesh Srigiriraju",
"creator": "Philipp Zumstein, Asa Kusuma and Ramesh Srigiriraju",
"target": "^https?://(ukads|cdsads|ads|adsabs|esoads|adswww|www\\.ads)\\.(inasan|iucaa\\.ernet|nottingham\\.ac|harvard|eso|u-strasbg|nao\\.ac|astro\\.puc|bao\\.ac|on|kasi\\.re|grangenet|lipi\\.go|mao\\.kiev)\\.(edu|org|net|fr|jp|cl|id|uk|cn|ua|in|ru|br|kr)/(cgi-bin|abs)/",
"minVersion": "1.0.0b4.r1",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2013-02-07 23:22:18"
"lastUpdated": "2018-02-17 17:39:33"
}

function detectWeb(doc, url) {
var singXpath = '//input[@name="bibcode"][@type="hidden"]';
var multXpath = '//input[@name="bibcode"][@type="checkbox"]';
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2018 Philipp Zumstein
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
if (doc.evaluate(multXpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
***** END LICENSE BLOCK *****
*/


// attr()/text() v2
function attr(docOrElem,selector,attr,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.getAttribute(attr):null;}function text(docOrElem,selector,index){var elem=index?docOrElem.querySelectorAll(selector).item(index):docOrElem.querySelector(selector);return elem?elem.textContent:null;}


function detectWeb(doc, url) {
if (attr(doc, 'input[name="bibcode"][type="checkbox"]', 'value')) {
return "multiple";
} else if (doc.evaluate(singXpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext()){
} else if (attr(doc, 'input[name="bibcode"][type="hidden"]', 'value')){
return "journalArticle";
}
}

function parseRIS(bibcodes, hostname){
var getURL = "http://" + hostname + "/cgi-bin/nph-bib_query?"
+ bibcodes + "data_type=REFMAN&nocookieset=1";
var pdfURL = "http://" + hostname + "/cgi-bin/nph-data_query?"
+ bibcodes + "link_type=ARTICLE";
//Z.debug(getURL)
//Z.debug(pdfURL)
Zotero.Utilities.HTTP.doGet(getURL, function(text){
function scrape(bibcodes, url, doc) {
// We can retrieve one RIS file containing the bibliographic
// information of multiples entries specified by their
// bibcode.
var hostname = url.replace(/\/(cgi-bin|abs)\/.*/, '');
var getURL = hostname + '/cgi-bin/nph-bib_query?'+bibcodes.join('&')+'&data_type=REFMAN&nocookieset=1';
// Some articles have a full text link to the PDF, which
// we can find by going through all links in <a>-tags. This
// works for the detail page as well as for the mulitple page.
var atags = doc.querySelectorAll('a');
Zotero.Utilities.HTTP.doGet(getURL, function(text){
// load translator for RIS
var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
translator.setString(text);
translator.setHandler("itemDone", function(obj, item) {
item.attachments = [{url:pdfURL, title: "NASA/ADS Full Text PDF", mimeType: "application/pdf"}];
if (item.url && item.url.includes('/abs/')) {
let bibcode = item.url.split('/abs/')[1];
let pdfUrl;
// Go through all a tags and look for one including the
// corresponding bibcode and the link_type for full-text PDF.
for (let i=0; i<atags.length; i++) {
let atagUrl = atags[i].href;
if (atagUrl.includes('bibcode='+bibcode) && atagUrl.includes('link_type=ARTICLE')) {
pdfUrl = atagUrl;
}
}
if (pdfUrl) {
//Z.debug(pdfUrl);
item.attachments = [{
url: pdfUrl,
title: "Full Text PDF",
mimeType: "application/pdf"
}];
}
}
item.complete();
});
translator.translate();
}, function() {});
});
}

function doWeb(doc, url) {

var singXpath = '//input[@name="bibcode"][@type="hidden"]';
var multXpath = '//input[@name="bibcode"][@type="checkbox"]';
var titleXpath = '//table/tbody/tr/td[4]'; //will find scores and titles
var hostname = doc.location.host
var bibElmts = doc.evaluate(multXpath, doc, null, XPathResult.ANY_TYPE, null);
var titleElmts = doc.evaluate(titleXpath, doc, null, XPathResult.ANY_TYPE, null);
var titleElmt;
var bibElmt;

if ((bibElmt = bibElmts.iterateNext()) && (titleElmt = titleElmts.iterateNext())) {
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('input[name="bibcode"][type="checkbox"]');
for (var i=0; i<rows.length; i++) {
var value = rows[i].value;
var title = value;
if (!value || !title) continue;
if (checkOnly) return true;
found = true;
items[value] = title;
}
return found ? items : false;
}

var items = new Array();

do {
titleElmt = titleElmts.iterateNext(); //iterate a second time to avoid score
items[bibElmt.value] = Zotero.Utilities.trimInternal(titleElmt.textContent);
} while((bibElmt = bibElmts.iterateNext()) && (titleElmt = titleElmts.iterateNext()));

Zotero.selectItems(items, function (items) {
if (!items) {
return true;
}
var bibcodes="";
for(var bibcode in items) {
bibcodes = bibcodes + "bibcode="+encodeURIComponent(bibcode) + "&";
}
parseRIS(bibcodes, hostname);
});
} else if (bibElmt = doc.evaluate(singXpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext()){
var bibcode = bibElmt.value;
var bibcodes = "bibcode="+encodeURIComponent(bibcode) + "&";
parseRIS(bibcodes, hostname);
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (!items) {
return true;
}
var bibcodes = [];
for (var i in items) {
bibcodes.push('bibcode='+i);
}
scrape(bibcodes, url, doc);
});
} else {
var bibcode = attr(doc, 'input[name="bibcode"][type="hidden"]', 'value');
scrape(['bibcode='+bibcode], url, doc);
}
}/** BEGIN TEST CASES **/
}

/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
Expand All @@ -91,36 +138,35 @@ var testCases = [
"items": [
{
"itemType": "journalArticle",
"title": "The Luminosity Function and Stellar Evolution.",
"creators": [
{
"lastName": "Salpeter",
"firstName": "Edwin E.",
"creatorType": "author"
}
],
"notes": [],
"tags": [],
"seeAlso": [],
"date": "January 1, 1955",
"DOI": "10.1086/145971",
"ISSN": "0004-637X",
"abstractNote": "Abstract image available at: \nhttp://adsabs.harvard.edu/abs/1955ApJ...121..161S",
"journalAbbreviation": "The Astrophysical Journal",
"libraryCatalog": "NASA ADS",
"pages": "161",
"publicationTitle": "The Astrophysical Journal",
"url": "http://adsabs.harvard.edu/abs/1955ApJ...121..161S",
"volume": "121",
"attachments": [
{
"title": "NASA/ADS Full Text PDF",
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"title": "The Luminosity Function and Stellar Evolution.",
"journalAbbreviation": "The Astrophysical Journal",
"volume": "121",
"date": "January 1, 1955",
"pages": "161",
"url": "http://adsabs.harvard.edu/abs/1955ApJ...121..161S",
"abstractNote": "Abstract image available at: \r\nhttp://adsabs.harvard.edu/abs/1955ApJ...121..161S",
"DOI": "10.1086/145971",
"ISSN": "0004-637X",
"publicationTitle": "The Astrophysical Journal",
"libraryCatalog": "NASA ADS",
"accessDate": "CURRENT_TIMESTAMP"
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/
/** END TEST CASES **/

0 comments on commit 1c91c9b

Please sign in to comment.