forked from tengfei-xy/amazon-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
236 lines (214 loc) · 5.28 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
_ "github.com/go-sql-driver/mysql"
log "github.com/tengfei-xy/go-log"
"gopkg.in/yaml.v3"
)
const MYSQL_APPLICATION_STATUS_START int = 0
const MYSQL_APPLICATION_STATUS_OVER int = 1
const MYSQL_APPLICATION_STATUS_SEARCH int = 2
const MYSQL_APPLICATION_STATUS_PRODUCT int = 3
const MYSQL_APPLICATION_STATUS_SELLER int = 4
type appConfig struct {
Mysql `yaml:"mysql"`
Basic `yaml:"basic"`
Proxy `yaml:"proxy"`
Exec `yaml:"exec"`
db *sql.DB
cookie string
primary_id int64
}
type Exec struct {
Enable `yaml:"enable"`
Loop `yaml:"loop"`
Search_priority int `yaml:"search_priority"`
}
type Enable struct {
Search bool `yaml:"search"`
Product bool `yaml:"product"`
Seller bool `yaml:"seller"`
}
type Loop struct {
All int `yaml:"all"`
all_time int
Search int `yaml:"search"`
search_time int
Product int `yaml:"product"`
product_time int
Seller int `yaml:"seller"`
seller_time int
}
type Basic struct {
App_id int `yaml:"app_id"`
Host_id int `yaml:"host_id"`
Test bool `yaml:"test"`
Domain string `yaml:"domain"`
}
type Proxy struct {
Enable bool `yaml:"enable"`
Sockc5 []string `yaml:"socks5"`
}
type Mysql struct {
Ip string `yaml:"ip"`
Port string `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Database string `yaml:"database"`
}
type flagStruct struct {
config_file string
}
var app appConfig
var robot Robots
const userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36`
func init_config(flag flagStruct) {
log.Infof("读取配置文件:%s", flag.config_file)
yamlFile, err := os.ReadFile(flag.config_file)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(yamlFile, &app)
if err != nil {
panic(err)
}
if !app.Exec.Enable.Search && !app.Exec.Enable.Product && !app.Exec.Enable.Seller {
panic("没有启动功能,检查配置文件的enable配置的选项")
}
if app.Exec.Loop.All == 0 {
app.Exec.Loop.All = 999999
}
if app.Exec.Loop.Search == 0 {
app.Exec.Loop.Search = 999999
}
if app.Exec.Loop.Product == 0 {
app.Exec.Loop.Product = 999999
}
if app.Exec.Loop.Seller == 0 {
app.Exec.Loop.Seller = 999999
}
app.Exec.product_time = 0
app.Exec.search_time = 0
app.Exec.seller_time = 0
log.Infof("程序标识:%d 主机标识:%d", app.Basic.App_id, app.Basic.Host_id)
}
func init_rebots() {
robotTxt := fmt.Sprintf("https://%s/robots.txt", app.Domain)
log.Infof("加载文件: %s", robotTxt)
txt, err := request_get(robotTxt, userAgent)
if err != nil {
log.Error("网络错误")
panic(err)
}
robot = GetRobotFromTxt(txt)
}
func init_mysql() {
DB, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", app.Mysql.Username, app.Mysql.Password, app.Mysql.Ip, app.Mysql.Port, app.Mysql.Database))
if err != nil {
panic(err)
}
DB.SetConnMaxLifetime(100)
DB.SetMaxIdleConns(10)
if err := DB.Ping(); err != nil {
panic(err)
}
log.Info("数据库已连接")
app.db = DB
}
func init_network() {
log.Info("网络测试开始")
var s searchStruct
s.en_key = "Hardware+electrician"
_, err := s.request(0)
if err != nil {
log.Error("网络错误")
panic(err)
}
}
func init_signal() {
// 创建一个通道来接收操作系统的信号
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGABRT)
go func() {
<-sigCh
log.Info("")
log.Infof("程序即将结束")
app.end()
app.db.Close()
log.Infof("程序结束")
os.Exit(0)
}()
}
func init_flag() flagStruct {
var f flagStruct
flag.StringVar(&f.config_file, "c", "config.yaml", "打开配置文件")
flag.Parse()
return f
}
func main() {
f := init_flag()
init_config(f)
init_rebots()
init_mysql()
init_network()
init_signal()
app.start()
for app.Exec.Loop.all_time = 0; app.Exec.Loop.all_time < app.Exec.Loop.All; app.Exec.Loop.all_time++ {
var search searchStruct
search.main()
var product productStruct
product.main()
var seller sellerStruct
seller.main()
}
}
func (app *appConfig) get_cookie() (string, error) {
var cookie string
if app.Basic.Host_id == 0 {
return "", fmt.Errorf("配置文件中host_id为0,cookie将为空")
}
if err := app.db.QueryRow("select cookie from cookie where host_id = ?", app.Basic.Host_id).Scan(&cookie); err != nil {
return "", err
}
cookie = strings.TrimSpace(cookie)
if app.cookie != cookie {
log.Infof("使用新cookie: %s", cookie)
}
app.cookie = cookie
return app.cookie, nil
}
func (app *appConfig) start() {
if app.Basic.Test {
log.Infof("测试模式启动")
return
}
r, err := app.db.Exec("insert into application (app_id) values(?)", app.Basic.App_id)
if err != nil {
panic(err)
}
id, err := r.LastInsertId()
if err != nil {
panic(err)
}
app.primary_id = id
}
func (app *appConfig) update(status int) {
_, err := app.db.Exec("update application set status=? where id=?", status, app.primary_id)
if err != nil {
panic(err)
}
}
func (app *appConfig) end() {
if app.Basic.Test {
return
}
if _, err := app.db.Exec("update application set status=? where id=?", MYSQL_APPLICATION_STATUS_OVER, app.primary_id); err != nil {
log.Error(err)
}
}