Shell and JSON

Post

Posted
Rating:
#1 (In Topic #1519)
Regular
rj71 is in the usergroup ‘Regular’
Hi All,

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  :lol:
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)

  1. (JSONCollection 0x5604107aa548)
  2.  
Google doesn't give me anything on that error.
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)

  1. Dim collJson As Collection
  2.  
  3. Try Shell "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'" To Result
  4.         '????
  5.       Message("Error")
  6.   collJson = Json.decode(Result, True)
  7.   'Print "Temp: ", collJson["current"]["temperature_2m"]
  8.   label1.Text = collJson["current"]["time"]
  9.   label3.Text = collJson["current"]["temperature_2m"] & " F"
  10.   weathercode.Value = collJson["current"]["weather_code"]
  11.   label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
  12.   label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
  13.   'there's more code here that deals with what weather_code is returned
  14.  
  15.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’
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.

Code (gambas)

  1. Dim collJson As Collection
  2. Dim sCommand As String
  3.  
  4. sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
  5.  
  6. Try Exec [System.Shell, "-c", sCommand] To Result
  7.   Message.Info("Error or empty result returned")
  8.   collJson = Json.decode(Result, True)
  9.   'Print "Temp: ", collJson["current"]["temperature_2m"]
  10.   label1.Text = collJson["current"]["time"]
  11.   label3.Text = collJson["current"]["temperature_2m"] & " F"
  12.   weathercode.Value = collJson["current"]["weather_code"]
  13.   label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
  14.   label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
  15.   'there's more code here that deals with what weather_code is returned
  16.  
  17.  
If I have time I will try it myself, but a bit to busy ATM.

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!
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
OpenWeatherMap is still free. The URLs are
Current Weather

Code (gambas)

  1. https://api.openweathermap.org/data/2.5/weather?lat=&1&lon=&2&appid=&3&units=&4&lang=&5
Forecast

Code (gambas)

  1. https://api.openweathermap.org/data/2.5/forecast?lat=&1&lon=&2&appid=&3&units=&4&lang=&5
Image

(Click to enlarge)


Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
rj71 is in the usergroup ‘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.

Code (gambas)

  1. Dim collJson As Collection
  2. Dim sCommand As String
  3.  
  4. sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
  5.  
  6. Try Exec [System.Shell, "-c", sCommand] To Result
  7.   Message.Info("Error or empty result returned")
  8.   collJson = Json.decode(Result, True)
  9.   'Print "Temp: ", collJson["current"]["temperature_2m"]
  10.   label1.Text = collJson["current"]["time"]
  11.   label3.Text = collJson["current"]["temperature_2m"] & " F"
  12.   weathercode.Value = collJson["current"]["weather_code"]
  13.   label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
  14.   label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
  15.   'there's more code here that deals with what weather_code is returned
  16.  
  17.  
If I have time I will try it myself, but a bit to busy ATM.

Thanks gbWilly, I will test here shortly.
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
rj71 is in the usergroup ‘Regular’

thatbruce said

OpenWeatherMap is still free. The URLs are
Current Weather

Code (gambas)

  1. https://api.openweathermap.org/data/2.5/weather?lat=&1&lon=&2&appid=&3&units=&4&lang=&5
Forecast

Code (gambas)

  1. https://api.openweathermap.org/data/2.5/forecast?lat=&1&lon=&2&appid=&3&units=&4&lang=&5
Current Weather_001.png

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  :lol:  I started looking elsewhere because a php script using openweathermap 2.5 I have suddenly stopped giving me high and low temps in the forecast. I haven't touched that script in years so I'm still trying to figure out what's wrong with it. I'm looking at it now but since I have now spent a couple days with open-meteo, I'll stick with it unless it causes problems. Nevertheless, I'd love to learn a better way to handle the Shell when it doesn't do what you're expecting. Thanks Bruce!

EDIT: And how long before they cutoff access to 2.5? Who knows. I'll just continue with openmeteo.
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
rj71 is in the usergroup ‘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.

Code (gambas)

  1. Dim collJson As Collection
  2. Dim sCommand As String
  3.  
  4. sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
  5.  
  6. Try Exec [System.Shell, "-c", sCommand] To Result
  7.   Message.Info("Error or empty result returned")
  8.   collJson = Json.decode(Result, True)
  9.   'Print "Temp: ", collJson["current"]["temperature_2m"]
  10.   label1.Text = collJson["current"]["time"]
  11.   label3.Text = collJson["current"]["temperature_2m"] & " F"
  12.   weathercode.Value = collJson["current"]["weather_code"]
  13.   label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
  14.   label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
  15.   'there's more code here that deals with what weather_code is returned
  16.  
  17.  
If I have time I will try it myself, but a bit to busy ATM.

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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

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.
Nice…  ;)

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!
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
rj71 is in the usergroup ‘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.

Code (gambas)

  1. Dim collJson As Collection
  2. Dim sCommand As String
  3.  
  4. sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
  5.  
  6. Try Exec [System.Shell, "-c", sCommand] To Result
  7.   Message.Info("Error or empty result returned")
  8.   collJson = Json.decode(Result, True)
  9.   'Print "Temp: ", collJson["current"]["temperature_2m"]
  10.   label1.Text = collJson["current"]["time"]
  11.   label3.Text = collJson["current"]["temperature_2m"] & " F"
  12.   weathercode.Value = collJson["current"]["weather_code"]
  13.   label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
  14.   label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
  15.   'there's more code here that deals with what weather_code is returned
  16.  
  17.  
