forked from ycrash/yc-data-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattendance.go
67 lines (56 loc) · 1.38 KB
/
attendance.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
package shell
import (
"fmt"
"net"
"time"
"shell/config"
"shell/logger"
)
func sleep4Attendance() {
utc := time.Now().UTC()
target, sub := calDuration4Attendance(utc)
logger.Log("now is %s, will do attendance task at %s, after %s", utc.Format("2006/01/02 15:04:05"), target.Format("2006/01/02 15:04:05"), sub)
time.Sleep(sub)
}
func calDuration4Attendance(utc time.Time) (time.Time, time.Duration) {
target := utc.Truncate(24 * time.Hour)
target = target.AddDate(0, 0, 1)
sub := target.Sub(utc)
return target, sub
}
func sleep4Distribution() {
d := calDuration4Distribution(GetOutboundIP())
logger.Log("sleep4Distribution %s", d)
if d <= 0 {
return
}
time.Sleep(d)
}
func calDuration4Distribution(ip net.IP) time.Duration {
bs := []byte(ip)
var sum byte
for _, b := range bs {
sum += b
}
m := sum % 10
d := time.Duration(m) * time.Minute
return d
}
func attend(typ string) (string, bool) {
timestamp := time.Now().Format("2006-01-02T15-04-05")
parameters := fmt.Sprintf("de=%s&ts=%s", GetOutboundIP().String(), timestamp)
endpoint := fmt.Sprintf("%s/yc-attendance?type=%s&%s",
config.GlobalConfig.Server, typ, parameters)
if config.GlobalConfig.M3 {
endpoint += "&m3=true"
}
return GetData(endpoint)
}
func Attend() (string, bool) {
sleep4Attendance()
sleep4Distribution()
return attend("daily")
}
func StartupAttend() (string, bool) {
return attend("startup")
}