Initial project import

This commit is contained in:
Agila Forge Importer committed 2026-07-13 15:20:32 +00:00
commit 23f2d29e32
11957 files changed
+1754577

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
# get_weather
get_weather is a web crawler. Its goal is to get a weather forecast from
`https://api.caiyunapp.com` in chinese and translated to another selected language.
We use `http.fetch()` to get a forecast `http.Response`, with a custom user-agent
and then we decode the json into a struct with only relevant fields `lang`,
`result` and `forecast_keypoint`.
The chinese texts are translated with another `http.fetch()` to
`https://translate.googleapis.com` and then decoding the `http.Response` as `json2.Any` arrays.
## running
By default texts are translated to English. Another language can be indicated
with first argument as an [ISO 639](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes) code:
```bash
$ v run examples/get_weather/get_weather.v
zh_CN: 未来两小时天气
en: Weather in the next two hours
zh_CN: 最近的降雨带在西北66公里外呢
en: The nearest rainfall zone is 66 kilometers northwest
$
$ v run examples/get_weather/get_weather.v es
zh_CN: 未来两小时天气
es: Clima en las próximas dos horas
zh_CN: 最近的降雨带在西北66公里外呢
es: La zona de lluvia más cercana está a 66 kilómetros al noroeste
```
@@ -0,0 +1,68 @@
import net.http
import os
import rand
import x.json2 as json
struct Weather {
lang string
result Result
}
struct Result {
forecast_keypoint string
}
const config = http.FetchConfig{
user_agent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0'
}
fn main() {
dest_lang := if os.args.len > 1 { os.args[1] } else { 'en' }
rnd := rand.f32()
url := 'https://api.caiyunapp.com/v2.5/96Ly7wgKGq6FhllM/116.391912,40.010711/weather.jsonp?hourlysteps=120&random=${rnd}'
resp := http.fetch(http.FetchConfig{ ...config, url: url }) or {
println('failed to fetch data from the server')
return
}
weather := json.decode[Weather](resp.body) or {
println('failed to decode weather json')
return
}
for ch in ['未来两小时天气', weather.result.forecast_keypoint] {
println('${weather.lang:8}: ${ch}')
t := translate(ch, weather.lang, dest_lang) or {
println('failed to translate ${ch}: ${err}')
continue
}
println('${dest_lang:8}: ${t}')
}
}
// translate fetch google to print translate text `q` in languate `sl` into language `tl`.
// A translation typical response json is like: `[[["Weather in the next two hours",
// "未来两小时天气",null,null,3,null,null,[[null,"offline"]],
// [[["61549914d65604307a34fd1855292577","offline_launch_doc.md"],null,null,null,null,
// [[[6,8,0]]]]]]],null,"zh-CN",null,null,null,null,[]]`
// where translated text is located at position `json_resp[0][0][0]`.
fn translate(q string, sl string, tl string) !string {
url := 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${q}'
resp := http.fetch(http.FetchConfig{ ...config, url: url })!
json_resp := json.decode[json.Any](resp.body)!
a := json_resp.as_array()
if a.len > 0 {
a0 := a[0].as_array()
if a0.len > 0 {
a00 := a0[0].as_array()
if a00.len > 0 {
return a00[0].str()
}
}
}
return error('invalid translation response')
}