-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
108 lines (89 loc) · 2.23 KB
/
scheduler.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
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/jasonlvhit/gocron"
"log"
"net/http"
"time"
)
const itcUaURL = "https://itc.ua/"
const newsUpdateTimeMinute = 2
var lastReceivedITCURL string
// Scheduler is a scheduler service struct
type Scheduler struct {
chatService Chat
bot Bot
}
// NewScheduler returns new Bot service
func NewScheduler(b Bot, c Chat) Scheduler {
return Scheduler{
chatService: c,
bot: b,
}
}
// StartBotScheduler starts bot Scheduler
func (s *Scheduler) StartBotScheduler() {
time.Sleep(time.Second * 5)
sc := gocron.NewScheduler()
sc.Every(newsUpdateTimeMinute).Minutes().Do(s.UpdateNews)
// starts scheduler
<-sc.Start()
}
// UpdateNews fetches all updates
func (s *Scheduler) UpdateNews() {
// ITC.ua block start
err := s.FetchITC()
if err != nil {
log.Printf(err.Error())
}
// ITC.ua block end
}
// FetchITC fetches ITC ua
func (s *Scheduler) FetchITC() (err error) {
url, err := s.LastITCUAPostURL()
if err != nil {
return fmt.Errorf("error while retreiving ITC post %v", err)
}
if lastReceivedITCURL == url || url == "" {
return
}
msg := fmt.Sprintf("Так-так... у нас тут новый пост! Зацени ка %v", url)
err = s.bot.SendToChats(s.chatService.GetChatIDs(), msg)
if err != nil {
return fmt.Errorf("error while sending to chats %v", err)
}
lastReceivedITCURL = url
return
}
// LastITCUAPostURL parses last post url from ITC.ua
func (s *Scheduler) LastITCUAPostURL() (firstPostURL string, err error) {
res, err := http.Get(itcUaURL)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return "", fmt.Errorf("got %v", res.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", fmt.Errorf("got %v", res.StatusCode)
}
// To find all hrefs
//doc.Find("div.row").Each(func(i int, s *goquery.Selection) {
//s.Find("div.post").Each(func(i int, s *goquery.Selection) {
// str,_:= s.Find("div.col-xs-8").Find("a").Attr("href")
// fmt.Println(str)
//})
//})
firstPostURL, found := doc.Find("div.row").
Find("div.post").
Find("div.col-xs-8").
Find("a").
Attr("href")
if !found {
return "", fmt.Errorf("received no url")
}
return
}