2021-05-14 11:09:21 +03:00
|
|
|
import json
|
|
|
|
import rand
|
|
|
|
import net.http
|
|
|
|
|
|
|
|
struct Weather {
|
|
|
|
status string [skip] // drop this field
|
|
|
|
api_version string [skip]
|
|
|
|
api_status string [skip]
|
|
|
|
lang string [skip]
|
|
|
|
unit string [skip]
|
|
|
|
tzshift int [skip]
|
|
|
|
timezone string [skip]
|
|
|
|
server_time u32 [skip]
|
|
|
|
location []f32 [skip]
|
|
|
|
result Result //[json: result] if the field name is different in JSON, it can be specified
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Result {
|
|
|
|
realtime Realtime [skip]
|
|
|
|
minutely Minutely [skip]
|
|
|
|
hourly Hourly [skip]
|
|
|
|
daily Daily [skip]
|
|
|
|
primary int [skip]
|
|
|
|
forecast_keypoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Realtime {}
|
|
|
|
|
|
|
|
struct Minutely {}
|
|
|
|
|
|
|
|
struct Hourly {}
|
|
|
|
|
|
|
|
struct Daily {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
config := http.FetchConfig{
|
|
|
|
user_agent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0'
|
|
|
|
}
|
|
|
|
|
|
|
|
rnd := rand.f32()
|
2022-11-15 16:53:13 +03:00
|
|
|
url := 'https://api.caiyunapp.com/v2.5/96Ly7wgKGq6FhllM/116.391912,40.010711/weather.jsonp?hourlysteps=120&random=${rnd}'
|
2021-05-14 11:09:21 +03:00
|
|
|
// println(url)
|
|
|
|
|
2021-08-17 18:36:10 +03:00
|
|
|
resp := http.fetch(http.FetchConfig{ ...config, url: url }) or {
|
2021-05-14 11:09:21 +03:00
|
|
|
println('failed to fetch data from the server')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-29 20:27:18 +03:00
|
|
|
weather := json.decode(Weather, resp.body) or {
|
2021-05-14 11:09:21 +03:00
|
|
|
println('failed to decode weather json')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
println('未来两小时天气:\n${weather.result.forecast_keypoint}.')
|
|
|
|
}
|