Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOI Content Negotiation: Use Content-Type header instead of sniffing #3285

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions DOI Content Negotiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"priority": 100,
"inRepository": true,
"translatorType": 8,
"lastUpdated": "2023-09-22 09:54:11"
"lastUpdated": "2024-03-25 16:57:39"
}

/*
Expand Down Expand Up @@ -64,30 +64,28 @@ async function doSearch(items) {
}

async function processDOI(doi) {
let response = await requestText(
let response = await request(
`https://doi.org/${encodeURIComponent(doi)}`,
{ headers: { Accept: "application/vnd.datacite.datacite+json, application/vnd.crossref.unixref+xml, application/vnd.citationstyles.csl+json" } }
);
let { headers, body } = response;
// by content negotiation we asked for datacite or crossref format, or CSL JSON
if (!response) return;
Z.debug(response);
if (!body) return;
Z.debug(body);

let trans = Zotero.loadTranslator('import');
trans.setString(response);
trans.setString(body);

if (response.includes("<crossref")) {
let contentType = headers['content-type'] || '';
if (contentType.startsWith('application/vnd.crossref.unixref+xml')) {
// Crossref Unixref
trans.setTranslator('93514073-b541-4e02-9180-c36d2f3bb401');
trans.setHandler('itemDone', function (obj, item) {
item.libraryCatalog = "DOI.org (Crossref)";
item.complete();
});
}
else if (response.includes("http://datacite.org/schema")
// TEMP
// https://github.com/zotero/translators/issues/2018#issuecomment-616491407
|| response.includes('"agency": "DataCite"')
|| response.includes('"providerId": ')) {
else if (contentType.startsWith('application/vnd.datacite.datacite+json')) {
// Datacite JSON
trans.setTranslator('b5b5808b-1c61-473d-9a02-e1f5ba7b8eef');
trans.setHandler('itemDone', function (obj, item) {
Expand Down
Loading