forked from 1995parham/loser-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (61 loc) · 1.6 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/1995parham/loser-scraper/config"
"github.com/1995parham/loser-scraper/mail"
"github.com/1995parham/loser-scraper/parser"
"github.com/1995parham/loser-scraper/scrap"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
)
func main() {
fmt.Println("18.20 at Sep 07 2016 7:20 IR721")
var latest time.Time
cfg := config.New()
ml, err := mail.New(cfg.Mail.Host, cfg.Mail.Port, cfg.Mail.Username, cfg.Mail.Password)
if err != nil {
logrus.Fatalf("mailer initiation failed: %s", err)
}
c := cron.New()
if _, err := c.AddFunc(fmt.Sprintf("@every %s", cfg.Period), func() {
sc := scrap.New(cfg.Target)
rd, err := sc.Scrap()
if err != nil {
logrus.Fatalf("scrap failed: %s", err)
return
}
defer func() {
if err := rd.Close(); err != nil {
logrus.Errorf("reader closed: %s", err)
}
}()
ts, err := parser.ExtractTimeline(rd)
if err != nil {
logrus.Errorf("parser failed: %s", err)
return
}
toSend := make([]parser.Timeline, 0)
for _, t := range ts {
if t.At.After(latest) {
latest = t.At
logrus.Infof("Tweet %d from %s at %s: %s\n", t.Index, t.User, t.At, t.Content)
toSend = append(toSend, t)
}
}
if err := ml.Send(cfg.Target, toSend, cfg.You, cfg.Mail.Username); err != nil {
logrus.Errorf("send failed: %s", err)
}
}); err != nil {
logrus.Fatalf("failed to register scraper: %s", err)
}
c.Start()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("18.20 As always ... left me alone")
c.Stop()
}