wikipedia-apimediawiki-apiwikimediawikimedia-commons

Retrieve Wikimedia Commons Category of a Wikipedia page using the Wikimedia API


I am trying to use the Wikimedia Api to get the corresponding Wikimedia Commons category to a specific Wikipedia page. I assume that it is possible as most Wikipedia pages include an "in other projects" - section in the sidebar which has a link that redirects to the Commons Category (for example: https://de.wikipedia.org/wiki/Albert_Einstein)

Thanks in advance.


Solution

  • You can do it in two API calls, the first call to German Wikipedia gets you the Wikidata Qid:

    https://de.wikipedia.org/w/api.php?action=query&format=json&prop=wbentityusage&titles=Albert%20Einstein&wbeuprop=&wbeuaspect=

    Which returns:

    {
        "batchcomplete": "",
        "query": {
            "pages": {
                "1278360": {
                    "pageid": 1278360,
                    "ns": 0,
                    "title": "Albert Einstein",
                    "wbentityusage": {
                        "Q937": {
                            "aspects": [
                                "S",
                                "T",
                                "C.P227",
                                "C.P214",
                                "C.P244"
                            ]
                        }
                    }
                }
            }
        }
    }
    

    Then you can use the Wikidata API to get the name of the Commons category: https://www.wikidata.org/w/api.php?action=wbgetclaims&format=json&entity=Q937&property=P373

    Which returns:

    {
        "claims": {
            "P373": [
                {
                    "mainsnak": {
                        "snaktype": "value",
                        "property": "P373",
                        "hash": "be154a8a3dfc826844ceb5a62389857a65ff1e4e",
                        "datavalue": {
                            "value": "Albert Einstein",
                            "type": "string"
                        },
                        "datatype": "string"
                    },
                    "type": "statement",
                    "id": "q937$2F332903-133D-4CA0-AD24-8B4292C2BF89",
                    "rank": "normal"
                }
            ]
        }
    }
    

    The value in datavalue is the name of the category. You get the full URL by just prepending https://commons.wikimedia.org/wiki/Category:

    https://commons.wikimedia.org/wiki/Category:Albert Einstein