From c862f2ffb61b693752e6a396378098edce9c3e9d Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Tue, 29 Sep 2020 12:19:28 -0400 Subject: [PATCH] Implement Semantic Scholar lookups --- README.md | 8 +-- bin/build.sh | 7 +- .../content/scripts/zoterocitationcounts.js | 67 +++++++++++++++++++ install.rdf | 2 +- update.rdf | 4 +- 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f9a1083..ba5bfb1 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ This is an add-on for Zotero, a research source management tool. The add-on can auto-fetch citation counts for journal articles using -various APIs, including Crossref, Inspire HEP (and in the future -probably NASA/ADS and Semantic Scholar). (Google Scholar is not -supported because automated access is against its terms of service.) +various APIs, including Crossref, Inspire HEP, and and Semantic +Scholar (and in the future possibly NASA/ADS). Google Scholar is not +supported because automated access is against its terms of service. Please report any bugs, questions, or feature requests in the Github repository. @@ -26,7 +26,7 @@ Code for this extension is based on the [Zotero DOI - Run Zotero (version 5.x) - Go to `Tools -> Add-ons` - `Install Add-on From File` -- Choose the file `zotero-citationcounts-0.1.4.xpi` +- Choose the file `zotero-citationcounts-0.1.5.xpi` - Restart Zotero ## License diff --git a/bin/build.sh b/bin/build.sh index dcc232f..4011f17 100755 --- a/bin/build.sh +++ b/bin/build.sh @@ -1,11 +1,6 @@ #!/bin/sh -version=$1 - -if [ -z "$version" ]; then - echo "Synopsis: $0 " - exit 1 -fi +version='0.1.5' rm -f zotero-citationcounts-${version}.xpi zip -r zotero-citationcounts-${version}.xpi chrome/* defaults/* chrome.manifest install.rdf diff --git a/chrome/content/scripts/zoterocitationcounts.js b/chrome/content/scripts/zoterocitationcounts.js index 2ac9053..7c6eecf 100644 --- a/chrome/content/scripts/zoterocitationcounts.js +++ b/chrome/content/scripts/zoterocitationcounts.js @@ -148,6 +148,57 @@ async function getInspireCount(item, idtype) { return count; } +async function getSemanticScholarCount(item, idtype) { + let doi = null; + if (idtype == 'doi') { + doi = item.getField('DOI'); + } else if (idtype == 'arxiv') { + const arxiv = item.getField('url'); // check URL for arXiv id + const patt = /(?:arxiv.org[/]abs[/]|arXiv:)([a-z.-]+[/]\d+|\d+[.]\d+)/i; + const m = patt.exec(arxiv); + if (!m) { + // No arxiv id found + return -1; + } + doi = m[1]; + } else { + // Internal error + return -1; + } + if (!doi) { + // There is no DOI / arXiv id; skip item + return -1; + } + const edoi = encodeURIComponent(doi); + + const url = + "https://api.semanticscholar.org/v1/paper/" + + (idtype == 'doi' ? '' : 'arXiv:') + edoi + const response = await fetch(url) + .then(response => response.json()) + .catch(err => null); + + if (response === null) { + // Something went wrong + return -1; + } + + let count = null; + try { + // Semantic Scholar returns the actual citations + count = response['citations'].length; + // Semantic Scholar imposes a rate limit of 100 requests per 5 + // minutes. We should keep track of this globally so that we + // don't need to rate limit if there are just a few requests. + await await new Promise(r => setTimeout(r, 3000)); + } catch (err) { + // There are no citations + return -1; + } + + return count; +} + // Preference managers function getPref(pref) { @@ -389,6 +440,22 @@ Zotero.CitationCounts.updateItem = async function(item, operation) { } Zotero.CitationCounts.updateNextItem(operation); + } else if (operation == "semanticscholar") { + + const count_doi = await getSemanticScholarCount(item, 'doi'); + const count_arxiv = await getSemanticScholarCount(item, 'arxiv'); + if (count_doi >= 0 || count_arxiv >= 0) { + if (count_doi >= 0) { + setCitationCount(item, 'Semantic Scholar/DOI', count_doi); + } + if (count_arxiv >= 0) { + setCitationCount(item, 'Semantic Scholar/arXiv', count_arxiv); + } + item.saveTx(); + Zotero.CitationCounts.counter++; + } + Zotero.CitationCounts.updateNextItem(operation); + } else { Zotero.CitationCounts.updateNextItem(operation); } diff --git a/install.rdf b/install.rdf index c9bdc14..0224e6b 100644 --- a/install.rdf +++ b/install.rdf @@ -7,7 +7,7 @@ RDF:about="urn:mozilla:install-manifest" em:id="schnetter@gmail.com" em:name="Zotero Citation Counts Manager" - em:version="0.1.4" + em:version="0.1.5" em:type="2" em:creator="Erik Schnetter" em:description="Automatically fetch and update citation counts" diff --git a/update.rdf b/update.rdf index 09424af..90f6dca 100644 --- a/update.rdf +++ b/update.rdf @@ -5,13 +5,13 @@ - 0.1.4 + 0.1.5 zotero@chnm.gmu.edu 5.0 5.* - https://github.com/eschnett/zotero-citationcounts/releases/download/v0.1.4/zotero-citationcounts-0.1.4.xpi + https://github.com/eschnett/zotero-citationcounts/releases/download/v0.1.5/zotero-citationcounts-0.1.5.xpi