I am looking for the best method to map a given Wikipedia/Wikisource/Wikibooks/... URL to the corresponding Wikidata entity id (similar to this question but via API calls instead of a full data dump). The URL can be in many forms, e.g.
- http://en.wikipedia.org/w/index.php?title=Element_18 is redirected (HTTP) to
- https://en.wikipedia.org/w/index.php?title=Element_18 is redirected (HTTP) to
- https://en.wikipedia.org/wiki/Element_18 is redirected (JavaScript/HTML) to
- https://en.wikipedia.org/wiki/Argon
Methods I tried so far:
- SPARQL query service - requires the URL to have a canonical form and seems to not know about redirects
- MediaWiki API at Wikidata, e.g. https://www.wikidata.org/w/api.php?action=wbgetentities&sites=enwiki&titles=Argon&languages=en&props=labels|descriptions&format=jsonfm requires to securely split URL into title and "site" key
- MediaWiki API at project site, e.g. https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&titles=Argon requires to securely split URL into project and title
- Info page, e.g. https://en.wikipedia.org/w/index.php?title=Argon&action=info requires to modify URL and to parse resulting HTML
The most reliable (but nasty) method seems to enulate a browser to do a HTTP request to the URL, follow all redirects and parse the resulting HTML page to get the Wikidata ID.