-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.go
63 lines (58 loc) · 1.06 KB
/
task.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"time"
)
func startTask() {
fmt.Println("Start Task!")
_, err := os.Stat("./config/")
if os.IsNotExist(err) {
_ = os.Mkdir("./config/", os.ModePerm)
}
ticker := time.NewTicker(4 * time.Hour)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
taskHandler()
case <-quit:
ticker.Stop()
return
}
}
}()
}
func taskHandler() {
config := Config{}
_, err := os.Stat("./config/config.json")
if os.IsNotExist(err) {
config.LastUpdate = 10210
} else {
getJson("./config/config.json", &config)
}
id := config.LastUpdate
sum := 0
for sum < 10 {
fmt.Println("Checking " + strconv.Itoa(id+sum))
success, bookid := bookManagement(strconv.Itoa(id + sum))
if success {
pushBook(bookid, strconv.Itoa(id+sum))
sum += 1
} else {
break
}
}
config.LastUpdate = id + sum
jsonString, _ := json.Marshal(config)
err = ioutil.WriteFile("./config/config.json", jsonString, 0644)
if err != nil {
log.Panic(err)
return
}
}