Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 1847 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted
 
Hello
I use a function that allows me to recover data on openbooks (title, author, publisher ... cover image ...); if one of the elements is missing I have an error returned. Attached is the one returned if the image is missing.
How to handle this and prevent the message from appearing?
Thank you for your help

error.png

Posted (edited)

Here is the function

Quote

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶

vsm_titre = vsm_auteur1 = vsm_auteur2 = vsm_nbPages = vsm_urlImage = vsm_Image = vsm_Editeur = \"\"¶

def parsedResponse = new JsonSlurper().parse(new URL(url))¶

if (parsedResponse.size() == 1 ){¶

    root = parsedResponse.keySet()[0]¶
    keySet = parsedResponse.\"${root}\".keySet()¶

    vsm_titre = parsedResponse.\"${root}\".title¶
    if (parsedResponse.\"${root}\".authors != null) {¶
        vsm_auteur1 = parsedResponse.\"${root}\".authors[0].name¶
        vsm_auteur2 = parsedResponse.\"${root}\".authors.size() > 1 ? parsedResponse.\"${root}\".authors[1].name : \"\" ¶
    }¶
    vsm_nbPages = parsedResponse.\"${root}\".number_of_pages¶

    vsm_Editeur = parsedResponse.\"${root}\".publishers.name¶
        
   
    vsm_urlImage = parsedResponse.\"${root}\".cover.medium¶
    vsm_Image = new URL ( vsm_urlImage )¶
    1¶
}"; "isGui=false" )

 
Honesty obliges I am not the creator! Clem present on the forum is the daddy of this job!

I try to use openbooks since amazon allows its API only to those who sell the apps !!!

Edited by Noél Dubau
Posted

Try this

 

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶
¶
vsm_titre = vsm_auteur1 = vsm_auteur2 = vsm_nbPages = vsm_urlImage = vsm_Image = vsm_Editeur = ''¶
¶
parsedResponse = new JsonSlurper().parse(new URL(url))¶
¶
if (parsedResponse.size() == 1 ){¶
	root = parsedResponse.keySet()[0]¶
	keySet = parsedResponse.\"${root}\"¶
¶
vsm_titre = keySet.title?:''¶
¶
vsm_auteur1 = keySet.authors[0]?keySet.authors[0].name:''¶
vsm_auteur2 = keySet.authors[1]?keySet.authors[1].name:''¶
vsm_nbPages = keySet.number_of_pages?:''¶
vsm_Editeur = keySet.publishers.name?:''¶
vsm_urlImage = keySet.cover.medium?:null¶
¶
vsm_Image = vsm_urlImage?new URL ( vsm_urlImage ):''¶
¶
} else {¶
	return ''¶
}" ; "isGui = false" )

it just tests each node is there and then extracts the value if it is

Posted (edited)

I just tried it ;

  • I  encountered the error about the cover and many times a blank cover was added in the container.
  • A new error appeared and I don't know the reason

1677772602_Capturedecran2019-10-22a14_25_35.png.96e78d18f3dcae1964c32252e265e2f3.png

Capture d’écran 2019-10-22 à 14.21.40.png

Edited by Noél Dubau
Posted (edited)

can you post the sample URL??

 

I am pretty sure it might be because the 'cover' node does not exists and so it can't find the medium sub-node on it...

 

try replacing the line with...

vsm_urlImage = keySet.cover?keySet.cover.medium:null

 

Edited by john renfrew
Posted

For the error "Cannot invoke method getAt() on null object the following ones : a webviewer on these url is not empty but we don't get any info about the book

 

Posted (edited)

The issue is that these books have no authors - so it fails quite early on...

I changed the code to test for each presence

 

Quote

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶
¶
//vsm_titre = vsm_auteur1 = vsm_auteur2 = vsm_nbPages = vsm_urlImage = vsm_Image = vsm_Editeur = ''¶
¶
parsedResponse = new JsonSlurper().parse(new URL(url))¶
¶
if (parsedResponse.size() == 1 ){¶
	root = parsedResponse.keySet()[0]¶
	keySet = parsedResponse.\"${root}\"¶
¶
	vsm_titre = keySet.title?:''¶
	vsm_auteur1 = keySet.authors?keySet.authors[0].name:''¶
	vsm_auteur2 = (keySet.authors && keySet.authors[1])?keySet.authors[1].name:''¶
	vsm_nbPages = keySet.number_of_pages?:''¶
	vsm_Editeur = keySet.publishers?keySet.publishers.name:''¶
	vsm_urlImage = keySet.cover?keySet.cover.medium:null¶
¶
	vsm_Image = vsm_urlImage?new URL ( vsm_urlImage ):''¶
} else {¶
	return ''¶
}" ; "isGui = false " )

 

 

Edited by john renfrew
Posted

you could also be really smart and return the whole thing as a JSON block too... (this definitely needs SM 5.2+)

 

return JsonOutput.toJson([vsm_titre: vsm_titre, vsm_auteur1: vsm_auteur1, vsm_auteur2: vsm_auteur2, vsm_nbPages:  vsm_nbPages, vsm_Editeur: vsm_Editeur, vsm_Image: vsm_Image])

 

Posted
  1. Your previous post seems running well
  2. Would you me more explicit about the last ? That line is to replace one of the function ? Or anything else ?

Thanks for your help !

Posted

I presume at the moment you are doing lots of SMGetVariable calls after running this function

If you added the return statement right at the end you would get back all the answers as a transformed JSON block you could process in a different way...

Posted
1 hour ago, john renfrew said:

I presume at the moment you are doing lots of SMGetVariable calls after running this function

If you added the return statement right at the end you would get back all the answers as a transformed JSON block you could process in a different way...

Sorry to be so bad pupil but where exactly must I add that line  ☹️

Posted

What you get back is JSON version of the information you asked the function for..

so instead of SMGetVariable calls you would use JSONGetElement instead..

 

The benefit was to show how this was possible - not to make you change how you do things, it's up to you if change your implementation, but was to show how to export the data out as a JSON block

  • 2 weeks later...
Posted

hello John

Sorry to come back with a new problem. I launched the acquisition of datas on 1117 isbn or ean. It runs fine except these four references.

9782874260377 - 9782070511914 - 9782092021637 - 2713506859

These references returns me 2 alerts

Have you ideas to void these alerts ?

Thanks

ND

Capture d’écran 2019-11-01 à 09.44.21.png

Capture d’écran 2019-11-01 à 09.44.45.png

  • 2 weeks later...
Posted

Hello John

My question was answered privately by a participant in the French forum, but also on this one, I named ericire. His strategy was to modify the function by detecting these error messages to avoid the display of information boxes. I allow myself to join his code (still Hebrew for the novice that I am).

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶try {¶parsedResponse = new JsonSlurper().parse(new URL(url))¶¶if (parsedResponse.size() == 1 ){¶    root = parsedResponse.keySet()[0]¶    keySet = parsedResponse.\"${root}\"¶¶    vsm_titre = keySet.title?:''¶    vsm_auteur1 = keySet.authors?keySet.authors[0].name:''¶    vsm_auteur2 = (keySet.authors && keySet.authors[1])?keySet.authors[1].name:''¶    vsm_nbPages = keySet.number_of_pages?:''¶    vsm_Editeur = keySet.publishers?keySet.publishers.name:''¶    vsm_urlImage = keySet.cover?keySet.cover.medium:null¶    vsm_Image = vsm_urlImage?new URL ( vsm_urlImage ):''¶} else {¶return 'pas de résultat'¶}¶} catch (Exception e) {¶return 'Erreur : ' + e¶}" )

Merci à tous deux

Noël

This topic is 1847 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.