aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/examples/get_weather/get_weather.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/examples/get_weather/get_weather.v
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/examples/get_weather/get_weather.v')
-rw-r--r--v_windows/v/examples/get_weather/get_weather.v55
1 files changed, 55 insertions, 0 deletions
diff --git a/v_windows/v/examples/get_weather/get_weather.v b/v_windows/v/examples/get_weather/get_weather.v
new file mode 100644
index 0000000..4de0572
--- /dev/null
+++ b/v_windows/v/examples/get_weather/get_weather.v
@@ -0,0 +1,55 @@
+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()
+ url := 'https://api.caiyunapp.com/v2.5/96Ly7wgKGq6FhllM/116.391912,40.010711/weather.jsonp?hourlysteps=120&random=$rnd'
+ // println(url)
+
+ resp := http.fetch(http.FetchConfig{ ...config, url: url }) or {
+ println('failed to fetch data from the server')
+ return
+ }
+
+ weather := json.decode(Weather, resp.text) or {
+ println('failed to decode weather json')
+ return
+ }
+
+ println('未来两小时天气:\n${weather.result.forecast_keypoint}.')
+}