crossref.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """CrossRef"""
  4. from urllib.parse import urlencode
  5. from datetime import datetime
  6. about = {
  7. "website": "https://www.crossref.org/",
  8. "wikidata_id": "Q5188229",
  9. "official_api_documentation": "https://api.crossref.org",
  10. "use_official_api": False,
  11. "require_api_key": False,
  12. "results": "JSON",
  13. }
  14. categories = ["science", "scientific publications"]
  15. paging = True
  16. search_url = "https://api.crossref.org/works"
  17. def request(query, params):
  18. params["url"] = search_url + "?" + urlencode({"query": query, "offset": 20 * (params["pageno"] - 1)})
  19. return params
  20. def response(resp):
  21. results = []
  22. for record in resp.json()["message"]["items"]:
  23. if record["type"] == "component":
  24. # These seem to be files published along with papers. Not something you'd search for
  25. continue
  26. result = {
  27. "template": "paper.html",
  28. "content": record.get("abstract", ""),
  29. "doi": record.get("DOI"),
  30. "pages": record.get("page"),
  31. "publisher": record.get("publisher"),
  32. "tags": record.get("subject"),
  33. "type": record.get("type"),
  34. "url": record.get("URL"),
  35. "volume": record.get("volume"),
  36. }
  37. if record["type"] == "book-chapter":
  38. result["title"] = record["container-title"][0]
  39. if record["title"][0].lower().strip() != result["title"].lower().strip():
  40. result["title"] += f" ({record['title'][0]})"
  41. else:
  42. result["title"] = record["title"][0] if "title" in record else record.get("container-title", [None])[0]
  43. result["journal"] = record.get("container-title", [None])[0] if "title" in record else None
  44. if "resource" in record and "primary" in record["resource"] and "URL" in record["resource"]["primary"]:
  45. result["url"] = record["resource"]["primary"]["URL"]
  46. if "published" in record and "date-parts" in record["published"]:
  47. result["publishedDate"] = datetime(*(record["published"]["date-parts"][0] + [1, 1][:3]))
  48. result["authors"] = [a.get("given", "") + " " + a.get("family", "") for a in record.get("author", [])]
  49. result["isbn"] = record.get("isbn") or [i["value"] for i in record.get("isbn-type", [])]
  50. # All the links are not PDFs, even if the URL ends with ".pdf"
  51. # result["pdf_url"] = record.get("link", [{"URL": None}])[0]["URL"]
  52. results.append(result)
  53. return results