| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  | """CrossRef""" | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | about = { | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  |     "website": "https://www.crossref.org/", | 
					
						
							|  |  |  |     "wikidata_id": "Q5188229", | 
					
						
							|  |  |  |     "official_api_documentation": "https://api.crossref.org", | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |     "use_official_api": False, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  |     "results": "JSON", | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  | categories = ["science", "scientific publications"] | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  | search_url = "https://api.crossref.org/works" | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  |     params["url"] = search_url + "?" + urlencode({"query": query, "offset": 20 * (params["pageno"] - 1)}) | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  |     for record in resp.json()["message"]["items"]: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if record["type"] == "component": | 
					
						
							|  |  |  |             # These seem to be files published along with papers. Not something you'd search for | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         result = { | 
					
						
							|  |  |  |             "template": "paper.html", | 
					
						
							|  |  |  |             "content": record.get("abstract", ""), | 
					
						
							|  |  |  |             "doi": record.get("DOI"), | 
					
						
							|  |  |  |             "pages": record.get("page"), | 
					
						
							|  |  |  |             "publisher": record.get("publisher"), | 
					
						
							|  |  |  |             "tags": record.get("subject"), | 
					
						
							|  |  |  |             "type": record.get("type"), | 
					
						
							|  |  |  |             "url": record.get("URL"), | 
					
						
							|  |  |  |             "volume": record.get("volume"), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if record["type"] == "book-chapter": | 
					
						
							|  |  |  |             result["title"] = record["container-title"][0] | 
					
						
							|  |  |  |             if record["title"][0].lower().strip() != result["title"].lower().strip(): | 
					
						
							|  |  |  |                 result["title"] += f" ({record['title'][0]})" | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2023-09-13 18:21:10 +02:00
										 |  |  |             result["title"] = record["title"][0] if "title" in record else record.get("container-title", [None])[0] | 
					
						
							|  |  |  |             result["journal"] = record.get("container-title", [None])[0] if "title" in record else None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if "resource" in record and "primary" in record["resource"] and "URL" in record["resource"]["primary"]: | 
					
						
							|  |  |  |             result["url"] = record["resource"]["primary"]["URL"] | 
					
						
							|  |  |  |         if "published" in record and "date-parts" in record["published"]: | 
					
						
							|  |  |  |             result["publishedDate"] = datetime(*(record["published"]["date-parts"][0] + [1, 1][:3])) | 
					
						
							|  |  |  |         result["authors"] = [a.get("given", "") + " " + a.get("family", "") for a in record.get("author", [])] | 
					
						
							|  |  |  |         result["isbn"] = record.get("isbn") or [i["value"] for i in record.get("isbn-type", [])] | 
					
						
							|  |  |  |         # All the links are not PDFs, even if the URL ends with ".pdf" | 
					
						
							|  |  |  |         # result["pdf_url"] = record.get("link", [{"URL": None}])[0]["URL"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.append(result) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-26 18:10:12 +02:00
										 |  |  |     return results |