forked from liudanking/didi-car-rank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collecte_data.go
456 lines (401 loc) · 13.6 KB
/
collecte_data.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/liudanking/goutil/encodingutil"
"github.com/liudanking/goutil/netutil"
log "github.com/liudanking/goutil/logutil"
"github.com/elazarl/goproxy"
"github.com/urfave/cli"
)
func collectData(c *cli.Context) error {
if err := setCA(caCert, caKey); err != nil {
log.Error("setCA failed:%v", err)
}
proxy := goproxy.NewProxyHttpServer()
// proxy.Verbose = true
listenAddr := c.String("listen")
if listenAddr == "" {
return errors.New("listen address is empty")
}
dh := NewDidiHooker(c.String("dir"))
dh.RegisterHook(proxy)
log.Info("start serving %s", listenAddr)
if err := http.ListenAndServe(listenAddr, proxy); err != nil {
log.Error("listen %s failed:%v", listenAddr, err)
os.Exit(1)
}
return nil
}
type DidiHooker struct {
dataMtx sync.Mutex
dataDir string
}
func NewDidiHooker(dataDir string) *DidiHooker {
return &DidiHooker{
dataDir: dataDir,
}
}
func (dh *DidiHooker) RegisterHook(p *goproxy.ProxyHttpServer) {
dstHost := "devcon-go.am.xiaojukeji.com:443"
p.OnRequest(goproxy.DstHostIs(dstHost)).HandleConnect(goproxy.AlwaysMitm)
p.OnResponse(goproxy.DstHostIs(dstHost)).DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
if strings.HasPrefix(ctx.Req.URL.Path, "/front/gasstation/index") {
log.Info("gasstation hook!")
return dh.hookGasstation(resp, ctx)
} else if strings.HasPrefix(ctx.Req.URL.Path, "/map/store/near") {
log.Info("near store hook!")
return dh.hookNearStore(resp, ctx)
}
return resp
})
}
func (dh *DidiHooker) hookGasstation(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
data, err := repeatReadBody(resp)
if err != nil {
log.Error("read gasstation rsp failed:%v", err)
return resp
}
s := string(data)
startStr := `$CONFIG = JSON.parse(`
start := strings.Index(s, startStr)
if start < 0 {
log.Warning("gasstation data start index not found")
return resp
}
end := strings.Index(s[start:], ");\n")
if end < 0 {
log.Warning("gasstation data end index not found")
return resp
}
subs := s[start+len(startStr) : start+end]
gasstationStr, err := strconv.Unquote(subs)
if err != nil {
log.Warning("unquote [%s] failed:%v", subs, err)
return resp
}
rsp := &ListGasstationRsp{}
if err := json.Unmarshal([]byte(gasstationStr), rsp); err != nil {
log.Error("unmarshal [%s] failed:%v", gasstationStr, err)
return resp
}
lng, lat := ctx.Req.URL.Query().Get("lng"), ctx.Req.URL.Query().Get("lat")
city := GetCityByPosition(lng, lat)
dh.dataMtx.Lock()
if err := rsp.updateToFile(dh.cityDataDir(city)); err != nil {
log.Error("update gasstation data failed:%v", err)
}
dh.dataMtx.Unlock()
go func() {
dh.doCollectData(city, rsp.StoreForMap, rsp.AmChannel)
}()
return resp
}
type NearStoreRsp struct {
Status int `json:"status"`
Msg string `json:"msg"`
Data struct {
StoreCount int `json:"store_count"`
StoreType int `json:"store_type"`
StoreForMap []Store `json:"store_for_map"`
StoreList []interface{} `json:"store_list"`
FilterCondition interface{} `json:"filter_condition"`
SelectedFuelCategory string `json:"selected_fuel_category"`
SelectedGoodsCategory string `json:"selected_goods_category"`
SelectedBrand string `json:"selected_brand"`
FuelCategoryName string `json:"fuel_category_name"`
GoodsCategoryName string `json:"goods_category_name"`
BrandName string `json:"brand_name"`
TotalScore string `json:"total_score"`
} `json:"data"`
}
func (dh *DidiHooker) hookNearStore(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
data, err := repeatReadBody(resp)
if err != nil {
log.Warning("read near store body failed:%v", err)
return resp
}
rsp := &NearStoreRsp{}
if err := json.Unmarshal(data, rsp); err != nil {
log.Warning("unmarshal near store rsp failed:%v", err)
return resp
}
lng, lat := ctx.Req.URL.Query().Get("lng"), ctx.Req.URL.Query().Get("lat")
city := GetCityByPosition(lng, lat)
go func() {
dh.doCollectData(city, rsp.Data.StoreForMap, 10001)
}()
return resp
}
func (dh *DidiHooker) cityDataDir(city string) string {
return filepath.Join(dh.dataDir, city)
}
func (dh *DidiHooker) doCollectData(city string, stores []Store, amChannel int) {
dir := dh.cityDataDir(city)
currentOrderDir := filepath.Join(dir, "currentorder")
repurchaseDir := filepath.Join(dir, "repurchase")
os.MkdirAll(currentOrderDir, 0700)
os.MkdirAll(repurchaseDir, 0700)
// current order
for _, store := range stores {
fn := filepath.Join(currentOrderDir, fmt.Sprintf("%s.json", store.StoreID))
fi, err := os.Lstat(fn)
v := map[string]CurrentOrderItem{}
if err == nil {
if time.Since(fi.ModTime()) < 5*time.Second {
continue
} else {
if err := encodingutil.UnmarshalJSONFromFile(fn, &v); err != nil {
log.Warning("unmarshal from file %s failed:%v", err)
}
}
}
currentOrderRsp, err := store.GetCurrentOrder(amChannel)
if err != nil {
log.Warning("get [store_id:%s] current order failed:%v", store.StoreID, err)
continue
}
for _, item := range currentOrderRsp.Data.Items {
v[item.ID] = item
}
dh.dataMtx.Lock()
if err := jsonMarshalIndentToFile(fn, &v); err != nil {
log.Warning("write json data to %s failed:%v", err)
}
dh.dataMtx.Unlock()
}
files, _ := ioutil.ReadDir(currentOrderDir)
log.Info("saved %d store currentorder data for %s", len(files), city)
// repurchase
for _, store := range stores {
fn := filepath.Join(repurchaseDir, fmt.Sprintf("%s.json", store.StoreID))
fi, err := os.Lstat(fn)
v := map[string]RepurchaseItem{}
if err == nil {
if time.Since(fi.ModTime()) < 5*time.Second {
continue
} else {
if err := encodingutil.UnmarshalJSONFromFile(fn, &v); err != nil {
log.Warning("unmarshal from file %s failed:%v", err)
}
}
}
repurchaseDriverRsp, err := store.GetRepurchaseDriver(amChannel)
if err != nil {
log.Warning("get [store_id:%s] current order failed:%v", store.StoreID, err)
continue
}
for _, item := range repurchaseDriverRsp.Data.Items {
v[item.DriverID] = item
}
dh.dataMtx.Lock()
if err := jsonMarshalIndentToFile(fn, &v); err != nil {
log.Warning("write json data to %s failed:%v", err)
}
dh.dataMtx.Unlock()
}
files, _ = ioutil.ReadDir(repurchaseDir)
log.Info("saved %d store repurchase data for %s", len(files), city)
}
type ListGasstationRsp struct {
AmChannel int `json:"am_channel"`
Avater string `json:"avater"`
BrandName string `json:"brand_name"`
CenterURL string `json:"center_url"`
CityID string `json:"city_id"`
CityName string `json:"city_name"`
ConfirmURL string `json:"confirm_url"`
CouponCount int `json:"coupon_count"`
DistanceCount struct {
Num8000 int `json:"8000"`
} `json:"distance_count"`
FilterCondition struct {
Oil struct {
FuelCategoryInfo struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"fuel_category_info"`
GoodsCategoryInfo []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"goods_category_info"`
BrandInfo []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"brand_info"`
} `json:"oil"`
Gas struct {
FuelCategoryInfo struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"fuel_category_info"`
GoodsCategoryInfo []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"goods_category_info"`
BrandInfo []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"brand_info"`
} `json:"gas"`
} `json:"filter_condition"`
FuelCategoryName string `json:"fuel_category_name"`
GoodsCategoryName string `json:"goods_category_name"`
GulfstreamCityID int `json:"gulfstream_city_id"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
PassportUID string `json:"passport_uid"`
Phone string `json:"phone"`
SelectedBrand string `json:"selected_brand"`
SelectedFuelCategory string `json:"selected_fuel_category"`
SelectedGoodsCategory string `json:"selected_goods_category"`
StoreCount int `json:"store_count"`
StoreForMap []Store `json:"store_for_map"`
StoreList []struct {
StoreID string `json:"store_id"`
Name string `json:"name"`
Logo string `json:"logo"`
LogoX string `json:"logo_x"`
LogoXx string `json:"logo_xx"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
Price string `json:"price"`
Discount string `json:"discount"`
Distance string `json:"distance"`
Address string `json:"address"`
MonthOrderCount string `json:"month_order_count"`
RepurchaseUserRate int `json:"repurchase_user_rate"`
Rank int `json:"rank"`
RankText string `json:"rank_text"`
IsNew int `json:"is_new"`
Rawid string `json:"rawid"`
ActivityNum int `json:"activity_num"`
ActivityList []interface{} `json:"activity_list"`
CouponInfo interface{} `json:"coupon_info"`
PromotionContent string `json:"promotion_content"`
FreshUser int `json:"fresh_user"`
TotalScore string `json:"total_score"`
DidiGuideDiscount string `json:"didi_guide_discount"`
RankDidiDiscount string `json:"rank_didi_discount"`
RankGuideDiscount string `json:"rank_guide_discount"`
RankStoreDiscount string `json:"rank_store_discount"`
RankPrice string `json:"rank_price"`
} `json:"store_list"`
StoreType int `json:"store_type"`
Ticket string `json:"ticket"`
UserRank int `json:"user_rank"`
UserRankImg string `json:"user_rank_img"`
UserRankName string `json:"user_rank_name"`
}
type Store struct {
StoreID string `json:"store_id"`
Name string `json:"name"`
Logo string `json:"logo"`
LogoX string `json:"logo_x"`
LogoXx string `json:"logo_xx"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
Rawid string `json:"rawid"`
Distance string `json:"distance"`
Price string `json:"price"`
}
func (rsp *ListGasstationRsp) updateToFile(dir string) error {
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
fn := filepath.Join(dir, "gasstations.json")
v := map[string]Store{}
if _, err := os.Lstat(fn); err == nil {
if err := encodingutil.UnmarshalJSONFromFile(fn, &v); err != nil {
log.Warning("unmarshal failed:%v", err)
return err
}
}
for _, store := range rsp.StoreForMap {
v[store.StoreID] = store
}
return jsonMarshalIndentToFile(fn, &v)
}
type CurrentOrderRsp struct {
Status int `json:"status"`
Msg string `json:"msg"`
Data struct {
Page int `json:"page"`
Size int `json:"size"`
Total int `json:"total"`
Items []CurrentOrderItem `json:"items"`
} `json:"data"`
}
type CurrentOrderItem struct {
ID string `json:"id"`
UID string `json:"uid"`
Pid string `json:"pid"`
UserName string `json:"user_name"`
Avater string `json:"avater"`
SalePrice string `json:"sale_price"`
RealPrice string `json:"real_price"`
RealPriceFmt string `json:"real_price_fmt"`
Status int `json:"status"`
PayTime int `json:"pay_time"`
PayTimeFmt string `json:"pay_time_fmt"`
CarModel string `json:"car_model"`
SavePrice string `json:"save_price"`
SavePriceFmt string `json:"save_price_fmt"`
}
func (store Store) GetCurrentOrder(amChannel int) (*CurrentOrderRsp, error) {
addr := "https://devcon-go.am.xiaojukeji.com/front/statistic/currentorder"
rsp := &CurrentOrderRsp{}
data, err := netutil.DefaultHttpClient().UserAgent(netutil.UA_CHROME).
RequestForm("GET", addr, map[string]interface{}{
"am_channel": amChannel,
"store_id": store.StoreID,
}).DoJSON(rsp)
if err != nil {
log.Error("get currentorder [store_id:%s] failed:[data:%s]%v", store.StoreID, data, err)
return nil, err
}
return rsp, nil
}
type RepurchaseDriverRsp struct {
Status int `json:"status"`
Msg string `json:"msg"`
Data struct {
Page int `json:"page"`
Size int `json:"size"`
Total int `json:"total"`
Items []RepurchaseItem `json:"items"`
} `json:"data"`
}
type RepurchaseItem struct {
DriverID string `json:"driver_id"`
Avanter string `json:"avanter"`
DriverName string `json:"driver_name"`
UserName string `json:"user_name"`
CarModel string `json:"car_model"`
OrderCount1M int `json:"order_count_1m"`
Ordercount1M int `json:"ordercount_1m"`
OrderDiscount1MFmt string `json:"order_discount_1m_fmt"`
}
func (store Store) GetRepurchaseDriver(amChannel int) (*RepurchaseDriverRsp, error) {
addr := "https://devcon-go.am.xiaojukeji.com/front/statistic/repurchase"
rsp := &RepurchaseDriverRsp{}
data, err := netutil.DefaultHttpClient().UserAgent(netutil.UA_CHROME).
RequestForm("GET", addr, map[string]interface{}{
"am_channel": amChannel,
"store_id": store.StoreID,
}).DoJSON(rsp)
if err != nil {
log.Error("get currentorder [store_id:%s] failed:[data:%s]%v", store.StoreID, data, err)
return nil, err
}
return rsp, nil
}