Search code examples
mediawikiwikipediawikipedia-apimediawiki-api

How to combine two Wikipedia API calls into one?


I'm making a call to a Wikipedia API which returns a Title, Shot-Text, Image and geo-cordinates of that location. My Wikipedia API is:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts|pageimages|coordinates&titles=Berlin&redirects=1&formatversion=2&exintro=1&explaintext=1&piprop=thumbnail&pithumbsize=400

Also I'm using another Wikipedia API which returns a list of place names according to their geo-coordinates:

https://en.wikipedia.org/w/api.php?format=json&action=query&list=geosearch&gsradius=1000&gscoord=52.5243700|13.4105300&gslimit=50&gsprop=type|dim|globe

For the second API I get a response like this:

"query": {
    "geosearch": [
        {
            "pageid": 28782169,
            "ns": 0,
            "title": "1757 Berlin raid",
            "lat": 52.523405,
            "lon": 13.4114,
            "dist": 122.4,
            "primary": "",
            "type": null,
            "dim": 1000
        },
        {
            "pageid": 526195,
            "ns": 0,
            "title": "Scheunenviertel",
            "lat": 52.526111111111,
            "lon": 13.41,
            "dist": 196.9,
            "primary": "",
            "type": "landmark",
            "dim": 1000
        },
        ...
    ]
}

Now I want to combine these two searches in one API. I want to add information from my first API within the second API, something like as below:

"query": {
    "geosearch": [
        {
            "pageid": 28782169,
            "ns": 0,
            "title": "1757 Berlin raid",
            "lat": 52.523405,
            "lon": 13.4114,
            "dist": 122.4,
            "primary": "",
            "type": null,
            "dim": 1000

            "pages": [
                {
                    "pageid": 28782169,
                    "ns": 0,
                    "title": "1757 Berlin raid",
                    "extract": "Berlin is the capital of Germany and one of the 16 states of Germany. With a population of 3.5 million people, it is the second most populous city proper and the seventh.........",
                    "thumbnail": {
                        "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Siegessaeule_Aussicht_10-13_img4_Tiergarten.jpg/400px-Siegessaeule_Aussicht_10-13_img4_Tiergarten.jpg",
                        "width": 400,
                        "height": 267
                    }
                }
            ]
        },
        ...
    ]
}

I want to know is it possible in this way?


Solution

  • So, if I understand right, you want with one Wikipedia API request to get a title, shot-text, image and geo-cordinates (your first API) for all the places (Wikipedia articles) that are located in a certain area by given coordinates and radius (your second API). If that correct, you can do it in this way:

    1. main parameters: format=json&action=query

    2. query parameters:

      • redirects=1
      • generator=geosearch (your second API: see point 3)
      • prop=extracts|coordinates|pageimages (your first API: see point 4, 5, and 6)
    3. geosearch parameters (all generator parameters are prefixed with a "g"):

      • ggslimit=20 - your total results from the query (because the exlimit=20)
      • ggsradius=1000&ggscoord=52.5243700|13.4105300 - this is your entry point
    4. parameters for extracts: exintro=1&explaintext=1&exlimit=20 (max exlimit is 20)

    5. parameters for coordinates: coprop=type|dim|globe&colimit=20 (max colimit is 500)

    6. parameters for pageimages: piprop=thumbnail&pithumbsize=400&pilimit=20 (max is 50)

    How you see, the max colimit is 500 and the max pilimit is 50, but we can't use more than 20, because of exlimit.

    Or finally, your request will be the joining of all parameters above:

    https://en.wikipedia.org/w/api.php?format=json&action=query&redirects=1&generator=geosearch&prop=extracts|coordinates|pageimages&ggslimit=20&ggsradius=1000&ggscoord=52.5243700|13.4105300&exintro=1&explaintext=1&exlimit=20&coprop=type|dim|globe&colimit=20&piprop=thumbnail&pithumbsize=400&pilimit=20
    

    And here is the response:

    "query":{
        "pages":{
            "2511":{
                "pageid":2511,
                "ns":0,
                "title":"Alexanderplatz",
                "extract":"Alexanderplatz (pronounced [\u0294al\u025bk\u02c8sand\u0250\u02ccplats]) is a large public square and transport hub in the central Mitte district of Berlin, near the Fernsehturm. Berliners often call it simply Alex, referring to a larger neighbourhood stretching from Mollstra\u00dfe in the northeast to Spandauer Stra\u00dfe and the Rotes Rathaus in the southwest.",
                "coordinates":[
                    {
                        "lat":52.52166748,
                        "lon":13.41333294,
                        "primary":"",
                        "type":"landmark",
                        "dim":"1000",
                        "globe":"earth"
                    }
                ],
                "thumbnail":{
                    "source":"https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Alexanderplatz_by_the_night_-_ProtoplasmaKid.webm/400px--Alexanderplatz_by_the_night_-_ProtoplasmaKid.webm.jpg",
                    "width":400,
                    "height":225
                }
            },
            ...
        },
    }