-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.go
277 lines (232 loc) · 5.37 KB
/
news.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"image"
"image/draw"
"strings"
"sync"
"time"
"github.com/fogleman/gg"
"github.com/mmcdole/gofeed"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
)
// News configurator
type News struct {
Feeds []interface{}
Detail bool
SeedTime string
atTime time.Time
endTime time.Time
Duration time.Duration
Repeat time.Duration
newsTimer *time.Timer
resetTimer *time.Timer
lastNews time.Time
Velocity int
Width int
Limit int
news string
face font.Face
fontHeight float64
mux sync.Mutex
bnd image.Rectangle
rct image.Rectangle
pt image.Point
marquee chan bool
slice *image.RGBA
canvas *image.RGBA
place image.Rectangle
display bool
active bool
}
type freedres struct {
feed *gofeed.Feed
err error
}
// InitNews instantiate news collector
func InitNews(nc News) *News {
n := &News{
Feeds: nc.Feeds,
Detail: nc.Detail,
SeedTime: nc.SeedTime,
Duration: nc.Duration,
Velocity: nc.Velocity,
Width: nc.Width,
Limit: nc.Limit,
atTime: time.Now().Add(-1 * time.Hour),
lastNews: time.Now().Add(-24 * time.Hour),
Repeat: nc.Repeat,
face: basicfont.Face7x13,
fontHeight: 13,
active: false,
}
if n.Velocity == 0 {
n.Velocity = 6
}
if n.Width == 0 {
n.Width = 126
}
n.nextTime()
return n
}
// Stop deactivate news scheduling
func (n *News) Stop() {
n.marquee <- true
// deactivate schedule etc
if n.resetTimer != nil {
n.resetTimer.Stop()
}
if n.newsTimer != nil {
n.newsTimer.Stop()
}
}
func (n *News) nextTime() {
// define next active window and init timers
if n.atTime.Before(time.Now()) {
n.atTime, _ = time.Parse(`2006-01-02T15:04-0700`,
strings.Replace(time.Now().Format(`2006-01-02TXX:XX-0700`),
`XX:XX`, n.SeedTime, -1))
}
for {
if n.atTime.After(time.Now()) {
break
}
n.atTime = n.atTime.Add(n.Repeat)
}
n.endTime = n.atTime.Add(n.Duration)
waitDuration := time.Duration(n.atTime.Add(-1*time.Minute).UnixNano() - time.Now().UnixNano())
n.newsTimer = time.AfterFunc(waitDuration, func() { n.getNews() })
waitDuration = time.Duration(n.endTime.UnixNano() - time.Now().UnixNano())
n.resetTimer = time.AfterFunc(waitDuration, func() { n.nextTime() })
n.stopScroller()
}
// SetFace defines font face
func (n *News) SetFace(f font.Face) {
if n.face != f {
n.face = f
fmx := n.face.Metrics()
n.fontHeight = float64((fmx.Height >> 6) + 2)
}
if `` != n.news {
n.paintCanvas()
}
}
// Display init news scheduling
func (n *News) Display() bool {
t := time.Now()
return (t.After(n.atTime.Add(-1*time.Second)) &&
t.Before(n.endTime) &&
n.news != ``)
}
// getNews fetch news text
func (n *News) paintCanvas() {
n.stopScroller()
n.mux.Lock()
const H = 2000
const P = 2
dc := gg.NewContext(n.Width, H)
dc.Clear()
dc.SetHexColor("#52bbc5cc")
dc.SetFontFace(n.face)
w, h := dc.MeasureMultilineString(strings.Join(dc.WordWrap(n.news, float64(n.Width-2)), "\n"), 1.2)
dc.DrawStringWrapped(n.news, 1, 1, 0, 0, float64(n.Width-2), 1.2, gg.AlignLeft)
nh := h + 5
n.canvas = image.NewRGBA(image.Rect(0, 0, int(w), int(nh)))
//n.canvas.
n.bnd = n.canvas.Bounds()
draw.Draw(n.canvas, n.bnd, dc.Image(), image.ZP, draw.Over)
n.rct = n.bnd
n.rct.Max.Y = n.Velocity
n.slice = image.NewRGBA(n.rct)
n.place = image.Rect(0, n.bnd.Max.Y-n.Velocity, n.bnd.Max.X, n.bnd.Max.Y)
n.pt = image.Pt(0, n.Velocity)
n.mux.Unlock()
if int(nh) > n.Limit {
n.initScroller()
}
}
func (n *News) initScroller() {
n.stopScroller()
n.marquee = sched(n.scroller, 200*time.Millisecond)
n.active = true
}
func (n *News) stopScroller() {
if n.active {
n.marquee <- true
}
n.active = false
}
// marquee scrolls text through canvas
func (n *News) scroller() {
if n.canvas.Bounds().Max.Y > n.Limit {
n.mux.Lock()
draw.Draw(n.slice, n.rct, n.canvas, n.rct.Min, draw.Src)
draw.Draw(n.canvas, n.bnd, n.canvas, n.bnd.Min.Add(n.pt), draw.Src)
draw.Draw(n.canvas, n.place, n.slice, n.rct.Min, draw.Src)
n.mux.Unlock()
}
}
// getNews fetch news text
func (n *News) getNews() {
// shouldn't need this!
defer func() {
if err := recover(); err != nil {
panic(err)
}
}()
f := n.fetchFeeds()
n.news = ``
sep := ``
for _, fl := range f {
if fl != nil {
for xx, fi := range fl.Items {
if xx < 9 && fi.PublishedParsed != nil && fi.PublishedParsed.After(n.lastNews) {
if !strings.Contains(n.news, fi.Title) {
n.news += sep + "• " + fi.Title
if n.Detail {
n.news += "\n" + fi.Description
}
sep = "\n"
}
}
}
}
}
n.lastNews = time.Now().UTC()
n.paintCanvas()
}
func (n *News) fetchFeeds() []*gofeed.Feed {
fc := make(chan freedres, len(n.Feeds))
for f := range n.Feeds {
feed := n.Feeds[f].(map[interface{}]interface{})
//if feed["active"].(bool) {
go n.fetchFeed(feed["link"].(string), fc)
//}
}
var fs []*gofeed.Feed
for i := 0; i < len(n.Feeds); i++ {
res := <-fc
if res.err != nil {
continue
}
fs = append(fs, res.feed)
}
return fs
}
func (n *News) fetchFeed(uri string, fc chan freedres) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(uri)
if err != nil {
fc <- freedres{nil, err}
}
fc <- freedres{feed, nil}
}
// Image returns current canvas graphic-mode
func (n *News) Image() *image.RGBA {
return n.canvas
}
func newsStop() {
if nil != news {
news.Stop()
}
}