Skip to content

Commit

Permalink
Fix PhilPaper
Browse files Browse the repository at this point in the history
update translator and adjust for minor change in bibtex handling
https://forums.zotero.org/discussion/107127/zotero-connector-for-philpapers-org-safari-on-mac
  • Loading branch information
adam3smith committed Sep 10, 2023
1 parent 999764a commit 62cc27e
Showing 1 changed file with 86 additions and 69 deletions.
155 changes: 86 additions & 69 deletions PhilPapers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,93 +9,109 @@
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2021-08-13 23:13:58"
"lastUpdated": "2023-09-10 18:24:30"
}

/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2012 Sebastian Karcher
Copyright © 2023 Sebastian Karcher
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
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/>.
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/


function detectWeb(doc, url) {
if (/\/s|pub\//.test(url)) return "multiple";
if (url.includes("/browse/") && ZU.xpathText(doc, '//ol[@class="entryList"]/li/@id') !== null) return "multiple";
if (url.includes("/rec/")) return "journalArticle";
if (url.includes('/rec/')) {
return 'journalArticle';
}
else if (getSearchResults(doc, true)) {
return 'multiple';
}
return false;
}


function doWeb(doc, url) {
let isPhilArchive = /^https?:\/\/philarchive\.org\//.test(url);
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('.entryList .citation>a');
for (let row of rows) {
let href = row.href;
let title = ZU.trimInternal(row.textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}

function idFromUrl(url) {
return url.match(/\/rec\/([A-Z-\d]+)/)[1];
}
async function doWeb(doc, url) {
let isPhilArchive = /^https?:\/\/philarchive\.org\//.test(url);
var ids = [];
if (detectWeb(doc, url) == "multiple") {
var items = {};
var titles = ZU.xpath(doc, '//li/span[@class="citation"]//span[contains (@class, "articleTitle")]');
var identifiers = ZU.xpath(doc, '//ol[@class="entryList"]/li/@id');
for (var i in titles) {
items[identifiers[i].textContent] = titles[i].textContent;

if (detectWeb(doc, url) == 'multiple') {
let items = await Zotero.selectItems(getSearchResults(doc, false));
if (!items) return;
for (let url of Object.keys(items)) {
let id = idFromUrl(url);
ids.push(id);
}
Zotero.selectItems(items, function (items) {
if (!items) {
return;
}
for (var i in items) {
ids.push(i.replace(/^e/, ""));
}
scrape(ids, isPhilArchive);
});
await scrape(ids, isPhilArchive);
}
else {
var identifier = url.match(/(\/rec\/)([A-Z-\d]+)/)[2];
let identifier = idFromUrl(url);
// Z.debug(identifier)
scrape([identifier], isPhilArchive);
await scrape([identifier], isPhilArchive);
}
}

function scrape(identifiers, isPhilArchive) {

async function scrape(identifiers, isPhilArchive) {
for (let id of identifiers) {
let bibtexURL = "/export.html?__format=bib&eId=" + id + "&formatName=BibTeX";
Zotero.Utilities.HTTP.doGet(bibtexURL, function (text) {
// remove line breaks, then match match the bibtex.
var bibtex = text.replace(/\n/g, "").match(/<pre class='export'>.+<\/pre>/)[0];
var url = "/rec/" + id;
var translator = Zotero.loadTranslator("import");
translator.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4");
translator.setString(bibtex);
translator.setHandler("itemDone", function (obj, item) {
if (isPhilArchive) {
item.libraryCatalog = 'PhilArchive';
item.url = `https://philarchive.org/rec/${id}`; // full-text
item.attachments.push({
title: 'Full Text PDF',
mimeType: 'application/pdf',
url: `/archive/${id}`
});
}

item.attachments.push({ url, title: "Snapshot", mimeType: "text/html" });
item.complete();
});
translator.translate();
let bibUrl = "https://philpapers.org/export.html?__format=bib&eIds=" + id + "&formatName=BibTeX";
let bibText = await requestText(bibUrl);
bibText = bibText.replace(/\n/g, "").match(/<pre class="export">.+<\/pre>/)[0];
let url = "/rec/" + id;
let translator = Zotero.loadTranslator("import");
translator.setTranslator('9cb70025-a888-4a29-a210-93ec52da40d4');
translator.setString(bibText);
translator.setHandler('itemDone', (_obj, item) => {
if (isPhilArchive) {
item.libraryCatalog = 'PhilArchive';
item.url = `https://philarchive.org/rec/${id}`; // full-text
item.attachments.push({
title: 'Full Text PDF',
mimeType: 'application/pdf',
url: `/archive/${id}`
});
}
else {
item.attachments.push({ url,
title: 'Snapshot',
mimeType: 'text/html' });
}
item.complete();
});
await translator.translate();
}
}

Expand All @@ -107,7 +123,7 @@ var testCases = [
"items": [
{
"itemType": "journalArticle",
"title": "Observation, Character, and A Purely First-Person Point of View",
"title": "Observation, Character, and a Purely First-Person Point of View",
"creators": [
{
"firstName": "Josep E.",
Expand Down Expand Up @@ -156,7 +172,7 @@ var testCases = [
"items": [
{
"itemType": "journalArticle",
"title": "Norm-Based Governance for a New Era: Collective Action in the Face of Hyper-Politicization",
"title": "Norm-Based Governance for a New Era: Lessons From Climate Change and Covid-19",
"creators": [
{
"firstName": "Leigh",
Expand All @@ -174,19 +190,18 @@ var testCases = [
"creatorType": "author"
}
],
"itemID": "RaymondForthcoming-RAYNGF",
"date": "2021",
"itemID": "Raymond2021-RAYNGF",
"libraryCatalog": "PhilArchive",
"pages": "1–14",
"publicationTitle": "Perspectives on Politics",
"shortTitle": "Norm-Based Governance for a New Era",
"url": "https://philarchive.org/rec/RAYNGF",
"volume": "1",
"attachments": [
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
},
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
Expand All @@ -200,7 +215,7 @@ var testCases = [
"url": "https://philarchive.org/rec/LANTEO-39",
"items": [
{
"itemType": "manuscript",
"itemType": "journalArticle",
"title": "The Ethics of Partiality",
"creators": [
{
Expand All @@ -209,17 +224,19 @@ var testCases = [
"creatorType": "author"
}
],
"itemID": "LangeManuscript-LANTEO-39",
"date": "2022",
"DOI": "10.1111/phc3.12860",
"issue": "8",
"itemID": "Lange2022-LANTEO-39",
"libraryCatalog": "PhilArchive",
"pages": "1–15",
"publicationTitle": "Philosophy Compass",
"url": "https://philarchive.org/rec/LANTEO-39",
"volume": "1",
"attachments": [
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
},
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
Expand Down

0 comments on commit 62cc27e

Please sign in to comment.