nogarcia / Epsilon

My personal general-purpose Discord bot.
MIT License
1 stars 0 forks source link

Fix Wikipedia command with REDIRECTs and Disambiguations. #1

Closed nogarcia closed 5 years ago

nogarcia commented 5 years ago

That code is a mess and crazily inefficient. It also breaks on a page REDIRECT and doesn't do well with disambiguation pages. I need to find a way to give some sort of checklist with that page.

nogarcia commented 5 years ago

This is what I get for blindly copy and pasting from StackOverflow. The docs for OpenSearch give the parameters plain and simple. Adding &redirects=resolve will return pages after the redirect.

GET to https://en.wikipedia.org/w/api.php?action=opensearch&search=The%20Incredible%20Hulk&limit=10&namespace=0&format=json:

[
    "The Incredible Hulk",
    [
        "The Incredible Hulk",
...snip
    ],
    [
        "",
        "The Incredible Hulk is a 2008 American superhero film based on the Marvel Comics character the Hulk, produced by Marvel Studios and distributed by Universal Pictures.",
...snip
    ],
    [
        "https://en.wikipedia.org/wiki/The_Incredible_Hulk",
...snip
    ]
]

The first URL redirects to Hulk, but the actual response we get is #REDIRECT[[Hulk]].

GET to https://en.wikipedia.org/w/api.php?action=opensearch&search=The%20Incredible%20Hulk&limit=10&namespace=0&format=json&redirects=resolve:

[
    "The Incredible Hulk",
    [
        "Hulk",
...snip
    ],
    [
        "The Hulk is a fictional superhero appearing in publications by the American publisher Marvel Comics. Created by writer Stan Lee and artist Jack Kirby, the character first appeared in the debut issue of The Incredible Hulk (May 1962).",
...snip
    ],
    [
        "https://en.wikipedia.org/wiki/Hulk",
...snip
    ]
]

The lesson is to always read the documentation.