-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircbot_news.go
64 lines (59 loc) · 1.26 KB
/
ircbot_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
package main
import (
"context"
"fmt"
"time"
"gopkg.in/tomb.v2"
)
func (b *ircbot) pollNews() error {
var next time.Duration
var oldnews string
now := time.Now()
min := now.Minute()
delta := 1 - min
if delta > 0 {
next = time.Minute * time.Duration(delta)
} else if delta == 0 {
next = time.Minute
} else {
next = time.Minute * time.Duration(60+delta)
}
rootctx := b.tomb.Context(nil)
timer := time.NewTimer(next)
for {
select {
case <-b.tomb.Dying():
timer.Stop()
return tomb.ErrDying
case <-timer.C:
timer.Reset(time.Hour)
ctx, cancel := context.WithTimeout(rootctx, 10*time.Second)
line, err := getNewsPost(ctx, "https://t.me/s/neuralmeduza", b.config.userAgent)
cancel()
if err != nil {
b.Logf("Error getting news: %#v", err)
continue
}
if line == oldnews {
continue
}
b.mu.Lock()
channel := b.channels["#mania"]
b.mu.Unlock()
channel.Say(fmt.Sprintf("новости: %s", line))
oldnews = line
}
}
}
func getNewsPost(ctx context.Context, url string, userAgent string) (string, error) {
body, _, err := get(ctx, url, "text/html", userAgent)
if err != nil {
return "", err
}
defer body.Close()
text, err := extractTGLastPost(body)
if err != nil {
return "", err
}
return text, nil
}