Skip to content

Commit

Permalink
Implement Semantic Scholar lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Sep 29, 2020
1 parent a09cfcf commit c862f2f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions bin/build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/bin/sh

version=$1

if [ -z "$version" ]; then
echo "Synopsis: $0 <version>"
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
67 changes: 67 additions & 0 deletions chrome/content/scripts/zoterocitationcounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
RDF:about="urn:mozilla:install-manifest"
em:id="[email protected]"
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"
Expand Down
4 changes: 2 additions & 2 deletions update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<em:version>0.1.4</em:version>
<em:version>0.1.5</em:version>
<em:targetApplication>
<rdf:Description>
<em:id>[email protected]</em:id>
<em:minVersion>5.0</em:minVersion>
<em:maxVersion>5.*</em:maxVersion>
<em:updateLink>https://github.com/eschnett/zotero-citationcounts/releases/download/v0.1.4/zotero-citationcounts-0.1.4.xpi</em:updateLink>
<em:updateLink>https://github.com/eschnett/zotero-citationcounts/releases/download/v0.1.5/zotero-citationcounts-0.1.5.xpi</em:updateLink>
</rdf:Description>
</em:targetApplication>
</rdf:Description>
Expand Down

0 comments on commit c862f2f

Please sign in to comment.