-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (60 loc) · 1.48 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
package main
import (
"fmt"
"log"
"strconv"
"time"
"github.com/jasonlvhit/gocron"
"github.com/stackhound/ande-crawl/crawl"
"github.com/stackhound/ande-crawl/db"
"github.com/stackhound/ande-crawl/status"
)
func main() {
// Con esto metemos el servidor web en una goroutine
go status.Listen()
gocron.Every(1).Day().Do(dayCheck)
// remove, clear and next_run
_, time := gocron.NextRun()
fmt.Println(time)
// function Start start all the pending jobs
<-gocron.Start()
}
func dayCheck() {
log.Println("Another day under the sun!")
//This is updated daily so we check if it is our desired date every day
currentTime := time.Now().Local()
day := currentTime.Day()
if day != 28 {
return
}
doCrawl()
}
func doCrawl() {
records, err := db.GetAvailableNIS()
if err != nil {
log.Fatal("Couldn't get the data:", err)
}
for _, nis := range records {
t := strconv.FormatInt(nis.NIS, 10)
consumption, amount, pendingBills, expirationDate, err := crawl.FetchConsumption(t)
if err != nil {
log.Fatal("Couldn't fetch consumption!")
// Skip this:
continue
}
// No errors, store the record:
record := db.ConsumptionRecord{}
record.NIS = t
record.Consumption = consumption
record.Amount = amount
record.PendingBills = pendingBills
record.Expiration = expirationDate
err = db.StoreConsumptionRecord(&record)
if err != nil {
log.Println("Couldn't insert data:", err)
}
log.Println("Done with this one")
}
status.S.Iterations++
log.Println("Goodbye")
}