SNT
SNT

Reputation: 1393

Wikibook API query

I am trying to understand how to build an API call where I can get data (json format) for the recipes, ingredients, and procedure as mention here. Anyone who could help me out with this?

https://en.wikibooks.org/wiki/Cookbook:Recipes

This URL has the recipe names and when clicked on each item it gets the ingredients and the procedure.

Upvotes: 1

Views: 682

Answers (2)

IRA1777
IRA1777

Reputation: 613

I'm not sure if it was your question, but in addition to Pascalco's answer, it worth note that you will NOT be able to get structured json data that details ingredients with their quantities and procedures, ie something like:

{ "ingredient": "milk", "quantity": { "number": "1", "unit":"liter"}}

The API will drop the raw mediawiki's syntax page content in one single field, and a few extra metadata about the page.

Moreover, the fact that those pages do not use templates make this type of data very difficult to extract, either with a syntax parser or html parser.

Upvotes: 1

Pascalco
Pascalco

Reputation: 2826

To get all recipes I would not use Cookbook:Recipes but rather Category:Recipes which is more complete.

The API call to list all recipes which are listed in Category:Recipes is the following: https://en.wikibooks.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Recipes&gcmlimit=max&format=json&gcmcontinue=. It will return you 500 recipes but there are more on Wikibooks. To get the remaining ones, use the continue -> gcmcontinue value in the response and append it to the next API call.

To get the ingredients and procedure of a recipe, call for example https://en.wikibooks.org/w/api.php?action=query&prop=revisions&format=json&rvprop=content&rvslots=%2A&rvsection=1&titles=Cookbook:Biscuits. You can use the | character to retrieve multiple recipes with the same API call: https://en.wikibooks.org/w/api.php?action=query&prop=revisions&format=json&rvprop=content&rvslots=%2A&titles=Cookbook:Biscuits|Cookbook:Baklava.

If you want to retrieve only the ingredients or only the procedure of recipes, use the additional parameter rvsection=. Most of the time (but not always) the ingredients are in the first section and the procedure is the second section. So calling

Upvotes: 2

Related Questions