Just starting out? Need help? Post your questions and find answers here.
PowerSoft
User
Posts: 65 Joined: Sun Aug 16, 2015 2:54 pm
Post
by PowerSoft » Thu Dec 05, 2019 4:46 pm
Hello,
I try to read data from the openweathermap.org. Have good results with python.
I use this simple python code:
Code: Select all
# Enter your API key here
api_key = "e8b010e1ab578d6426ad5a9359f636c1"
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
#city_name = input("Enter city name : ")
city_name = "hellevoetsluis"
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
# get method of requests module
# return response object
response = requests.get(complete_url)
I try the same in Purebasic with no results.
This is the code:
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Call OpenWeathermap.org
;
; http://api.openweathermap.org/Data/2.5/weather?appid=e8b010e1ab578d6426ad5a9359f636c1&q=hellevoetsluis
;
; (c) Jan Kromhout
;
; ------------------------------------------------------------
;
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, "http://api.openweathermap.org/Data/2.5/weather?appid=e8b010e1ab578d6426ad5a9359f636c1&q=hellevoetsluis")
If HttpRequest
Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
What is wrong with the Purebasic code?
Thanks for any help.
Jan Kromhout
Hellevoetsluis-NL
OS X 10.10.5 PB 5.31(x64)
infratec
Always Here
Posts: 6883 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Thu Dec 05, 2019 5:19 pm
Code: Select all
; Enter your API key here
#api_key = "e8b010e1ab578d6426ad5a9359f636c1"
; base_url variable To store url
#base_url = "http://api.openweathermap.org/data/2.5/weather?"
; Give city name
#city_name = "hellevoetsluis"
#complete_url = #base_url + "appid=" + #api_key + "&q=" + #city_name
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, #complete_url)
If HttpRequest
Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
You missed the query
kenmo
Addict
Posts: 1967 Joined: Tue Dec 23, 2003 3:54 am
Post
by kenmo » Thu Dec 05, 2019 5:34 pm
Their URL is case-sensitive.
'data' got changed to 'Data' ***
Change it back to '/data/' and it works fine.
*** I'm guessing it got changed by the IDE's aggressive case correction (Data is a keyword) which I would like to change now that the IDE is open source
infratec
Always Here
Posts: 6883 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Thu Dec 05, 2019 5:37 pm
Since you will ask later ...
Code: Select all
EnableExplicit
; Enter your API key here
#api_key = "e8b010e1ab578d6426ad5a9359f636c1"
; base_url variable To store url
#base_url = "http://api.openweathermap.org/data/2.5/weather?"
; Give city name
#city_name = "hellevoetsluis"
#complete_url = #base_url + "appid=" + #api_key + "&q=" + #city_name
Structure coordStructure
lon.f
lat.f
EndStructure
Structure weatherList
id.i
main.s
description.s
icon.s
EndStructure
Structure mainStructure
temp.f
pressure.i
humidity.i
temp_min.f
temp_max.f
EndStructure
Structure windStructure
speed.f
deg.i
EndStructure
Structure cloudStructure
all.i
EndStructure
Structure sysStructure
type.i
id.i
country.s
sunrise.i
sunset.i
EndStructure
Structure WeatherStructure
coord.coordStructure
List weather.weatherList()
base.s
main.mainStructure
visibility.i
wind.windStructure
clouds.cloudStructure
dt.i
sys.sysStructure
timezone.i
id.i
name.s
cod.i
EndStructure
Define HTTPRequest.i, JSON$, JSON.i
Define Weather.WeatherStructure
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, #complete_url)
If HttpRequest
If HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode) = "200"
JSON$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
Debug "Response: " + JSON$
Debug ""
JSON = ParseJSON(#PB_Any, JSON$)
If JSON
ExtractJSONStructure(JSONValue(JSON), @Weather, WeatherStructure)
Debug "Lon: " + StrF(Weather\coord\lon, 2)
Debug "Lat: " + StrF(Weather\coord\lat, 2)
ForEach Weather\weather()
Debug "Description: " + Weather\weather()\description
Next
Debug Weather\sys\country + " " + Weather\name
Debug "Temp: " + StrF(Weather\main\temp, 2)
Debug "Windspeed: " + Str(Weather\wind\speed)
Debug "Cloud: " + Str(Weather\clouds\all)
FreeJSON(JSON)
EndIf
Else
MessageRequester("Error", "GET failed")
EndIf
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
PowerSoft
User
Posts: 65 Joined: Sun Aug 16, 2015 2:54 pm
Post
by PowerSoft » Thu Dec 05, 2019 6:13 pm
Thanks all for the help.
The code was verry helpfull.
CHEERS
OS X 10.10.5 PB 5.31(x64)
l1marik
User
Posts: 49 Joined: Tue Jun 30, 2020 6:22 am
Post
by l1marik » Tue Dec 29, 2020 4:11 pm
I updated previous code from infratec to use onecall functionality.
Code: Select all
EnableExplicit
; Enter your API key here
#api_key = "e8b010e1ab578d6426ad5a9359f636c1"
; base_url variable To store url
#base_url = "http://api.openweathermap.org/data/2.5/onecall?lat=49.5780410767&lon=17.4841003418&"
#complete_url = #base_url + "appid=" + #api_key + "&units=metric&lang=cz"
Debug #complete_url
Structure OWMWeatherList
id.i
main.s
description.s
icon.s
EndStructure
Structure OWMWeatherMinutelyList
dt.i
precipitation.f
EndStructure
Structure OWMWeatherStructure_current
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
List weather.OWMWeatherList()
EndStructure
Structure OWMWeatherStructure_hourly
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
weather.OWMWeatherList
pop.f
EndStructure
Structure OWMDailyTemp
day.f
min.f
max.f
night.f
eve.f
morn.f
EndStructure
Structure OWMDailyFeels_Like
day.f
night.f
eve.f
morn.f
EndStructure
Structure OWMWeatherStructure_daily
dt.i
sunrise.i
sunset.i
temp.OWMDailyTemp
feels_like.OWMDailyFeels_Like
pressure.i
humidity.i
dew_point.f
wind_speed.f
wind_deg.i
List weather.OWMWeatherList()
clouds.i
pop.f
rain.f
snow.f
uvi.f
EndStructure
Structure OWMAlerts
sender_name.s
event.s
start.i
End.i
description.s
EndStructure
Structure OWMWeatherCompleteStructure
lat.f
lon.f
timezone.s
timezone_offset.i
current.OWMWeatherStructure_current
List minutely.OWMWeatherMinutelyList()
List hourly.OWMWeatherStructure_hourly()
List daily.OWMWeatherStructure_daily()
List alerts.OWMAlerts()
EndStructure
Define HTTPRequest.i, JSON$, JSON.i
Define Weather.OWMWeatherCompleteStructure
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, #complete_url)
If HttpRequest
If HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode) = "200"
JSON$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
Debug "Response: " + JSON$
Debug ""
JSON = ParseJSON(#PB_Any, JSON$)
If JSON
ExtractJSONStructure(JSONValue(JSON), @Weather, OWMWeatherCompleteStructure)
Debug weather\timezone
ForEach Weather\minutely()
Debug "Date: " + FormatDate("%dd.%mm. %hh:%ii", Weather\minutely()\dt + Weather\timezone_offset) + " - " + Weather\minutely()\precipitation
Next
FreeJSON(JSON)
EndIf
Else
MessageRequester("Error", "GET failed")
EndIf
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
infratec
Always Here
Posts: 6883 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Tue Dec 29, 2020 5:19 pm
found a small bug:
Code: Select all
Structure OWMWeatherStructure_hourly
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
weather.OWMWeatherList
pop.f
EndStructure
should be
Code: Select all
Structure OWMWeatherStructure_hourly
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
List weather.OWMWeatherList()
pop.f
EndStructure
l1marik
User
Posts: 49 Joined: Tue Jun 30, 2020 6:22 am
Post
by l1marik » Tue Jan 05, 2021 7:58 pm
I found not bug, but issue with name of one value in OWM JSON. In alert is one value named as reserved word end . Is way to solve it?
Lukas
infratec
Always Here
Posts: 6883 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Tue Jan 05, 2021 8:21 pm
The only trick I know:
Code: Select all
JSON$ = ReplaceString(JSON$, #DQUOTE + "end" + #DQUOTE, #DQUOTE + "_end" + #DQUOTE)