如何将两个维基百科API调用合并为一个?

时间:2016-03-06 11:46:58

标签: mediawiki wikipedia wikipedia-api mediawiki-api

我打电话给Wikipedia API,它返回该位置的标题,镜头文本,图像和地理坐标。我的维基百科API是:

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

此外,我还使用了另一个Wikipedia API,它根据地理坐标返回地名列表:

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

对于第二个API,我得到这样的回复:

"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
        },
        ...
    ]
}

现在我想在一个API中合并这两个搜索。我想在第二个API中添加第一个API的信息,如下所示:

"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
                    }
                }
            ]
        },
        ...
    ]
}

我想知道这有可能吗?

1 个答案:

答案 0 :(得分:8)

所以,如果我理解正确,你想要一个维基百科API请求获得标题拍摄文本图像和< strong> geo-cordinates (您的第一个API)适用于按给定坐标和半径(您的第二个API)位于特定区域内的所有地点(维基百科文章)。如果这是正确的,你可以这样做:

  1. main parametersformat=json&action=query

  2. query parameters

    • redirects=1
    • generator=geosearch(您的第二个API:见第3点)
    • prop=extracts|coordinates|pageimages(您的第一个API:请参阅第4,5和6点)
  3. geosearch parameters(所有生成器参数都以“g”为前缀):

    • ggslimit=20 - 查询的总结果(因为exlimit=20
    • ggsradius=1000&ggscoord=52.5243700|13.4105300 - 这是您的切入点
  4. parameters for extractsexintro=1&explaintext=1&exlimit=20(最多exlimit为20)

  5. parameters for coordinatescoprop=type|dim|globe&colimit=20(最多colimit为500)

  6. parameters for pageimagespiprop=thumbnail&pithumbsize=400&pilimit=20(最多为50)

  7. 如何看,最高colimit为500,最高pilimit为50,但由于exlimit,我们的使用次数不能超过20次。

    或者最后,您的请求将是上述所有参数的加入:

    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
    

    以下是回复:

    "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
                }
            },
            ...
        },
    }