Samuel Derenboim Posted August 14, 2023 Share Posted August 14, 2023 (edited) HI everyone, I've come across multiple marionette scripts that use rest api to retrieve json information. The caveat - they use python, that require additional libraries to use the GET function. my question - can vectorscript do something similar without additional libraries? I am looking to use an API to extract records from a remote web based json api. As it appears, vw script can open a web browser using the showwebdlg function. I was looking to see if I can pull records from json using vectorscript. Thank you in advance!! here is an example from @Marionette.NodeDefinition class Params(metaclass = Marionette.OrderedClass): #APPEARANCE #Name this = Marionette.Node( 'Pass' ) this.SetDescription( 'Do nothing node. It just passes the input to the output: y = x' ) #Input Ports x = Marionette.PortIn( [], 'item' ) x.SetDescription( "input" ) #OIP Controls #Output Ports y = Marionette.PortOut('item') y.SetDescription( "output" ) #BEHAVIOR #this.SetListAbsorb() def RunNode(self): import requests #script api_url = "https://jsonplaceholder.typicode.com/todos/1" response = requests.get(api_url) vs.Message(str(response.json())) x = 0 #outputs self.Params.y.value = x Edited August 14, 2023 by Samuel Derenboim Quote Link to comment
Pat Stanford Posted August 14, 2023 Share Posted August 14, 2023 As far as I know, Vectorscript does not have any ability to access web data. You will have to use Python for that functionality. 1 Quote Link to comment
Samuel Derenboim Posted August 19, 2023 Author Share Posted August 19, 2023 Thank you! Quote Link to comment
DomC Posted August 25, 2023 Share Posted August 25, 2023 On 8/14/2023 at 9:46 PM, Samuel Derenboim said: additional libraries Maybe not necessarely. urllib is a standard library and also should do the job. Give a usable feedback or your request: Quote Link to comment
DomC Posted August 25, 2023 Share Posted August 25, 2023 (edited) The forum allows me not to paste some code ... I think maybe for security reasons. Or your testing api request: import urllib.request import json api_url = "https://jsonplaceholder.typicode.com/todos/1" result_string = '' with urllib.request.urlopen(api_url) as f: result = json.loads(f.read().decode('utf-8')) vs.AlrtDialog(str(result)) An other real example with a http request url, if you can't get https to work import urllib.request import json import math #dezimalgrad in minuten und Sekunden umwandeln (copy paste aus dem Netz) def deg_to_dms(deg, type='lat'): deg = float(deg) decimals, number = math.modf(deg) d = int(number) m = int(decimals * 60) s = (deg - d - m / 60) * 3600.00 compass = { 'lat': ('N','S'), 'lon': ('E','W') } compass_str = compass[type][0 if d >= 0 else 1] return '{}º{}\'{:.2f}"{}'.format(abs(d), abs(m), abs(s), compass_str) h = vs.FSActLayer() x = 2600000 y = 1200000 z = 500 #Nimmt Position von aktiviertem Hilspunkt, Objekt oder Symbol sonst Bern Sternwarte als Vorgabewert if h != vs.Handle(0): x,y,z = vs.GetLocus3D(h) #vs.AlrtDialog(str(vs.GeogCoordToVWN(x, y))) vs.AlrtDialog(str(vs.VWCoordToGeog(x, y))) xPt, yPt, zPt = vs.PtDialog3D('LV95 Koordikante zu WGS84', x, y, z) #Abfragestring auf dem Geportal direkt im Internet url = "http://geodesy.geo.admin.ch/reframe/lv95towgs84?easting="+str(xPt)+"&northing="+str(yPt)+"&altitude="+str(zPt)+"&format=json" result_string = '' with urllib.request.urlopen(url) as f: result = json.loads(f.read().decode('utf-8')) #Antwort vom Geoportal interpretieren und umwandeln in json result_string = str(result['easting']) + '\r' + str(result['northing']) + '\r' + str(result['altitude'])+'\r\r'\ 'Grad: ' result_string += '\r' result_string += 'Grad Minuten Sekunden: \r' + deg_to_dms(result['easting'], 'lat') +' ' + deg_to_dms(result['easting'], 'lon') vs.AlrtDialog(str(result_string)) Edited August 25, 2023 by DomC Quote Link to comment
DomC Posted August 25, 2023 Share Posted August 25, 2023 (edited) OK, the part of the code which I will not paste is the part how you update your certificates by the script on mac computers to make python accept them to communicate over TLS (SSL) (https instead of http). caution should be exercised. Edited August 25, 2023 by DomC Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.