Wikipedia Viewer - How to get Extracts

Wikipedia Viewer - How to get Extracts
0.0 0

#1

Hello World!

I’m having a problem trying to get the extract of a wiki page. From my understanding, I cannot search titles and get extracts in one ajax call; also I cannot make two ajax calls on one button click (unless its coded after\inside the other). My attempt so far is thus:
-query list of titles, store them
-get page ids from query, store them
-call second ajax at the end of success to:
-query using pageids to get extracts, store them (trouble here!)

Here is a link to the CodePen so far: https://codepen.io/tylerOtj/pen/xPeoER
If I type in my browser https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&exchars=60&pageids=18630637 I can view the ‘extracts’ content. This is what I want to access!

attempts:
Usually in firefox console, on an output of apiResults, I can expand Object -> Query -> Search-> and see the array for ex. all titles, Ajax call using above url outputs Object -> Query -> Pages with no relevant information. (1)
Separating common wiki api access url and data results in the same output (2)
Querying just using pageid to access all key:values results in same output (3)
Using combinations of generator, exintro: true, prop: extracts results in same output

If any of this is false, there’s a better way to go about it, I’m not using the API correctly, any other insight, or point in the right direction would be much appreciated! Thank you


#2

If you really want to get the extracts, then there’s technically no problem with making another AJAX call inside your first success handler. This is actually a very common pattern, though if you wanted to learn about promises you could simplify the code quite well. My suggestion is to seriously think about whether you need the extracts. In your first request, you get a response with snippets for each answer. Would the snippets suffice for your purpose?


#3

The snippet[0] I was using to test on showed a bunch of links that I mistook for the “see all” wiki section. Snippets will work fine! So simple… Thank you!


#4

I was trying to do the same thing (because the sample product seemed to be using extracts) and I found that I can get search results and their extracts in 1 call by using the “generator” feature.

https://www.mediawiki.org/wiki/API:Page_info_in_search_results#Querying_query_results_in_one_request

My code: