Problems with calling wikipedia API

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Problems with calling wikipedia API

Post by glenn9 » Tue Jan 26, 2021 2:24 pm

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

Thunder
Posts: 20
Joined: Thu Mar 12, 2015 1:52 pm

Re: Problems with calling wikipedia API

Post by Thunder » Tue Jan 26, 2021 4:29 pm

This work for me :

Code: Select all

put url "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=LiveCode&format=json"

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Problems with calling wikipedia API

Post by glenn9 » Tue Jan 26, 2021 5:02 pm

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
Example 1.png
.

However, I can get retrieve the 'name' when it is at layer 1 without a problem
Example 2.png
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'
Example 3.png
.

I guess its a syntax issue that I'm not understanding for getting the relevant element.

Thanks,

Glenn

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Problems with calling wikipedia API

Post by Thierry » Tue Jan 26, 2021 8:23 pm

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:

screenshot 2021-01-26 à 16.57.25.jpg

and the result in the field:

screenshot 2021-01-26 à 20.18.32.jpg
screenshot 2021-01-26 à 20.18.32.jpg (25.74 KiB) Viewed 10443 times

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Problems with calling wikipedia API

Post by glenn9 » 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.

Regards,

Glenn

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Problems with calling wikipedia API

Post by Thierry » Wed Jan 27, 2021 4:36 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: Problems with calling wikipedia API

Post by glenn9 » Thu Jan 28, 2021 9:16 am

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

Code: Select all

... the number of elements in ...
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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problems with calling wikipedia API

Post by Klaus » Thu Jan 28, 2021 1:25 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Problems with calling wikipedia API

Post by Klaus » Thu Jan 28, 2021 4:47 pm

Not that I mentioned this in my last posting... :D

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Problems with calling wikipedia API

Post by FourthWorld » Thu Jan 28, 2021 4:59 pm

Klaus wrote:
Thu Jan 28, 2021 4:47 pm
Not that I mentioned this in my last posting... :D
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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

kdjanz
Posts: 300
Joined: Fri Dec 09, 2011 12:12 pm

Re: Problems with calling wikipedia API

Post by kdjanz » Thu Jan 28, 2021 7:11 pm

May I suggest a tablet as upgraded equipment for the breakfast table? :P

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Problems with calling wikipedia API

Post by FourthWorld » Thu Jan 28, 2021 8:58 pm

kdjanz wrote:
Thu Jan 28, 2021 7:11 pm
May I suggest a tablet as upgraded equipment for the breakfast table? :P
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. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 161
Joined: Tue Feb 23, 2010 10:53 pm

Re: Problems with calling wikipedia API

Post by bobcole » 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.
Sorry, but I don't have any useable code to share,
Bob

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Problems with calling wikipedia API

Post by FourthWorld » Fri Jan 29, 2021 7:28 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Problems with calling wikipedia API

Post by FourthWorld » Fri Jan 29, 2021 8:28 am

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:
Wikipedia POTD.zip
(1.32 KiB) Downloaded 215 times
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply