-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariflight_crawler.go
182 lines (169 loc) · 5.47 KB
/
variflight_crawler.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
package main
import (
"github.com/go-resty/resty"
"github.com/liyu4/tablewriter"
"github.com/tidwall/gjson"
"os"
)
type VariFlightCrawler struct {
RestClient *resty.Client
FlightNumberInfoTable *tablewriter.Table
AirportInfoTable *tablewriter.Table
}
func NewVariFlightCrawler() *VariFlightCrawler {
vari := &VariFlightCrawler{}
vari.initVariFlightCrawler()
return vari
}
// 初始化
func (v *VariFlightCrawler) initVariFlightCrawler() {
v.RestClient = resty.New()
}
// 构造航班信息请求数据
func (v *VariFlightCrawler) getFlightNumberPayload(flightNumber, date string) (payload map[string]string) {
payload = make(map[string]string)
payload["searchText"] = flightNumber
payload["searchDate"] = date
payload["timeZone"] = "-28800"
return
}
// 初始化航班信息表格
func (v *VariFlightCrawler) initFlightNumberInfoTable() {
table := tablewriter.NewColorWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader(FlightNumberInfoTableHeader)
v.FlightNumberInfoTable = table
}
// 解析信息成表格
func (v *VariFlightCrawler) parseDataToTable(tableJson gjson.Result) {
v.initFlightNumberInfoTable()
flightData := tableJson.Get("data").Array()
for _, data := range flightData {
flightStatus := FlightNumberStatus[data.Get("flightStatusCode").Int()]
flightNumber := data.Get("fnum").String()
departureAirportName := data.Get("forgAptCname").String()
arrivalAirportName := data.Get("fdstAptCname").String()
estimatedDepTime := timestampToTime(data.Get("scheduledDeptime").Int())
actualDepTime := timestampToTime(data.Get("actualDeptime").Int())
estimatedArrTime := timestampToTime(data.Get("scheduledArrtime").Int())
actualArrTime := timestampToTime(data.Get("actualArrtime").Int())
airCraftType := data.Get("ftype").String()
airCraftNumber := data.Get("aircraftNumber").String()
// 每一行的数据
row := []string{
flightStatus,
flightNumber,
departureAirportName,
arrivalAirportName,
estimatedDepTime,
actualDepTime,
estimatedArrTime,
actualArrTime,
airCraftType,
airCraftNumber,
}
v.FlightNumberInfoTable.Append(row)
}
v.FlightNumberInfoTable.Render()
}
// 查询航班信息
func (v *VariFlightCrawler) runFlightInfo(flightNumber, date string) {
payloadData := v.getFlightNumberPayload(flightNumber, date)
dataResp, err := v.RestClient.R().
SetQueryParam("lang", "zh_CN").
SetHeader("Content-Type", ContentTypeForm).
SetHeader("User-Agent", UserAgent).
SetFormData(payloadData).
Post(FlightNumberAPIURL)
if err != nil {
logger.Fatalf("[Flight-Go]接口请求出错!, 错误原因: %s", err.Error())
} else {
if dataResp.String() != "" {
v.parseDataToTable(gjson.Parse(dataResp.String()))
} else {
logger.Error("[Flight-Go]接口数据为空!")
}
}
}
// 初始化机场进出港表格
func (v *VariFlightCrawler) initAirportInfoTable() {
table := tablewriter.NewColorWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
v.AirportInfoTable = table
}
// 解析进场进出港数据表格
func (v *VariFlightCrawler) parseAirportInfoTable(depOrArr string, tableJson gjson.Result) {
airportInfoList := tableJson.Get("list").Array()
for _, airportInfo := range airportInfoList {
flightNumber := airportInfo.Get("fnum").String()
airCraftType := airportInfo.Get("ftype").String()
flightStatus := FlightNumberStatus[airportInfo.Get("flightStatusCode").Int()]
if depOrArr == "dep" {
destinationName := airportInfo.Get("fdstAptCcity").String()
destinationAirportName := airportInfo.Get("fdstAptCname").String()
scheduleDepTime := timestampToTime(airportInfo.Get("scheduledDeptime").Int())
actualDepTime := timestampToTime(airportInfo.Get("estimatedDeptime").Int())
row := []string{
flightNumber,
airCraftType,
destinationName,
destinationAirportName,
scheduleDepTime,
actualDepTime,
flightStatus,
}
v.AirportInfoTable.Append(row)
} else if depOrArr == "arr" {
destinationName := airportInfo.Get("forgAptCcity").String()
destinationAirportName := airportInfo.Get("forgAptCname").String()
scheduledArrTime := timestampToTime(airportInfo.Get("scheduledArrtime").Int())
var actualArrTimeStr string
actualArrTime := airportInfo.Get("actualArrtime").Int()
if actualArrTime == 0 {
actualArrTimeStr = timestampToTime(airportInfo.Get("estimatedArrtime").Int())
} else {
actualArrTimeStr = timestampToTime(actualArrTime)
}
row := []string{
flightNumber,
airCraftType,
destinationName,
destinationAirportName,
scheduledArrTime,
actualArrTimeStr,
flightStatus,
}
v.AirportInfoTable.Append(row)
}
}
v.AirportInfoTable.Render()
}
// 查询机场进出港信息
func (v *VariFlightCrawler) runAirportInfo(areaName, depOrArr string) {
v.initAirportInfoTable()
var ReqURL string
switch depOrArr {
case "dep":
ReqURL = AirportDepAPIURL
v.AirportInfoTable.SetHeader(AirportInfoDepTableHeader)
case "arr":
ReqURL = AirportArrAPIURL
v.AirportInfoTable.SetHeader(AirportInfoArrTableHeader)
}
dataResp, err := v.RestClient.R().
SetQueryParam("lang", "zh_CN").
SetQueryParam("iata", cityNameCode[areaName]).
SetQueryParam("pageSize", "15").
SetQueryParam("pageNum", "1").
SetHeader("User-Agent", UserAgent).
Get(ReqURL)
if err != nil {
logger.Fatalf("[Flight-Go]接口请求出错!, 错误原因: %s", err.Error())
} else {
if dataResp.String() != "" {
v.parseAirportInfoTable(depOrArr, gjson.Parse(dataResp.String()))
} else {
logger.Error("[Flight-Go]接口数据为空!")
}
}
}