This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitter.go
157 lines (110 loc) · 4.73 KB
/
twitter.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
package sobot
import (
"bufio"
"log"
"os"
"strings"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
)
var watch_twitter bool
func (user *AccountInfo) firstTwitterRun() {
os.MkdirAll("account_data/twitter/"+user.Username, os.ModePerm)
log.Println("First run for ", user.Username, " on ", user.Pname)
page := rod.New().SlowMotion(time.Second * 2).MustConnect().MustPage("about:blank")
page.MustEmulate(rod_device)
page.MustNavigate("https://twitter.com/i/flow/login").MustWaitLoad()
// For debugging
end_debug := make(chan bool)
if watch_twitter {
go user.watchBrowser(page, end_debug)
}
time.Sleep(time.Second * 3)
page.MustElementX("/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[5]/label/div/div[2]/div/input").MustInput(user.Username)
page.MustElementX("/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[6]/div/span").MustClick()
page.MustElementX("/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[3]/div/label/div/div[2]/div[1]/input").MustInput(user.Password)
page.MustElementX("/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[2]/div/div/div/span").MustClick()
// This section makes the first login to the account and reports if the password is wrong
time.Sleep(time.Second * 7)
_, err := page.Timeout(time.Second * 5).ElementX("/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[1]/div[1]/span")
if err == nil {
page.Close()
log.Println("Username or password incorrect")
return
}
// This section makes the first login to the account and reports if the password is wrong
page.MustWaitLoad()
log.Println("Wait 3 second for get cookies successful.")
time.Sleep(time.Second * 3)
f, err := os.Create("account_data/twitter/" + user.Username + "/cookie.dat")
if err != nil {
log.Fatal("We got an error at creating cookie data file\n", err)
}
for _, v := range page.MustCookies() {
_, err2 := f.WriteString(v.Name + "#" + v.Value + "#" + v.Domain + "\n")
if err2 != nil {
log.Fatal("Cookies doesn't writed on file\n", err2)
}
}
log.Println("Cookies saved.")
f.Close()
if watch_instagram {
end_debug <- true // for stop saving screenshots
}
page.Close()
log.Println("Login successful and cookies registered")
user.cookieTwitterRun()
}
func (user *AccountInfo) cookieTwitterRun() {
log.Println("Found cookie for ", user.Username)
// first section
page := rod.New().SlowMotion(time.Second * 2).MustConnect().MustPage("about:blank")
page.MustEmulate(rod_device)
file, err := os.Open("account_data/twitter/" + user.Username + "/cookie.dat")
if err != nil {
log.Fatalf(user.Username, user.Pname, " cookie read error: %s", err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
saved_dat := strings.Split(scanner.Text(), "#")
page.MustSetCookies([]*proto.NetworkCookieParam{{
Name: saved_dat[0],
Value: saved_dat[1],
Domain: saved_dat[2],
}}...)
}
file.Close()
log.Println("Cookies loaded")
page.MustNavigate("https://twitter.com").MustWaitLoad()
// For debugging
end_debug := make(chan bool)
if watch_twitter {
go user.watchBrowser(page, end_debug)
}
time.Sleep(time.Second * 5)
page.MustElementX("/html/body/div/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/label/div[1]/div/div/div/div/div[2]/div/div/div/div").MustClick()
page.MustElementX("/html/body/div/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/label/div[1]/div/div/div/div/div[2]/div/div/div/div").MustInput(user.Caption)
time.Sleep(time.Second * 3)
page.MustElementX("/html/body/div/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[3]/div/div/div[1]/input").MustSetFiles(user.FilePath)
time.Sleep(time.Second * 3)
page.MustElementX("/html/body/div/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[3]/div/div/div[2]/div[3]/div").MustClick()
log.Println("All process are done, waiting 15 seconds")
time.Sleep(time.Second * 15)
if watch_instagram {
end_debug <- true // for stop saving screenshots
}
log.Println("Everything ok, shutting down browser.")
page.Close()
log.Println("Login with Cookie and share successfully")
}
func (user *AccountInfo) shareTwitter(debug bool) {
_, err := os.ReadFile("account_data/twitter/" + user.Username + "/cookie.dat")
watch_twitter = debug
if err != nil {
user.firstTwitterRun()
} else {
user.cookieTwitterRun()
}
}