Skip to content
Permalink
Browse files

Rewrite MIT Press Journals.js

Closes #1436.
  • Loading branch information...
zuphilip authored and adam3smith committed Nov 13, 2017
1 parent 4ae2600 commit edebd2ec469db27d0b1872d5a0db362b28716c15
Showing with 103 additions and 61 deletions.
  1. +103 −61 MIT Press Journals.js
@@ -1,79 +1,123 @@
{
"translatorID": "2e43f4a9-d2e2-4112-a6ef-b3528b39b4d2",
"label": "MIT Press Journals",
"creator": "Michael Berkowitz",
"creator": "Philipp Zumstein",
"target": "^https?://www\\.mitpressjournals\\.org/(action|toc|doi)/",
"minVersion": "1.0.0b4.r5",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2014-06-02 00:32:10"
"lastUpdated": "2017-11-12 11:41:41"
}

/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2017 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/>.
***** 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 (url.match(/action\/doSearch/) || url.match(/toc\//)) {
if (url.includes('action/doSearch') || url.includes('toc/')) {
return "multiple";
} else if (url.match(/doi\/abs\//)) {
} else if (url.includes('doi/abs/') || url.includes('doi/full/')) {
return "journalArticle";
}
}


function getDOI(str) {
return str.match(/doi\/abs\/([^?]+)/)[1];
return str.match(/doi\/(abs|full)\/([^?]+)/)[2];
}



function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('.art_title>a.ref');
for (var i=0; i<rows.length; i++) {
var href = rows[i].href;
var title = ZU.trimInternal(rows[i].textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}


function doWeb(doc, url) {
var articles = new Array();
if (detectWeb(doc, url) == "multiple") {
var items = new Object();
var links = doc.evaluate('//table[@class="articleEntry"]/tbody/tr//a[text() = "First Page" or text() = "Citation" or text() = "Abstract"]', doc, null, XPathResult.ANY_TYPE, null);
var titles = doc.evaluate('//table[@class="articleEntry"]/tbody/tr//div[@class="arttitle"]', doc, null, XPathResult.ANY_TYPE, null);
var link, title;
while ((link = links.iterateNext()) && (title = titles.iterateNext())) {
items[link.href] = Zotero.Utilities.trimInternal(title.textContent);
}

Zotero.selectItems(items, function (items) {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (!items) {
return true;
}
urls = new Array();
var articles = [];
for (var i in items) {
articles.push('http://www.mitpressjournals.org/doi/abs/' + getDOI(i));
articles.push(i);
}
ZU.processDocuments(articles, scrape)
ZU.processDocuments(articles, scrape);
});
} else {
scrape(doc, 'http://www.mitpressjournals.org/doi/abs/' + getDOI(url));
scrape(doc, url);
}
}


function scrape(doc, url){
if (doc.evaluate('//div[@class="abstractSection"]/p[contains(@class, "last") or contains(@class, "first")]', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
var abs = Zotero.Utilities.trimInternal(doc.evaluate('//div[@class="abstractSection"]/p[contains(@class, "last") or contains(@class, "first")]', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent);
}
var doi = getDOI(doc.location.href);
var risurl = 'http://www.mitpressjournals.org/action/downloadCitation?doi=' + doi + '&include=cit&format=refman&direct=on&submit=Download+article+metadata';
var pdfurl = doc.location.href.replace("/doi/abs/", "/doi/pdf/");
Zotero.Utilities.HTTP.doGet(risurl, function(text) {
var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
//Zotero.debug(text)
translator.setString(text);
translator.setHandler("itemDone", function(obj, item) {
//picks up some weird attachments and notes from the RIS - delete
item.attachments= [];
item.notes=[];
item.attachments.push({url:doc.location.href, title:item.publicationTitle + " Snapshot", mimeType:"text/html"})
item.attachments.push({url:pdfurl, title:item.publicationTitle + " Full Text PDF", mimeType:"application/pdf"});
if (abs) item.abstractNote = abs;
item.complete();
var abs = text(doc, '.abstractSection');
var doi = getDOI(url);
var risurl = 'http://www.mitpressjournals.org/action/downloadCitation?doi=' + doi + '&include=cit&format=refman&direct=on&submit=Download+article+metadata';
var pdfurl = url.replace("/doi/abs/", "/doi/pdf/");
Zotero.Utilities.HTTP.doGet(risurl, function(text) {
var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
//Zotero.debug(text)
translator.setString(text);
translator.setHandler("itemDone", function(obj, item) {
//picks up some weird attachments and notes from the RIS - delete
item.attachments= [];
item.notes=[];
item.attachments.push({
url: url,
title: "Snapshot",
mimeType: "text/html"
});
item.attachments.push({
url: pdfurl,
title: "Full Text PDF",
mimeType: "application/pdf"
});
translator.translate();
if (abs) item.abstractNote = abs;
item.complete();
});
}
translator.translate();
});
}


/** BEGIN TEST CASES **/
@@ -89,39 +133,37 @@ var testCases = [
"items": [
{
"itemType": "journalArticle",
"title": "Removable Hair Caps of Karamoja (Uganda)",
"creators": [
{
"lastName": "Verswijver",
"firstName": "Gustaaf",
"creatorType": "author"
}
],
"notes": [],
"tags": [],
"seeAlso": [],
"date": "November 8, 2010",
"DOI": "10.1162/afar.2010.43.4.60",
"ISSN": "0001-9933",
"issue": "4",
"journalAbbreviation": "African Arts",
"libraryCatalog": "MIT Press Journals",
"pages": "60-71",
"publicationTitle": "African Arts",
"url": "https://doi.org/10.1162/afar.2010.43.4.60",
"volume": "43",
"attachments": [
{
"title": "African Arts Snapshot",
"title": "Snapshot",
"mimeType": "text/html"
},
{
"title": "African Arts Full Text PDF",
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"title": "Removable Hair Caps of Karamoja (Uganda)",
"date": "November 8, 2010",
"DOI": "10.1162/afar.2010.43.4.60",
"publicationTitle": "African Arts",
"journalAbbreviation": "African Arts",
"pages": "60-71",
"volume": "43",
"issue": "4",
"publisher": "MIT Press",
"ISSN": "0001-9933",
"url": "http://dx.doi.org/10.1162/afar.2010.43.4.60",
"accessDate": "September 4, 2012",
"libraryCatalog": "MIT Press Journals"
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
@@ -131,4 +173,4 @@ var testCases = [
"items": "multiple"
}
]
/** END TEST CASES **/
/** END TEST CASES **/

0 comments on commit edebd2e

Please sign in to comment.
You can’t perform that action at this time.