Skip to content

Commit

Permalink
Merge pull request #160 from MaRDI4NFDI/use_api
Browse files Browse the repository at this point in the history
use_api
  • Loading branch information
LizzAlice authored Jan 30, 2025
2 parents 722be8b + d879487 commit 688e42c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
14 changes: 4 additions & 10 deletions mardi_importer/mardi_importer/zbmath/ZBMathPublication.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions mardi_importer/mardi_importer/zbmath/misc.py
Original file line number Diff line number Diff line change
@@ -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<property_id>=<value>
: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):
Expand Down

0 comments on commit 688e42c

Please sign in to comment.