weather-station/internal/tools/forecast_fetch.go

53 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tools
import (
"context"
"log"
"time"
"weatherstation/internal/config"
"weatherstation/internal/forecast"
)
// RunForecastFetchForDay 按指定“日期”CST抓取当天0点到当前时间后三小时的预报Open-Meteo 与 彩云)
// dateStr: 形如 "2006-01-02"(按 Asia/Shanghai 解析)
func RunForecastFetchForDay(ctx context.Context, dateStr string) error {
loc, _ := time.LoadLocation("Asia/Shanghai")
if loc == nil {
loc = time.FixedZone("CST", 8*3600)
}
dayStart, err := time.ParseInLocation("2006-01-02", dateStr, loc)
if err != nil {
return err
}
now := time.Now().In(loc)
// 窗口dayStart .. now+3h 的各整点(写库函数内部按 issued_at=当前时间)
// 目前 open-meteo 与彩云抓取函数都是"取未来三小时"的固定窗口。
// 这里采用:先执行一次 open-meteo 与彩云的"未来三小时"写入,作为当下 issued 版本。
log.Printf("开始抓取 Open-Meteo 预报...")
if err := forecast.RunOpenMeteoFetch(ctx); err != nil {
log.Printf("Open-Meteo 抓取失败: %v", err)
} else {
log.Printf("Open-Meteo 抓取完成")
}
token := config.GetConfig().Forecast.CaiyunToken
log.Printf("彩云 token: %s", token)
if token != "" {
log.Printf("开始抓取彩云预报...")
if err := forecast.RunCaiyunFetch(ctx, token); err != nil {
log.Printf("彩云 抓取失败: %v", err)
} else {
log.Printf("彩云抓取完成")
}
} else {
log.Printf("未配置彩云 token跳过彩云抓取")
}
_ = dayStart
_ = now
return nil
}