-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
img.go
109 lines (91 loc) · 1.92 KB
/
img.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package weatherboard
import (
"bytes"
"context"
"embed"
_ "embed"
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"strings"
"github.com/robbydyer/sports/internal/logo"
)
//go:embed assets
var assets embed.FS
func cacheDir() (string, error) {
d := "/tmp/sportsmatrix_logos/weathericons"
if _, err := os.Stat(d); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(d, 0o755); err != nil {
return "", err
}
return d, nil
}
}
return d, nil
}
func customImgSource(iconCode string) (logo.SourceGetter, error) {
f := ""
// These conditions match https://openweathermap.org/weather-conditions
switch strings.ToLower(iconCode) {
case "01d":
f = "sun.png"
case "01n":
f = "moon.png"
case "02d":
f = "partcloud.png"
case "02n":
f = "partcloud.png"
case "03d", "03n", "04d", "04n":
f = "cloudy.png"
case "09d", "09n", "10d", "10n":
f = "rain.png"
case "11d", "11n":
f = "storm.png"
case "13d", "13n":
f = "snowflake.png"
case "50d", "50n":
f = "mist.png"
default:
return nil, fmt.Errorf("no custom img source for %s", iconCode)
}
b, err := assets.ReadFile(filepath.Join("assets", f))
if err != nil {
return nil, err
}
r := bytes.NewReader(b)
return func(ctx context.Context) (image.Image, error) {
return png.Decode(r)
}, nil
}
func (w *WeatherBoard) customIcon(iconCode string, bounds image.Rectangle) (*logo.Logo, error) {
w.iconLock.Lock()
defer w.iconLock.Unlock()
key := fmt.Sprintf("%s_cust_%dx%d", iconCode, bounds.Dx(), bounds.Dy())
if i, ok := w.iconCache[key]; ok {
return i, nil
}
getter, err := customImgSource(iconCode)
if err != nil {
return nil, err
}
d, err := cacheDir()
if err != nil {
return nil, err
}
l := logo.New(key, getter, d, bounds, &logo.Config{
Abbrev: key,
XSize: bounds.Dx(),
YSize: bounds.Dy(),
Pt: &logo.Pt{
X: 0,
Y: 0,
Zoom: 1,
},
})
l.SetLogger(w.log)
w.iconCache[key] = l
return l, nil
}