Page 1 of 2
Problems with calling wikipedia API
Posted: Tue Jan 26, 2021 2:24 pm
by glenn9
Hi,
I'm in the middle of learning about how to use APIs with LC but having problems calling the Wikipedia API. As per their documentation
https://www.mediawiki.org/wiki/API:Tutorial I've put together the following code (similar has worked for other APIs) but draws a blank with wikipedia.
Is it that the JSON output is not in format that LC is expecting as it looks slightly different to other APIs have been successfully calling?
Grateful for any advice.
Many thanks,
Glenn
Code: Select all
on mouseup
put "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=LiveCode&format=json" into tURL
put url tURL into tData
put JSONtoArray(tData) into tArray
repeat with x = 1 to the number of elements in tArray
put tArray["title"] into tSearch
put tSearch into field"Field"
end repeat
end mouseup
Re: Problems with calling wikipedia API
Posted: Tue Jan 26, 2021 4:29 pm
by Thunder
This work for me :
Code: Select all
put url "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=LiveCode&format=json"
Re: Problems with calling wikipedia API
Posted: Tue Jan 26, 2021 5:02 pm
by glenn9
Thanks for looking at this.
I think we're both using the same url.
The problem I think is that I'm not understanding how to get the element from the Array when its 'nested' within the JSON file, eg when looking at 'title' in this example
.
However, I can get retrieve the 'name' when it is at layer 1 without a problem
using this code:
Code: Select all
put URL tURL into tData
put JSONToArray(tData) into tArray
repeat with x = 1 to the number of elements in tArray
put tArray[x]["name"] into tName
but i fail when I try to retrieve 'statusSeverityDescription' in this example as its sort of 'nested'
.
I guess its a syntax issue that I'm not understanding for getting the relevant element.
Thanks,
Glenn
Re: Problems with calling wikipedia API
Posted: Tue Jan 26, 2021 8:23 pm
by Thierry
glenn9 wrote: ↑Tue Jan 26, 2021 2:24 pm
Grateful for any advice.
Hi Glenn,
Here is an update of your original code.
Check the repeat loop which is different of yours
and all seems to work as intented... is it ?
In this case, the debugger is your friend:
and the result in the field:

- screenshot 2021-01-26 à 20.18.32.jpg (25.74 KiB) Viewed 10431 times
Regards,
Thierry
Re: Problems with calling wikipedia API
Posted: Wed Jan 27, 2021 8:42 am
by glenn9
Hi Thierry,
Thank you so much, I now have a better understanding of the Array syntax.
This was a great help.
Regards,
Glenn
Re: Problems with calling wikipedia API
Posted: Wed Jan 27, 2021 4:36 pm
by Thierry
glenn9 wrote: ↑Wed Jan 27, 2021 8:42 am
Hi Thierry,
Thank you so much, I now have a better understanding of the Array syntax.
This was a great help.
Excellent
I would like to show you a couple of things you could do with your wiki array
to make the code may be a bit more readable.
You could replace your repeat loop:
Code: Select all
repeat with x = 1 to the number of elements in wikiArray["query"]["search"]
put x & TAB & wikiArray["query"]["search"][x]["title"] &cr after fld 1
end repeat
with:
Code: Select all
put wikiArray["query"]["search"] into wikiSearch
repeat with x = 1 to the number of elements in wikiSearch
put x & TAB & wikiSearch[x]["title"] &cr after fld 1
end repeat
or even this one (you will loose the order here) :
Code: Select all
put wikiArray["query"]["search"] into wikiSearch
repeat for each key aKey in wikiSearch
put aKey & TAB & wikiSearch[aKey]["title"] &cr after fld 1
end repeat
Kind regards,
Thierry
Re: Problems with calling wikipedia API
Posted: Thu Jan 28, 2021 9:16 am
by glenn9
Hi Thierry,
Thanks for the further examples, it's helping me get more familiar with calling and extracting information from the APIs.
An area that I still struggle with is how to determine
when the information is nested.
For example, for this Transport for London example
Code: Select all
[
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "bakerloo",
"name": "Bakerloo",
"modeName": "tube",
"disruptions": [],
"created": "2021-01-21T16:58:36.427Z",
"modified": "2021-01-21T16:58:36.427Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": []
}
], ...
I'm wanting to loop through all of the London 'id' lines, Bakerloo, Circle, Metropolitan... etc to capture the 'statusSeverityDescription'.
At the moment I'm using a workaround like this:
Code: Select all
on updateService
put "https://api.tfl.gov.uk/Line/Mode/tube,dlr,overground,tflrail/Status" into tURL
put url tURL into tData
put JSONtoArray(tData) into tArray
repeat with x = 1 to 14 -- the 14 london underground lines
put tArray[x]["id"] into tStation
put tArray[x]["linestatuses"]["1"]["statusSeverityDescription"] into tService
what I really want to use is something like this: (which doesn't work, I guess because I haven't got the syntax right!)
Code: Select all
... repeat with x = 1 to the number of elements in ["id"]["linestatuses"]["1"]["statusSeverityDescription"]...
Grateful for any hints on getting the syntax correct!
Many thanks,
Kind regards,
Glenn
Re: Problems with calling wikipedia API
Posted: Thu Jan 28, 2021 1:25 pm
by Klaus
Hi Glenn,
do you mean something like this?
Code: Select all
...
put the keys of tArray into tKeys
## Optional:
## sort line of tKeys numeric
repeat for each key tKey in tKeys
put tArray[tKey]["linestatuses"]["1"]["statusSeverityDescription"] & CR after listofdescriptions
end repeat
put listofdescriptions
...
I put the array into a TREEVIEW widget and can "browse" the data.

