forked from Evidlo/remarkable_news
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
137 lines (127 loc) · 3.09 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"image"
"image/color"
"log"
"time"
"github.com/disintegration/imaging"
"github.com/golang/freetype/truetype"
"github.com/owulveryck/remarkable_news/provider"
"github.com/owulveryck/remarkable_news/provider/generic"
"github.com/sethvargo/go-envconfig"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
_ "golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/math/fixed"
)
type configuration struct {
Output string `env:"OUTPUT,required"`
UpdateFrequency time.Duration `env:"UPDATE_FREQUENCY,default=1h"`
HealthCheck time.Duration `env:"HEALTH_CHECK,default=30s"`
}
func main() {
var c configuration
ctx := context.Background()
l := envconfig.PrefixLookuper("RKNEWS_", envconfig.OsLookuper())
err := envconfig.ProcessWith(ctx, &c, l)
if err != nil {
log.Fatal(err)
}
err = run(ctx, c)
if err != nil {
log.Fatal(err)
}
}
func hasSlept(ctx context.Context, healthCheck time.Duration, sleptTime time.Duration) <-chan struct{} {
signalC := make(chan struct{})
go func(chan<- struct{}) {
tick := time.NewTicker(healthCheck)
last := time.Now()
for {
select {
case <-ctx.Done():
return
case <-tick.C:
if time.Now().Round(0).Sub(last) > sleptTime {
signalC <- struct{}{}
}
last = time.Now()
}
}
}(signalC)
return signalC
}
func run(ctx context.Context, c configuration) error {
prvd := &generic.Provider{}
prvd.Configure(ctx)
ticker := time.NewTicker(c.UpdateFrequency)
err := runProvider(ctx, c, ticker, prvd)
if err != nil {
log.Fatal(err)
}
runC := make(chan struct{}, 1)
sleptC := hasSlept(ctx, c.HealthCheck, c.HealthCheck*2)
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
runC <- struct{}{}
case <-sleptC:
runC <- struct{}{}
ticker.Reset(c.UpdateFrequency)
case <-runC:
err := runProvider(ctx, c, ticker, prvd)
if err != nil {
log.Fatal(err)
}
}
}
}
func runProvider(ctx context.Context, c configuration, ticker *time.Ticker, prvd provider.Provider) error {
log.Println("Waiting for the provider to go online")
err := prvd.WaitOnline(ctx)
if err != nil {
return err
}
ticker.Reset(c.UpdateFrequency)
img, err := prvd.Fetch(ctx)
if err != nil {
return err
}
img, err = prvd.Process(ctx, img)
if err != nil {
return err
}
addLabel(img, 10, 40, "Remarkable is sleeping")
addLabel(img, 10, 1850, "Updated at "+time.Now().Format("2/1/2006 15:04:05"))
err = imaging.Save(img, c.Output)
if err != nil {
return err
}
log.Println("Picture saved: ", c.Output)
return nil
}
func addLabel(i image.Image, x, y int, label string) error {
fnt, err := truetype.Parse(goregular.TTF)
if err != nil {
return err
}
face := truetype.NewFace(fnt, &truetype.Options{
Size: 36,
})
if img, ok := i.(*image.NRGBA); ok {
// img is now an *image.RGBA
col := color.RGBA{0, 0, 0, 255}
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: face, //basicfont.Face7x13,
Dot: point,
}
d.DrawString(label)
}
return nil
}