From d8794878b2a18e3f8798122390d8d1f49d868fe4 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 30 Jan 2025 16:35:51 +0100 Subject: [PATCH] use_api --- .../zbmath/ZBMathPublication.py | 14 +++----- mardi_importer/mardi_importer/zbmath/misc.py | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/mardi_importer/mardi_importer/zbmath/ZBMathPublication.py b/mardi_importer/mardi_importer/zbmath/ZBMathPublication.py index c361260..359e5d2 100644 --- a/mardi_importer/mardi_importer/zbmath/ZBMathPublication.py +++ b/mardi_importer/mardi_importer/zbmath/ZBMathPublication.py @@ -1,3 +1,6 @@ +from .misc import search_item_by_property + + class ZBMathPublication: """Class to manage zbMath publication items in the local Wikibase instance. Attributes: @@ -160,16 +163,7 @@ def exists(self): if self.QID: return self.QID # instance of scholarly article - if self.title: - self.QID = self.item.is_instance_of_with_property( - "wd:Q13442814", self.label_id_dict["de_number_prop"], self.de_number - ) - else: - QID_list = self.api.search_entity_by_value( - self.label_id_dict["de_number_prop"], self.de_number - ) - # should not be more than one - self.QID = QID_list[0] if QID_list else None + self.QID = search_item_by_property(property_id = self.label_id_dict["de_number_prop"], value=self.de_number) if not self.QID: if self.arxiv_id: QID_list = self.api.search_entity_by_value("wdt:P818", self.arxiv_id) diff --git a/mardi_importer/mardi_importer/zbmath/misc.py b/mardi_importer/mardi_importer/zbmath/misc.py index 687b83c..dae2e57 100644 --- a/mardi_importer/mardi_importer/zbmath/misc.py +++ b/mardi_importer/mardi_importer/zbmath/misc.py @@ -1,5 +1,40 @@ from habanero import Crossref from requests.exceptions import HTTPError +import requests + + +def search_item_by_property(property_id,value): + """ + Search for pages in namespace 120 that have the statement: + haswbstatement:P= + + :param value: e.g. "6369674" + :return: JSON response from the API + """ + base_url = "https://portal.mardi4nfdi.de/w/api.php" + # Create the search query with the property_id and value + srsearch_query = f"haswbstatement:{property_id}={value}" + + # Set up the parameters for the API request + params = { + "action": "query", + "list": "search", + "srsearch": srsearch_query, + "srnamespace": "120", # Adjust if needed + "format": "json" + } + + response = requests.get(base_url, params=params) + # Raise an exception if the request was unsuccessful + response.raise_for_status() + + # Parse the response as JSON + data = response.json() + if data['query']['search']: + qid = data['query']['search'][0]['title'].split(':')[-1] + else: + qid = None + return qid def get_tag(tag_name, namespace):