WolfgangFahl / wdgrid

nicegui based wikidata grid and sync
Apache License 2.0
0 stars 0 forks source link

Add Wikidata search API wrapper #2

Closed WolfgangFahl closed 7 months ago

WolfgangFahl commented 7 months ago

https://www.mediawiki.org/wiki/Wikibase/API/ describes the full API including the search

https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities shows the API for the entity search

Example: https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Einstein&language=en&limit=2&uselang=de

{
    "searchinfo": {
        "search": "Einstein"
    },
    "search": [
        {
            "id": "Q937",
            "title": "Q937",
            "pageid": 1262,
            "display": {
                "label": {
                    "value": "Albert Einstein",
                    "language": "de"
                },
                "description": {
                    "value": "theoretischer Physiker, Entwickler der Relativit\u00e4tstheorie (1879\u20131955)",
                    "language": "de"
                }
            },
            "repository": "wikidata",
            "url": "//www.wikidata.org/wiki/Q937",
            "concepturi": "http://www.wikidata.org/entity/Q937",
            "label": "Albert Einstein",
            "description": "theoretischer Physiker, Entwickler der Relativit\u00e4tstheorie (1879\u20131955)",
            "match": {
                "type": "alias",
                "language": "en",
                "text": "Einstein"
            },
            "aliases": [
                "Einstein"
            ]
        },
        {
            "id": "Q146709",
            "title": "Q146709",
            "pageid": 148312,
            "display": {
                "label": {
                    "value": "(2001) Einstein",
                    "language": "de"
                },
                "description": {
                    "value": "Asteroid des Hauptg\u00fcrtels",
                    "language": "de"
                }
            },
            "repository": "wikidata",
            "url": "//www.wikidata.org/wiki/Q146709",
            "concepturi": "http://www.wikidata.org/entity/Q146709",
            "label": "(2001) Einstein",
            "description": "Asteroid des Hauptg\u00fcrtels",
            "match": {
                "type": "alias",
                "language": "en",
                "text": "Einstein"
            },
            "aliases": [
                "Einstein"
            ]
        }
    ],
    "search-continue": 2,
    "success": 1
}

see also https://github.com/samuelmeuli/wikibase-api

WolfgangFahl commented 7 months ago

Usage Example

# Import the WikidataSearch class
from wikidata_search import WikidataSearch

# Create an instance of WikidataSearch
# Optionally, you can specify the language and timeout (in seconds).
wikidata_search = WikidataSearch(language="en", timeout=5.0)

# Use the search method to look for a term, for example, "Python"
search_results = wikidata_search.search("Python", limit=5)

# Print the search results
print("Search Results:")
for result in search_results:
    print(f"ID: {result['id']}, Label: {result['label']}, Description: {result.get('description', 'No description')}")

# Use the searchOptions method to get a nicely formatted list of options
options = wikidata_search.searchOptions("Python", limit=5)

# Print the formatted options
print("\nFormatted Options:")
for qid, label, desc in options:
    print(f"QID: {qid}, Label: {label}, Description: {desc}")
WolfgangFahl commented 7 months ago

Test Example:

1:academic con(en):9
[
  [
    "Q2020153",
    "academic conference",
    "conference for researchers to present and discuss their work"
  ],
  [
    "Q47258130",
    "scientific conference series",
    "series of scientific conferences (e.g., yearly) with the same name and topic"
  ],