Wikidata searches using the API

Other Wikidata-related searches, mostly using Cirrus Search

go_to_wikidata(search_term)

Open the browser for manual search on Wikidata

Source code in wdcuration/api_searches.py
def go_to_wikidata(search_term):
    """
    Open the browser for manual search on Wikidata
    """
    url = "https://www.wikidata.org/w/index.php?search=" + quote(search_term)
    webbrowser.open_new_tab(url)

search_wikidata(search_term, excluded_types=[], fixed_type=None, exclude_basic=True)

Looks up string on Wikidata

Source code in wdcuration/api_searches.py
def search_wikidata(
    search_term,
    excluded_types=[],
    fixed_type=None,
    exclude_basic=True,
):
    """
    Looks up string on Wikidata
    """

    basic_exclusion = list(
        {
            "Q26842193": "journal",
            "Q5633421": "scientific journal",
            "Q737498": "academic journal",
            "Q7725634": "literary work",
            "Q47461344": "written work",
            "Q101352": "family name",
            "Q13442814": "scholarly article",
            "Q732577": "publication",
            "Q3331189": "version, edition or translation",
            "Q187685": "doctoral thesis",
            "Q1266946": "thesis",
            "Q4167410": "disambiguation page",
            "Q3754629": "taxon",
            "Q4167836": "Wikimedia category",
            "Q30612": "clinical trial",
            "Q215380": "musical group",
            "Q482994": "musical album",
            "Q105543609": "musical work",
            "Q5": "human",
            "Q838795": "comic strip",
        }.keys()
    )
    excluded_types_local = excluded_types

    # Workaround to avoid accumulation. Not sure why, but they are accumulating.
    excluded_types_local = list(set(excluded_types_local))

    if exclude_basic == True:
        for excluded_type in basic_exclusion:
            excluded_types_local.append(excluded_type)  # Disambiguation page

    for excluded_type in excluded_types_local:
        search_term += f" -haswbstatement:P31={excluded_type} "
    if fixed_type is not None:
        search_term += f" haswbstatement:P31={fixed_type} "

    base_url = "https://www.wikidata.org/w/api.php"
    payload = {
        "action": "query",
        "list": "search",
        "srsearch": search_term,
        "language": "en",
        "format": "json",
        "origin": "*",
    }

    res = requests.get(base_url, params=payload)

    parsed_res = parse_wikidata_result(res.json())
    return parsed_res