Noél Dubau Posted October 22, 2019 Posted October 22, 2019 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
john renfrew Posted October 22, 2019 Posted October 22, 2019 can you post your function Noel... I have used open books already.. john
Noél Dubau Posted October 22, 2019 Author Posted October 22, 2019 (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 October 22, 2019 by Noél Dubau
john renfrew Posted October 22, 2019 Posted October 22, 2019 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
Noél Dubau Posted October 22, 2019 Author Posted October 22, 2019 (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 Edited October 22, 2019 by Noél Dubau
john renfrew Posted October 22, 2019 Posted October 22, 2019 (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 October 22, 2019 by john renfrew
Noél Dubau Posted October 22, 2019 Author Posted October 22, 2019 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
john renfrew Posted October 22, 2019 Posted October 22, 2019 (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 October 22, 2019 by john renfrew
john renfrew Posted October 23, 2019 Posted October 23, 2019 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])
Noél Dubau Posted October 23, 2019 Author Posted October 23, 2019 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 !
john renfrew Posted October 23, 2019 Posted October 23, 2019 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...
Noél Dubau Posted October 23, 2019 Author Posted October 23, 2019 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 ☹️
john renfrew Posted October 23, 2019 Posted October 23, 2019 after the vsm_Image = line needs a ¶ at the end if you literally copy/paste
Noél Dubau Posted October 23, 2019 Author Posted October 23, 2019 I added that line... but now how to get the content of this block ? I join a screen copy of my script.
john renfrew Posted October 24, 2019 Posted October 24, 2019 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
Noél Dubau Posted October 24, 2019 Author Posted October 24, 2019 OK ! I'll try that way ! One more time thanks for your help. Noël
Noél Dubau Posted November 2, 2019 Author Posted November 2, 2019 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
Noél Dubau Posted November 14, 2019 Author Posted November 14, 2019 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now