MirrorBar+ part 2: tray, weather, radar, local news data
This commit is contained in:
+22
@@ -123,11 +123,15 @@ func weatherSetZip(z string) {
|
||||
_ = os.Remove(locCacheFile())
|
||||
}
|
||||
weatherRefresh(true)
|
||||
newsRefresh(true) // the local news follows the same ZIP code
|
||||
}
|
||||
|
||||
// weatherRefresh starts a fetch if the forecast is missing or old (or force is
|
||||
// set). Safe to call as often as you like.
|
||||
func weatherRefresh(force bool) {
|
||||
if !showWeather {
|
||||
return
|
||||
}
|
||||
zip := wxZip
|
||||
wxMu.Lock()
|
||||
if len(zip) != 5 {
|
||||
@@ -230,6 +234,24 @@ func wxGetJSON(u string, v any) (status int, err error) {
|
||||
return resp.StatusCode, json.NewDecoder(io.LimitReader(resp.Body, 2<<20)).Decode(v)
|
||||
}
|
||||
|
||||
// wxGetBytes downloads one file (a picture tile), up to 4 MB.
|
||||
func wxGetBytes(u string) ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", "MirrorBar weather flyout")
|
||||
resp, err := wxClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
return io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
}
|
||||
|
||||
// wxLocate turns a ZIP code into a place. The answer is kept in a small file so
|
||||
// the lookup happens once per ZIP code, not every time the bar starts.
|
||||
func wxLocate(zip string) (lat, lon float64, place string, err error) {
|
||||
|
||||
Reference in New Issue
Block a user