October 22, 20196 yr 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
October 22, 20196 yr 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 October 22, 20196 yr by Noél Dubau
October 22, 20196 yr 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
October 22, 20196 yr 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 Edited October 22, 20196 yr by Noél Dubau
October 22, 20196 yr 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 October 22, 20196 yr by john renfrew
October 22, 20196 yr Author 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 https://openlibrary.org/api/books?bibkeys=ISBN:2-211-04679-7&jscmd=data&format=json https://openlibrary.org/api/books?bibkeys=ISBN:2-7324-2621-0&jscmd=data&format=json https://openlibrary.org/api/books?bibkeys=ISBN:2-211-05303-3&jscmd=data&format=json
October 22, 20196 yr 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 October 22, 20196 yr by john renfrew
October 23, 20196 yr 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])
October 23, 20196 yr Author Your previous post seems running well Would you me more explicit about the last ? That line is to replace one of the function ? Or anything else ? Thanks for your help !
October 23, 20196 yr 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...
October 23, 20196 yr 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 ☹️
October 23, 20196 yr Author I added that line... but now how to get the content of this block ? I join a screen copy of my script.
October 24, 20196 yr 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
November 2, 20196 yr 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
November 14, 20196 yr 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