forked from berry-ordivo/automation-attendance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (140 loc) · 4.15 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"time"
"github.com/chromedp/cdproto/browser"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/chromedp"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
if err := run(ctx); err != nil {
log.Fatal(err.Error())
}
}
const talentaBaseURL = "https://hr.talenta.co"
var errInvalidArgument = fmt.Errorf("expected one argument: clock-in, clock-out, or check")
func run(ctx context.Context) error {
cfg, err := parseConfig()
if err != nil {
return err
}
if len(os.Args) != 2 {
return errInvalidArgument
}
allocatorOpts := chromedp.DefaultExecAllocatorOptions[:]
if cfg.Debug {
allocatorOpts = append(allocatorOpts, chromedp.Flag("headless", false))
}
allocatorCtx, stop := chromedp.NewExecAllocator(ctx, allocatorOpts...)
defer stop()
taskCtx, stop := chromedp.NewContext(allocatorCtx, chromedp.WithLogf(log.Printf))
defer stop()
var finalAction chromedp.Tasks
switch os.Args[1] {
case "clock-in":
finalAction = clockIn()
case "clock-out":
finalAction = clockOut()
case "check":
default:
return errInvalidArgument
}
var todayNodeStyle string
var lastTimeOffText string
if err := chromedp.Run(
taskCtx,
setGeolocation(cfg.Latitude, cfg.Longitude),
signIn(cfg.TalentaEmail, cfg.TalentaPassword),
getTodayNodeStyle(&todayNodeStyle),
getLastTimeOffText(&lastTimeOffText),
); err != nil {
return fmt.Errorf("sign in & initial check: %w", err)
}
weekday := time.Now().Weekday()
if weekday == time.Saturday || weekday == time.Sunday {
log.Printf("today is %s, skipping clock in/out", weekday)
return nil
}
if strings.Contains(todayNodeStyle, "red") {
log.Printf("today is a holiday, skipping clock in/out")
return nil
}
lastTimeOff, err := time.Parse("2006-01-02", lastTimeOffText)
if err != nil {
return fmt.Errorf("parse last time off date: %w", err)
}
if lastTimeOff.Format("2006-01-02") == time.Now().Format("2006-01-02") {
log.Printf("today is time off, skipping clock in/out")
return nil
}
if err := chromedp.Run(taskCtx, finalAction); err != nil {
return fmt.Errorf("clock in/out: %w", err)
}
return nil
}
func setGeolocation(latitude, longitude float64) chromedp.Tasks {
notification := browser.PermissionDescriptor{
Name: browser.PermissionTypeNotifications.String(),
}
geolocation := browser.PermissionDescriptor{
Name: browser.PermissionTypeGeolocation.String(),
}
return chromedp.Tasks{
browser.SetPermission(¬ification, browser.PermissionSettingGranted),
browser.SetPermission(&geolocation, browser.PermissionSettingGranted),
emulation.SetGeolocationOverride().
WithAccuracy(100).
WithLatitude(latitude).
WithLongitude(longitude),
}
}
func signIn(email, password string) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(talentaBaseURL),
chromedp.SendKeys("input#user_email", email),
chromedp.SendKeys("input#user_password", password),
chromedp.Click("#new-signin-button"),
chromedp.WaitNotPresent(`#new-signin-button`),
}
}
func openLiveAttendancePage() chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(talentaBaseURL + "/live-attendance"),
chromedp.WaitVisible("#tl-live-attendance-index"),
}
}
func clockIn() chromedp.Tasks {
return chromedp.Tasks{
openLiveAttendancePage(),
chromedp.Click(`//span[text()="Clock In"]`),
chromedp.Sleep(3 * time.Second),
}
}
func clockOut() chromedp.Tasks {
return chromedp.Tasks{
openLiveAttendancePage(),
chromedp.Click(`//span[text()="Clock Out"]`),
chromedp.Sleep(3 * time.Second),
}
}
// getTodayNodeStyle gets the style attribute of the node that represents today.
// The style attribute will be used to determine if today is a holiday.
func getTodayNodeStyle(today *string) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(talentaBaseURL + "/employee/company-calendar"),
chromedp.AttributeValue(`//td[contains(@class, "fc-today")]/span`, "style", today, nil),
}
}
func getLastTimeOffText(timeOff *string) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(talentaBaseURL + "/my-info/time-off"),
chromedp.Text(`//tr/td[@class="sorting_1"]`, timeOff),
}
}