Skip to content

Commit

Permalink
update to latest crossref api
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Mar 13, 2024
1 parent e184ec1 commit fce8b6e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ export class CrossrefSearcher {
}

lookup(searchTerm) {
return fetch(`https://search.crossref.org/dois?q=${encodeURIComponent(escape(searchTerm))}`, {
return fetch(`/api/citation_api_import/proxy/https://api.crossref.org/v1/works?query.bibliographic=${encodeURIComponent(escape(searchTerm))}&rows=5&select=DOI,ISBN,title,author,published,abstract`, {
method: "GET",
}).then(
response => response.json()
).then(items => {
).then(json => {
const searchEl = document.getElementById("bibimport-search-result-crossref")
if (!searchEl) {
// window was closed before result was ready.
return
}
if (items.length) {
searchEl.innerHTML = searchApiResultCrossrefTemplate({items})
} else {
if (json.status !== "ok" || !json.message.items) {
searchEl.innerHTML = ""
} else {
searchEl.innerHTML = searchApiResultCrossrefTemplate({items: json.message.items})
}
this.bind()
})
}

getBibtex(doi) {
this.importer.dialog.close()
fetch(`https://api.crossref.org/v1/works/${doi}/transform`, {
fetch(`/api/citation_api_import/proxy/https://api.crossref.org/v1/works/${encodeURIComponent(doi)}/transform`, {
method: "GET",
headers: {
"Accept": "application/x-bibtex"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export class PubmedSearcher {
}

lookup(searchTerm) {

return get("/api/citation_api_import/https://www.bioinformatics.org/texmed/cgi-bin/query.cgi", {query: escape(searchTerm)}).then(
return get("/api/citation_api_import/proxy/https://www.bioinformatics.org/texmed/cgi-bin/query.cgi", {query: escape(searchTerm)}).then(
response => response.text()
).then(
html => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,43 +176,41 @@ export const searchApiResultCrossrefTemplate = ({items}) => {
return "<h3 class=\"fw-green-title\">Crossref</h3><table class=\"fw-data-table fw-large dataTable-table\">" +
`<tr>
<th></th>
<th>${gettext("Title/Year")}</th>
<th>${gettext("Title/Published")}</th>
<th>${gettext("DOI")}</th>
<th>${gettext("Description")}</th>
<th>${gettext("Abstract")}</th>
</tr>` +
items.map(item =>
`<tr class="item">
items.map(item => {
const publishedDate = item.published && item.published['date-parts'] ? item.published['date-parts'].map(part => part.join("-")).join(", ") : ""

Check failure on line 184 in fiduswriter/citation_api_import/static/js/modules/citation_api_import/templates.js

View workflow job for this annotation

GitHub Actions / test

Strings must use doublequote

Check failure on line 184 in fiduswriter/citation_api_import/static/js/modules/citation_api_import/templates.js

View workflow job for this annotation

GitHub Actions / test

Strings must use doublequote
const abstractWithoutHTML = item.abstract ? item.abstract.replace(/<[^>]*>?/gm, '') : ""

Check failure on line 185 in fiduswriter/citation_api_import/static/js/modules/citation_api_import/templates.js

View workflow job for this annotation

GitHub Actions / test

Strings must use doublequote
return `<tr class="item">
<td><button type="button" class="api-import fw-button fw-orange fw-small"
data-doi="${item.doi.replace(/https?:\/\/(dx\.)?doi\.org\//gi, "")}">
data-doi="${item.DOI}">
${gettext("Import")}
</button></td>
<td><h3>
${
item.fullCitation ?
item.fullCitation :
`${item.title} ${item.year}`
}
${item.title} ${publishedDate}
</h3></td>
<td>
${
item.doi ?
`<p>${item.doi}</p>` :
item.DOI ?
`<p>${item.DOI}</p>` :
""
}
</td>
<td>
${
item.description ?
abstractWithoutHTML.length ?
`<p>
${
item.description.length < 200 ?
escapeText(item.description) :
escapeText(item.description.substring(0, 200)) + "..."
abstractWithoutHTML.length < 200 ?

Check failure on line 206 in fiduswriter/citation_api_import/static/js/modules/citation_api_import/templates.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 spaces but found 8
escapeText(abstractWithoutHTML) :
escapeText(abstractWithoutHTML.substring(0, 200)) + "..."
}
</p>` :
""
}
</td>
</tr>`
).join("") + "</table>"
}).join("") + "</table>"

Check failure on line 215 in fiduswriter/citation_api_import/static/js/modules/citation_api_import/templates.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 4
}
13 changes: 13 additions & 0 deletions fiduswriter/citation_api_import/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from django.views.decorators.http import require_GET
from django.http import HttpResponseForbidden
from django.http import HttpResponse
from django.conf import settings

ALLOWED_DOMAINS = {
"search.gesis.org": True,
"api.datacite.org": True,
"www.bioinformatics.org": True,
"api.crossref.org": True,
}


Expand All @@ -21,11 +23,22 @@ async def proxy(request, url):
if domain not in ALLOWED_DOMAINS:
return HttpResponseForbidden()
query_string = request.META["QUERY_STRING"]
if domain == "api.crossref.org":
mailto = f"mailto={settings.CONTACT_EMAIL}"
if len(query_string):
query_string += "&" + mailto
else:
query_string = mailto
if len(query_string):
url = f"{url}?{query_string}"
async with AsyncClient() as client:
response = await client.get(
url,
headers={
"User-Agent": request.META.get("HTTP_USER_AGENT", "Fidus Writer"),
"Referer": request.META.get("HTTP_REFERER", ""),
"Accept": request.META.get("HTTP_ACCEPT", "application/json"),
},
timeout=88, # Firefox times out after 90 seconds, so we need to return before that.
)
return HttpResponse(response.text, status=response.status_code)

0 comments on commit fce8b6e

Please sign in to comment.