Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(caiyun): use api resp tz to get local begin #5

Merged
merged 1 commit into from
Oct 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 55 additions & 9 deletions backends/caiyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

const (
CAIYUNAPI = "http://api.caiyunapp.com/v2.6/%s/%s/weather?lang=%s&dailysteps=%s&alert=true&unit=metric:v2"
CAIYUNAPI = "http://api.caiyunapp.com/v2.6/%s/%s/weather?lang=%s&dailysteps=%s&hourlysteps=%s&alert=true&unit=metric:v2&begin=%s"
CAIYUNDATE_TMPL = "2006-01-02T15:04-07:00"
)

Expand Down Expand Up @@ -77,25 +77,71 @@ func ParseCoordinates(latlng string) (float64, float64, error) {
return lat, lng, nil
}

func (c *CaiyunConfig) Fetch(location string, numdays int) iface.Data {
res := iface.Data{}
lat, lng, err := ParseCoordinates(location)
func (c *CaiyunConfig) GetWeatherDataFromLocalBegin(lng float64, lat float64, numdays int) (*CaiyunWeather, error) {
cyLocation := fmt.Sprintf("%v,%v", lng, lat)

localBegin, err := func() (*time.Time, error) {
now := time.Now()
url := fmt.Sprintf(
CAIYUNAPI, c.apiKey, cyLocation, c.lang,
strconv.FormatInt(int64(numdays), 10), strconv.FormatInt(int64(numdays)*24, 10),
strconv.FormatInt(now.Unix(), 10),
)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
weatherData := &CaiyunWeather{}
if err := json.Unmarshal(body, weatherData); err != nil {
return nil, err
}

loc, err := time.LoadLocation(weatherData.Timezone)
if err != nil {
panic(err)
}
localNow := now.In(loc)
localBegin := time.Date(localNow.Year(), localNow.Month(), localNow.Day(), 0, 0, 0, 0, loc)
return &localBegin, nil
}()
if err != nil {
panic(err)
return nil, err
}
cyLocation := fmt.Sprintf("%v,%v", lng, lat)
url := fmt.Sprintf(CAIYUNAPI, c.apiKey, cyLocation, c.lang, strconv.FormatInt(int64(numdays), 10))

url := fmt.Sprintf(
CAIYUNAPI, c.apiKey, cyLocation, c.lang,
strconv.FormatInt(int64(numdays), 10), strconv.FormatInt(int64(numdays)*24, 10),
strconv.FormatInt(localBegin.Unix(), 10),
)
resp, err := http.Get(url)
if err != nil {
panic(err)
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
return nil, err
}
weatherData := &CaiyunWeather{}
if err := json.Unmarshal(body, weatherData); err != nil {
return nil, err
}
return weatherData, nil
}

func (c *CaiyunConfig) Fetch(location string, numdays int) iface.Data {
res := iface.Data{}
lat, lng, err := ParseCoordinates(location)
if err != nil {
panic(err)
}
weatherData, err := c.GetWeatherDataFromLocalBegin(lng, lat, numdays)
if err != nil {
panic(err)
}
res.Current.Desc = weatherData.Result.Minutely.Description + "\t" + weatherData.Result.Hourly.Description
Expand Down