crossref.py 2.5 KB

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