-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
espnracing.go
99 lines (83 loc) · 1.79 KB
/
espnracing.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
package espnracing
import (
"bytes"
"context"
"embed"
"fmt"
"image"
"os"
"path/filepath"
"github.com/disintegration/imaging"
"go.uber.org/zap"
"github.com/robbydyer/sports/internal/logo"
)
//go:embed assets
var assets embed.FS
const cacheDir = "/tmp/sportsmatrix/racing"
// API ...
type API struct {
myLogo *image.Image
leaguer Leaguer
schedule *Scoreboard
log *zap.Logger
}
// Leaguer ...
type Leaguer interface {
ShortName() string
LogoSourceURL() string
HTTPPathPrefix() string
APIPath() string
LogoAsset() string
}
// New ...
func New(leaguer Leaguer, log *zap.Logger) (*API, error) {
return &API{
leaguer: leaguer,
log: log,
}, nil
}
// GetLogo ...
func (a *API) GetLogo(ctx context.Context, matrixBounds image.Rectangle) (*logo.Logo, error) {
return logo.New(
a.leaguer.ShortName(),
a.logoSourceGetter,
cacheDir,
matrixBounds,
&logo.Config{
FitImage: true,
Abbrev: a.leaguer.ShortName(),
XSize: matrixBounds.Dx(),
YSize: matrixBounds.Dy(),
Pt: &logo.Pt{
Zoom: 1,
},
},
), nil
}
// LeagueShortName ...
func (a *API) LeagueShortName() string {
return a.leaguer.ShortName()
}
// HTTPPathPrefix ...
func (a *API) HTTPPathPrefix() string {
return a.leaguer.HTTPPathPrefix()
}
func (a *API) logoSourceGetter(ctx context.Context) (image.Image, error) {
if a.myLogo != nil {
return *a.myLogo, nil
}
cacheFile := filepath.Join(cacheDir, a.leaguer.ShortName())
if _, err := os.Stat(cacheFile); err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to detect if logo cache exists: %w", err)
}
} else {
return imaging.Open(cacheFile)
}
b, err := assets.ReadFile(filepath.Join("assets", a.leaguer.LogoAsset()))
if err != nil {
return nil, err
}
reader := bytes.NewReader(b)
return imaging.Decode(reader)
}