Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

 
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

can you post your function Noel... I have used open books already..

 

 

john

  • Author

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

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

  • Author

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

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

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

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])

 

  • Author
  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 !

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...

  • Author
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  ☹️

after the 

vsm_Image =

line

 

needs a ¶ at the end if you literally copy/paste

  • Author

I added that line... but now how to get the content of this block ? I join a screen copy of my script.

Capture d’écran 2019-10-23 à 18.27.30.png

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

  • Author

OK ! I'll try that way !

One more time thanks for your help.

Noël

  • 2 weeks later...
  • Author

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...
  • Author

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

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.