Shell and JSON
Posted
#1
(In Topic #1519)
Regular

To the point: What is the best way to handle Shell when it doesn't return data or something else goes wrong?
I am adding current weather and forecasts to my movies/streaming app…totally unnecessary but I'm gonna do it anyways
I have previously used openweathermap although I have never tried to integrate into a Gambas. Openweathermap is now charging for access to the api and I'm not going to pay :x
A few days ago I found openmeteo and it's free (at least until it's not), and I have been learning how to use the api and I am now working to integrate into a form using Shell. The api is fast but it does seem to just not respond sometimes and I don't know why….that also sometimes happens when using a browser too. When it does not respond with JSON data it makes the form unresponsive/locked up. I do not get the message box Error at all, it will hang until I stop it from the IDE. I do have error handling plus I added a Print to help me see what is going on. When it's not returning data I get this in the IDE console:
Code (gambas)
- (JSONCollection 0x5604107aa548)
Here's how I'm getting the data: (from Gambas One member SteveDee's website that I stumbled onto and added my own error handling)
Code (gambas)
- '????
- 'Print "Temp: ", collJson["current"]["temperature_2m"]
- label1.Text = collJson["current"]["time"]
- label3.Text = collJson["current"]["temperature_2m"] & " F"
- weathercode.Value = collJson["current"]["weather_code"]
- label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
- label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
- 'there's more code here that deals with what weather_code is returned
Posted
Administrator



Code (gambas)
- sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles¤t=temperature_2m¤t=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
- 'Print "Temp: ", collJson["current"]["temperature_2m"]
- label1.Text = collJson["current"]["time"]
- label3.Text = collJson["current"]["temperature_2m"] & " F"
- weathercode.Value = collJson["current"]["weather_code"]
- label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
- label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
- 'there's more code here that deals with what weather_code is returned
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Regular

Current Weather
Code (gambas)
- https://api.openweathermap.org/data/2.5/weather?lat=&1&lon=&2&appid=&3&units=&4&lang=&5
Code (gambas)
- https://api.openweathermap.org/data/2.5/forecast?lat=&1&lon=&2&appid=&3&units=&4&lang=&5
Posted
Regular

gbWilly said
I haven't tested below, just a little re-code to make sure if you get no result returned is taken care of and using a special manner (learned from master Benoit himself when I had an issue with Shell) of using Exec, that usually does the job. Just give it a try, I wrote it right here so typo's might have occured.If I have time I will try it myself, but a bit to busy ATM.Code (gambas)
sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles¤t=temperature_2m¤t=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'" 'Print "Temp: ", collJson["current"]["temperature_2m"] label1.Text = collJson["current"]["time"] label3.Text = collJson["current"]["temperature_2m"] & " F" weathercode.Value = collJson["current"]["weather_code"] label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"] label6.Text = "Precipitation: " & collJson["current"]["precipitation"] 'there's more code here that deals with what weather_code is returned
Thanks gbWilly, I will test here shortly.
Posted
Regular

thatbruce said
OpenWeatherMap is still free. The URLs are
Current WeatherForecastCode (gambas)
https://api.openweathermap.org/data/2.5/weather?lat=&1&lon=&2&appid=&3&units=&4&lang=&5Current Weather_001.pngCode (gambas)
https://api.openweathermap.org/data/2.5/forecast?lat=&1&lon=&2&appid=&3&units=&4&lang=&5
Well yes technically it's still free but for 3.0 it seems you have to subscribe and give payment details even if you don't go over a limit which appears to be 1000 calls a day…I'm still not giving them my CC number
EDIT: And how long before they cutoff access to 2.5? Who knows. I'll just continue with openmeteo.
Posted
Regular

gbWilly said
I haven't tested below, just a little re-code to make sure if you get no result returned is taken care of and using a special manner (learned from master Benoit himself when I had an issue with Shell) of using Exec, that usually does the job. Just give it a try, I wrote it right here so typo's might have occured.If I have time I will try it myself, but a bit to busy ATM.Code (gambas)
sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles¤t=temperature_2m¤t=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'" 'Print "Temp: ", collJson["current"]["temperature_2m"] label1.Text = collJson["current"]["time"] label3.Text = collJson["current"]["temperature_2m"] & " F" weathercode.Value = collJson["current"]["weather_code"] label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"] label6.Text = "Precipitation: " & collJson["current"]["precipitation"] 'there's more code here that deals with what weather_code is returned
Working so far. I'm testing over and over until it does it's thing where is doesn't respond with data…I'll post back when that happens.
Posted
Administrator



Nice…rj71 said
Working so far. I'm testing over and over until it does it's thing where is doesn't respond with data…I'll post back when that happens.
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Regular

rj71 said
gbWilly said
I haven't tested below, just a little re-code to make sure if you get no result returned is taken care of and using a special manner (learned from master Benoit himself when I had an issue with Shell) of using Exec, that usually does the job. Just give it a try, I wrote it right here so typo's might have occured.If I have time I will try it myself, but a bit to busy ATM.Code (gambas)
sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles¤t=temperature_2m¤t=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'" 'Print "Temp: ", collJson["current"]["temperature_2m"] label1.Text = collJson["current"]["time"] label3.Text = collJson["current"]["temperature_2m"] & " F" weathercode.Value = collJson["current"]["weather_code"] label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"] label6.Text = "Precipitation: " & collJson["current"]["precipitation"] 'there's more code here that deals with what weather_code is returned
Working so far. I'm testing over and over until it does it's thing where is doesn't respond with data…I'll post back when that happens.
Just happened about 2/10 times I tested. Same result, form freezes and your error message did not show. I got that error again in the console: (JSONCollection 0x5590da9747d8)
Posted
Regular

Posted
Administrator



Try this:rj71 said
Just happened about 2/10 times I tested. Same result, form freezes and your error message did not show. I got that error again in the console: (JSONCollection 0x5590da9747d8)
Code (gambas)
- sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles¤t=temperature_2m¤t=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
- 'Print "Temp: ", collJson["current"]["temperature_2m"]
- label1.Text = collJson["current"]["time"]
- label3.Text = collJson["current"]["temperature_2m"] & " F"
- weathercode.Value = collJson["current"]["weather_code"]
- label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
- label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
- 'there's more code here that deals with what weather_code is returned
- $sOut &= MyError
- $hProcess.Kill
- $bError = True
- Debug "\n--End of process--\n"
Warning Not tested in IDE, but typed here as is…
…while waiting for my oldie to be finished installing.
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Regular

gbWilly said
Try this:rj71 said
Just happened about 2/10 times I tested. Same result, form freezes and your error message did not show. I got that error again in the console: (JSONCollection 0x5590da9747d8)
It should give you some status on the error happening (I hope..Code (gambas)
sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles¤t=temperature_2m¤t=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'" 'Print "Temp: ", collJson["current"]["temperature_2m"] label1.Text = collJson["current"]["time"] label3.Text = collJson["current"]["temperature_2m"] & " F" weathercode.Value = collJson["current"]["weather_code"] label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"] label6.Text = "Precipitation: " & collJson["current"]["precipitation"] 'there's more code here that deals with what weather_code is returned $sOut &= MyError $hProcess.Kill $bError = True Debug "\n--End of process--\n")
Warning Not tested in IDE, but typed here as is…
…while waiting for my oldie to be finished installing.
Thanks gbWilly, I'll have to try it tomorrow though. My gf wants to go out for a really early dinner to her favorite place. I'll check back in tomorrow!
Posted
Banned
I'd do it like this…
Code (gambas)
- sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles¤t=temperature_2m¤t=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
I know using To implies Wait but i like to also use Wait as i think i have found it not working as expected with just To
Posted
Banned
Posted
Banned
Shell "command 2>&1" Wait To Result
or..
That diverts any stderr messages to stdout (your Result String)
Posted
Regular

(If I had a beer for every time that I've typed
Code (gambas)
- &2>1
Yes, I know, I'm adding it to my IDE shortcuts now. :?
1 guest and 0 members have just viewed this.