A good way of getting an idea of the resulting array!
Best
Klaus
Re: Problems with calling wikipedia API
Posted: Thu Jan 28, 2021 4:47 pm
by Klaus
Not that I mentioned this in my last posting...

Re: Problems with calling wikipedia API
Posted: Thu Jan 28, 2021 4:59 pm
by FourthWorld
Klaus wrote: ↑Thu Jan 28, 2021 4:47 pm
Not that I mentioned this in my last posting...
Right you are. I was viewing this on my phone, and when I saw the code listing for the more complicated method I didn't notice the last line of your post, where you mention the code shown isn't needed at all if one just uses the Tree widget.
My morning reading includes about a hundred posts across a dozen venues and several newspapers, so the combination of the volume and consuming it on a small screen over breakfast sometimes means I do miss details.
I've removed my redundant post. Thank you for noting its redundancy.
Re: Problems with calling wikipedia API
Posted: Thu Jan 28, 2021 7:11 pm
by kdjanz
May I suggest a tablet as upgraded equipment for the breakfast table?

Re: Problems with calling wikipedia API
Posted: Thu Jan 28, 2021 8:58 pm
by FourthWorld
kdjanz wrote: ↑Thu Jan 28, 2021 7:11 pm
May I suggest a tablet as upgraded equipment for the breakfast table?
It's my first choice for reading, but I hate writing on it.
I should probably just get in the habit of spending more time with my laptop in the mornings.
Or better still leave em all behind and just take my bicycle to have a quiet breakfast next to the lake down the street.

Re: Problems with calling wikipedia API
Posted: Fri Jan 29, 2021 4:13 am
by bobcole
I am enjoying this Wikipedia API topic. There are some beautiful images on Wikipedia.
I tried to create a script to retrieve the Picture of the Day but I haven't been able to figure out how to use the API. Frustrated.
If anyone wants to spend time (I spent hours but it should be easier), I think it would be fun to see a working API script for those pictures.
Sorry, but I don't have any useable code to share,
Bob
Re: Problems with calling wikipedia API
Posted: Fri Jan 29, 2021 7:28 am
by FourthWorld
bobcole wrote: ↑Fri Jan 29, 2021 4:13 am
I am enjoying this Wikipedia API topic. There are some beautiful images on Wikipedia.
I tried to create a script to retrieve the Picture of the Day but I haven't been able to figure out how to use the API. Frustrated.
If anyone wants to spend time (I spent hours but it should be easier), I think it would be fun to see a working API script for those pictures.
I haven't look at the API, but I did look at the page source and I'll bet they use enough consistency to lend itself well to scraping.
Re: Problems with calling wikipedia API
Posted: Fri Jan 29, 2021 8:28 am
by FourthWorld
Forget what I wrote a minute ago. The API needed for POTD turns out to be easier than scraping.
You make a one-line call to obtain a list of images for POTD (or any page at Wikipedia), convert that to an array, then get the element for the first picture. If there are multiple images and you want to show them all you can walk through the elements of the "srcset" subarray, creating new image objects as you go.
This quickie demo grabs the JSON and displays it as an array, then grabs the first image URL and displays it by setting an image object's filename property: