-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
145 lines (120 loc) · 3.98 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
package main
import (
"context"
"fmt"
"github.com/2captcha/2captcha-go"
"github.com/chromedp/chromedp"
"github.com/go-co-op/gocron"
"log"
"math/rand"
"os"
"passport/config"
"strings"
"time"
)
var twoCaptchaApiClient *api2captcha.Client
func main() {
s := gocron.NewScheduler(time.UTC)
s.Every(86400 + 60*5).Seconds().Do(func() {
makeMyPassport()
})
s.StartBlocking()
}
func makeMyPassport() {
fmt.Println("Process started")
ctx, cancel := chromedp.NewExecAllocator(context.Background(), append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", true))...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
if err := chromedp.Run(ctx,
chromedp.Navigate("https://q.midpass.ru/"),
chromedp.Sleep(5*time.Second),
chromedp.WaitVisible(`.SelInp`),
chromedp.SetValue(`select.SelInp`, config.GetCountry(), chromedp.ByQuery),
chromedp.Sleep(randomDuration()),
chromedp.SetValue(`.wrap:nth-child(3) > .register_form > select.SelInp`, config.GetInstitution(), chromedp.ByQuery),
chromedp.Sleep(randomDuration()),
chromedp.SendKeys("#Email", config.GetLogin(), chromedp.ByID),
chromedp.Sleep(randomDuration()),
chromedp.SendKeys("#Password", config.GetPassword(), chromedp.ByID),
chromedp.Sleep(randomDuration()),
); err != nil && !strings.Contains(err.Error(), "net::ERR_ABORTED") {
handleError(ctx, err)
return
}
twoCaptchaApiClient = api2captcha.NewClient(config.GetApiKey())
code := resolveCptch(ctx, "//img[@id='imgCaptcha']")
if err := chromedp.Run(ctx,
chromedp.SendKeys("#Captcha", code, chromedp.ByID),
chromedp.Sleep(randomDuration()),
chromedp.Click(".registerForm:nth-child(16) > button", chromedp.ByQuery),
chromedp.Sleep(10*time.Second),
chromedp.Click("#topNav > li:nth-child(3)", chromedp.ByQuery),
chromedp.Sleep(randomDuration()),
chromedp.Click("#datagrid-row-r1-1-0 > td > div > input", chromedp.ByQuery),
chromedp.Sleep(randomDuration()),
chromedp.Click("#confirmAppointments", chromedp.ByQuery),
chromedp.Sleep(5*time.Second),
chromedp.WaitVisible(`#captchaValue`),
); err != nil && !strings.Contains(err.Error(), "net::ERR_ABORTED") {
handleError(ctx, err)
return
}
code2 := resolveCptch(ctx, "//img[@id='imgCaptcha']")
if err := chromedp.Run(ctx,
chromedp.SendKeys("#captchaValue", code2, chromedp.ByID),
chromedp.Sleep(randomDuration()),
chromedp.Click(".dialog-button > a", chromedp.ByQuery),
chromedp.Sleep(randomDuration()),
); err != nil && !strings.Contains(err.Error(), "net::ERR_ABORTED") {
handleError(ctx, err)
return
}
time.Sleep(5 * time.Second)
}
func resolveCptch(ctx context.Context, querySelector string) string {
var buf []byte
if err := chromedp.Run(ctx, chromedp.Screenshot(querySelector, &buf, chromedp.NodeVisible)); err != nil {
log.Fatal(err)
}
fileName := "cptch" + time.Now().Format(time.RFC850) + ".jpg"
if err := os.WriteFile("cptch/"+fileName, buf, 0o644); err != nil {
log.Fatal(err)
}
defer os.Remove("cptch/" + fileName)
capt := api2captcha.Normal{
File: "cptch/" + fileName,
CaseSensitive: true,
}
code, err := twoCaptchaApiClient.Solve(capt.ToRequest())
if err != nil {
if err == api2captcha.ErrTimeout {
log.Fatal("Timeout")
} else if err == api2captcha.ErrApi {
log.Fatal("API error")
} else if err == api2captcha.ErrNetwork {
log.Fatal("Network error")
} else {
log.Fatal(err)
}
}
fmt.Printf("Captcha code is " + code)
return code
}
func randomDuration() time.Duration {
rand.Seed(time.Now().UnixNano())
min := 2
max := 5
return (time.Duration(rand.Intn(max-min+1) + min)) * time.Second
}
func handleError(ctx context.Context, error error) {
var buf []byte
if err := chromedp.Run(ctx, chromedp.FullScreenshot(&buf, 90)); err != nil {
log.Fatal(err)
}
filePath := "full" + time.Now().Format(time.RFC850) + ".png"
if err := os.WriteFile("screenshots/"+filePath, buf, 0o644); err != nil {
log.Fatal(err)
}
fmt.Printf("Error: " + error.Error() + ". The full screenshot was saved to the " + filePath)
}