If I have time I will try it myself, but a bit to busy ATM.

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)
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
rj71 is in the usergroup ‘Regular’
 It does seem I'm getting (JSONCollection 0x5590da9747d8) in the console even if it is successful and returns data. My bad on that…I didn't notice it.
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
gbWilly leads the usergroup ‘GambOS Contributor’
gbWilly is in the usergroup ‘Blogger’

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)
Try this:

Code (gambas)

  1. Private $hProcess As Process
  2.  
  3. Public Sub GetData()
  4.  
  5.   Dim collJson As Collection
  6.   Dim sCommand As String
  7.  
  8.   sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
  9.  
  10.   $hProcess = Exec [System.Shell, "-c", sCommand] Wait For Input As "_Process"
  11.  
  12.   If Not $bError Then
  13.     collJson = Json.decode($sOut, True)
  14.     'Print "Temp: ", collJson["current"]["temperature_2m"]
  15.     label1.Text = collJson["current"]["time"]
  16.     label3.Text = collJson["current"]["temperature_2m"] & " F"
  17.     weathercode.Value = collJson["current"]["weather_code"]
  18.     label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
  19.     label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
  20.     'there's more code here that deals with what weather_code is returned
  21.   Else
  22.     Message.Error($sOut)
  23.  
  24.  
  25. Public Sub _Process_Read()
  26.  
  27.   Dim sLine As String
  28.  
  29.   sLine = Read #Last, Lof(Last)
  30.   If Not IsNull(Trim(sLine)) Then
  31.     $sOut &= Trim(sLine) & "\n"
  32.  
  33.  
  34. Public Sub _Process_Error(MyError As String)
  35.  
  36.   $sOut &= MyError
  37.   If Not IsNull($hProcess) Then
  38.     $hProcess.Kill
  39.  $bError = True
  40.  
  41.  
  42. Public Sub _Process_Kill()
  43.    
  44.   Debug "\n--End of process--\n"
  45.  
  46.  
It should give you some status on the error happening (I hope.. ;) )

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!
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
rj71 is in the usergroup ‘Regular’

gbWilly said

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)
Try this:

Code (gambas)

  1. Private $hProcess As Process
  2.  
  3. Public Sub GetData()
  4.  
  5.   Dim collJson As Collection
  6.   Dim sCommand As String
  7.  
  8.   sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
  9.  
  10.   $hProcess = Exec [System.Shell, "-c", sCommand] Wait For Input As "_Process"
  11.  
  12.   If Not $bError Then
  13.     collJson = Json.decode($sOut, True)
  14.     'Print "Temp: ", collJson["current"]["temperature_2m"]
  15.     label1.Text = collJson["current"]["time"]
  16.     label3.Text = collJson["current"]["temperature_2m"] & " F"
  17.     weathercode.Value = collJson["current"]["weather_code"]
  18.     label5.Text = "Wind Speed: " & collJson["current"]["wind_speed_10m"]
  19.     label6.Text = "Precipitation: " & collJson["current"]["precipitation"]
  20.     'there's more code here that deals with what weather_code is returned
  21.   Else
  22.     Message.Error($sOut)
  23.  
  24.  
  25. Public Sub _Process_Read()
  26.  
  27.   Dim sLine As String
  28.  
  29.   sLine = Read #Last, Lof(Last)
  30.   If Not IsNull(Trim(sLine)) Then
  31.     $sOut &= Trim(sLine) & "\n"
  32.  
  33.  
  34. Public Sub _Process_Error(MyError As String)
  35.  
  36.   $sOut &= MyError
  37.   If Not IsNull($hProcess) Then
  38.     $hProcess.Kill
  39.  $bError = True
  40.  
  41.  
  42. Public Sub _Process_Kill()
  43.    
  44.   Debug "\n--End of process--\n"
  45.  
  46.  
It should give you some status on the error happening (I hope.. ;) )

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!
Online now: No Back to the top

Post

Posted
Rating:
#12
Banned
I don't get the command using sh -c , why not just use the command?

I'd do it like this…

Code (gambas)

  1. sCommand = "curl 'https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation,rain,weather_code,showers,snowfall,wind_speed_10m&precipitation_unit=inch&temperature_unit=fahrenheit'"
  2.  
  3. Try Exec [sCommand] Wait To Result
  4.  
  5.  

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
Online now: No Back to the top

Post

Posted
Rating:
#13
Banned
or better still, do not use the curl command with Exec but use the gambas component gb.net.curl and HttpClient.Download()

Code (gambas)

  1.  
  2.  
  3. Try Result = HttpClient.Download("https://api.open-meteo.com/v1/forecast?latitude=45.633331&longitude=-122.599998&timezone=America%2FLos_Angeles&current=temperature_2m&current=precipitation")
  4.  
  5.  
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#14
Banned
A simple trick more relevant to your original question is to use 2>&1 with Shell

Shell "command 2>&1" Wait To Result
or..

Code (gambas)

  1. sCommand = "curl http://blah/blah.com"
  2. Shell sCommand & " 2>&1" Wait To Result
  3.  
  4. If Error Or if Process.LastValue then  ' Process.LastValue is non zero if curl exits with an error code.
  5.  Message("error getting data" & if(Process.LastValue, Result, Error.Text))
  6.  
That diverts any stderr messages to stdout (your Result String)
Online now: No Back to the top

Post

Posted
Rating:
#15
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
There should be some kind of short-key for that!!!!!!! It really helps on the occasion and a "friendly reminder" of that GAbash syntax would be soooooo helpful.
(If I had a beer for every time that I've typed

Code (gambas)

  1. &2>1
I'd be slightly more than immoderately diagonal.)

Yes, I know, I'm adding it to my IDE shortcuts now.  :?

Online now: No Back to the top
1 guest and 0 members have just viewed this.