/
init_db.go
934 lines (866 loc) · 25.6 KB
/
init_db.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
package initialize
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"reflect"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/chenhaonan-eth/dao/config"
"github.com/chenhaonan-eth/dao/dal"
"github.com/chenhaonan-eth/dao/dal/model"
"github.com/chenhaonan-eth/dao/dal/query"
"github.com/chenhaonan-eth/dao/economic/ftures"
"github.com/chenhaonan-eth/dao/economic/macroscopic"
"github.com/chenhaonan-eth/dao/pkg/utils"
"github.com/chromedp/chromedp"
"github.com/go-resty/resty/v2"
"github.com/gocolly/colly"
"github.com/gocolly/colly/extensions"
"github.com/robertkrimen/otto"
"github.com/tidwall/gjson"
"go.uber.org/zap"
"gorm.io/gorm"
)
var (
client = resty.New()
q = query.Q
// QueryCtxDo *query.QueryCtx
)
func Init() {
// 创建本地数据库
dal.DB = dal.ConnectDB(config.G_Config.System.Dsn)
query.SetDefault(dal.DB)
// QueryCtxDo = Q.WithContext(context.Background())
// 删除表
// dal.DB.Migrator().DropTable(&model.SocialFinancingStock{})
// 初始化数据库
for _, tabledb := range model.OpArgsMap {
// init db table 自动检测是否存在
dal.DB.AutoMigrate(tabledb)
}
// 检查数据库是否有数据 没有的话加载数据
for _, f := range []func() error{
initTotalSocialFinancing,
initMacroChinaMoneySupply,
initMacroChinaConsumerGoodsRetail,
initPMI,
initGDP,
initMacroChinaCpi,
initMacroPpi,
initSocialFinancingStock,
initSH300PE,
initBondZhUsRate,
initCADFuturesForeignHist,
initCXPMI,
initValueAddedOfIndustrialProduction,
initSocialElectricityConsumption,
initPassengerAndFreightTraffic,
initChinaNewFinancialCredit,
initForeignReserveAndGold,
initInvestmentInFixedAssets,
initCentralBankMonetaryAuthorityAssetsAndLiabilities,
initManufacturingPmiParticulars,
} {
// 加入延迟请求
num := rand.Int31n(500)
time.Sleep(time.Duration(num) * time.Millisecond)
if err := f(); err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
config.G_LOG.Error("init db ", zap.Error(err))
}
}
}
}
// 制造业PMI详情
func initManufacturingPmiParticulars() error {
t := q.ManufacturingPmiParticulars
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ManufacturingPmiParticulars("500")
if err != nil {
config.G_LOG.Error("find initManufacturingPmiParticulars err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 512)
if err != nil {
config.G_LOG.Error("CreateInBatches initManufacturingPmiParticulars err", zap.Any("err", err))
return err
}
}
return err
}
// 央行货币当局资产负债
func initCentralBankMonetaryAuthorityAssetsAndLiabilities() error {
t := q.CentralBankMonetaryAuthorityAssetsAndLiabilities
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.CentralBankMonetaryAuthorityAssetsAndLiabilities("500")
if err != nil {
config.G_LOG.Error("find initCentralBankMonetaryAuthorityAssetsAndLiabilities err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 512)
if err != nil {
config.G_LOG.Error("CreateInBatches initCentralBankMonetaryAuthorityAssetsAndLiabilities err", zap.Any("err", err))
return err
}
}
return err
}
// 固定资产投资
func initInvestmentInFixedAssets() error {
t := q.InvestmentInFixedAssets
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.InvestmentInFixedAssets("500")
if err != nil {
config.G_LOG.Error("find initInvestmentInFixedAssets err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 512)
if err != nil {
config.G_LOG.Error("CreateInBatches initInvestmentInFixedAssets err", zap.Any("err", err))
return err
}
}
return err
}
// 外汇储备与黄金
func initForeignReserveAndGold() error {
t := q.ForeignReserveAndGold
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ForeignReserveAndGold("500")
if err != nil {
config.G_LOG.Error("find initForeignReserveAndGold err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 512)
if err != nil {
config.G_LOG.Error("CreateInBatches initForeignReserveAndGold err", zap.Any("err", err))
return err
}
}
return err
}
// 中国新增信贷数据
func initChinaNewFinancialCredit() error {
t := q.NewFinancialCredit
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ChinaNewFinancialCredit()
if err != nil {
config.G_LOG.Error("find ChinaNewFinancialCredit err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Error("CreateInBatches ChinaNewFinancialCredit err", zap.Any("err", err))
return err
}
}
return err
}
// 外盘期货 铜
func initCADFuturesForeignHist() error {
t := q.FturesFoewign
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
r, err := ftures.FuturesForeignHist("CAD")
if err != nil {
config.G_LOG.Error("initCADFuturesForeignHist err", zap.Error(err))
}
for _, v1 := range r {
date, err := utils.ParseTime(v1.Date)
if err != nil {
config.G_LOG.Error("ParseTime", zap.Error(err))
}
v1.Date = date.Format(utils.DATE_FORMAT)
v1.Symbol = "CAD"
}
err = do.CreateInBatches(r, 2048)
if err != nil {
config.G_LOG.Error("CreateInBatches initCADFuturesForeignHist err", zap.Any("err", err))
return err
}
}
return err
}
// 国债收益率
func initBondZhUsRate() error {
t := q.BondZhUsRate
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.BondZhUsRate()
if err != nil {
config.G_LOG.Error("find Colly Macro China Money Supply err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 2048)
if err != nil {
config.G_LOG.Error("CreateInBatches CollyMacroChinaMoneySupply err", zap.Any("err", err))
return err
}
}
return err
}
// 沪深300市盈率
func initSH300PE() error {
t := q.SH300PE
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.SH300PE()
if err != nil {
config.G_LOG.Error("find SH300PE err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Error("CreateInBatches SH300PE err", zap.Any("err", err))
return err
}
}
return err
}
// 获取所有股票市盈率|市净率|股息率|市销率|总市值
// 写入sqlite数据库时间较长
func initleguleguPePSPb() {
t := q.PePbPsDvTotalmv
do := t.WithContext(context.Background())
c := colly.NewCollector(
colly.AllowedDomains("legulegu.com", "www.legulegu.com"),
)
extensions.RandomUserAgent(c)
//限速
err := c.Limit(&colly.LimitRule{
DomainRegexp: `legulegu\.com`,
RandomDelay: 5000 * time.Millisecond,
Parallelism: 10,
})
if err != nil {
log.Fatal(err)
}
// On every a element which has href attribute call callback
c.OnHTML("div.col-md-2 > a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
// Print link
log.Printf("Link found: %q -> %s\n", e.Text, link)
r, err := Process(e.Text, link)
if err == nil {
do.CreateInBatches(r, 5000)
}
})
// Before making a request print "Visiting ..."
c.OnRequest(func(r *colly.Request) {
log.Println("Visiting", r.URL.String())
})
// Set error handler
c.OnError(func(r *colly.Response, err error) {
log.Println("Request URL:", r.Request.URL, "failed with response:", r, "\nError:", err)
})
// Start scraping on https://legulegu.com/stocklist
c.Visit("https://legulegu.com/stocklist")
}
// 存储 冠龙节能(301151) /s/301151
func Process(name, link string) ([]*model.PePbPsDvTotalmv, error) {
symbolName := string([]byte(name)[:strings.Index(name, "(")])
result := []*model.PePbPsDvTotalmv{}
symbol := strings.Split(link, "/")[2]
token, err := getToken()
if err != nil {
panic(err)
}
resp, err := client.R().
SetQueryParams(map[string]string{
"token": token,
}).
Get(fmt.Sprintf("https://www.legulegu.com/api/s/base-info/%s", symbol))
if err != nil {
return result, err
}
b := resp.Body()
v := gjson.GetBytes(b, "data.items")
arrdata := v.Array()
for _, v := range arrdata {
v1 := v.Array()
m := model.PePbPsDvTotalmv{}
ivalue := reflect.ValueOf(&m).Elem()
for i, v := range v1 {
elem := ivalue.Field(i)
elem.SetString(v.String())
}
m.Name = symbolName
m.Symbol = symbol
result = append(result, &m)
}
return result, nil
}
func getToken() (string, error) {
vm := otto.New()
vm.Run(macroscopic.SCRIPT)
t := time.Now().Format("2006-01-02")
token, err := vm.Call("hex", nil, t)
if err != nil {
return "", err
}
return strings.ToLower(token.String()), nil
}
// 获取 M0 M1 M2存储
func initMacroChinaMoneySupply() error {
t := q.MacroChinaMoneySupply
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.MacroChinaMoneySupply()
if err != nil {
config.G_LOG.Error("find init Macro China Money Supply err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 5000)
if err != nil {
config.G_LOG.Error("find initMacroChinaMoneySupply err", zap.Any("err", err))
return err
}
}
return err
}
// 财新pmi
func initCXPMI() error {
t := q.PmiCx
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.PmiCx()
if err != nil {
config.G_LOG.Error("find initCXPMI err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 512)
if err != nil {
config.G_LOG.Error("find initCXPMI err", zap.Any("err", err))
return err
}
}
return err
}
// pmi 存储
func initPMI() error {
t := q.ChinaPMI
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ChinaPMI()
if err != nil {
config.G_LOG.Error("find initPMI err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Error("find initPMI err", zap.Any("err", err))
return err
}
}
return err
}
// 社会融资总额
func initTotalSocialFinancing() error {
t := q.SocialFinancingFlow
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
m, err := macroscopic.MacroChinaShrzgm()
if err != nil {
config.G_LOG.Error("find initTotalSocialFinancing err", zap.Any("err", err))
return err
}
for _, v := range m {
date, err := time.Parse("200601", v.Date)
if err != nil {
config.G_LOG.Error("ParseTime", zap.Error(err))
}
v.Date = date.Format(utils.DATE_FORMAT)
}
err = do.CreateInBatches(m, 1024)
if err != nil {
config.G_LOG.Error("CreateInBatches initTotalSocialFinancing err", zap.Any("err", err))
return err
}
}
return err
}
// GDP
func initGDP() error {
t := q.ChinaGDP
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ChinaGDP()
if err != nil {
config.G_LOG.Error("find initGDP err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Info("CreateInBatches initGDP err", zap.Any("err", err))
return err
}
}
return err
}
// 社会消费品零售总额
func initMacroChinaConsumerGoodsRetail() error {
t := q.MacroChinaConsumerGoodsRetail
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.MacroChinaConsumerGoodsRetail()
if err != nil {
config.G_LOG.Info("find initMacroChinaConsumerGoodsRetail err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Info("CreateInBatches initMacroChinaConsumerGoodsRetail err", zap.Any("err", err))
return err
}
}
return err
}
// cpi
func initMacroChinaCpi() error {
t := q.ChinaCPI
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ChinaCPI()
if err != nil {
config.G_LOG.Error("find initMacroChinaCpi err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Error("CreateInBatches initMacroChinaCpi err", zap.Any("err", err))
return err
}
}
return err
}
// ppi
func initMacroPpi() error {
t := q.ChinaPPI
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ChinaPPI()
if err != nil {
config.G_LOG.Error("find initMacroPpi err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Error("CreateInBatches initMacroPpi err", zap.Any("err", err))
return err
}
}
return err
}
// 全社会用电
func initSocialElectricityConsumption() error {
t := q.SocialElectricityConsumption
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.SocialElectricityConsumption()
if err != nil {
config.G_LOG.Error("find initSocialElectricityConsumption err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 1024)
if err != nil {
config.G_LOG.Error("CreateInBatches initSocialElectricityConsumption err", zap.Any("err", err))
return err
}
}
return err
}
// 全社会客货运输量
func initPassengerAndFreightTraffic() error {
t := q.PassengerAndFreightTraffic
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.PassengerAndFreightTraffic()
if err != nil {
config.G_LOG.Error("find initPassengerAndFreightTraffic err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 2048)
if err != nil {
config.G_LOG.Error("CreateInBatches initPassengerAndFreightTraffic err", zap.Any("err", err))
return err
}
}
return err
}
// 工业生产增加值
func initValueAddedOfIndustrialProduction() error {
t := q.ValueAddedOfIndustrialProduction
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
v, err := macroscopic.ValueAddedOfIndustrialProduction()
if err != nil {
config.G_LOG.Error("find initValueAddedOfIndustrialProduction err", zap.Any("err", err))
return err
}
err = do.CreateInBatches(v, 512)
if err != nil {
config.G_LOG.Error("CreateInBatches initValueAddedOfIndustrialProduction err", zap.Any("err", err))
return err
}
}
return err
}
// 社会融资存量
func initSocialFinancingStock() error {
t := q.SocialFinancingStock
do := t.WithContext(context.Background())
_, err := do.First()
if errors.Is(err, gorm.ErrRecordNotFound) {
var htmlContent string
urlSli, err := getSocialFinancingStockUrlMap()
if err != nil {
config.G_LOG.Error("getSocialFinancingStockUrlMap err", zap.Any("err", err))
}
for _, v := range urlSli {
url := `http://www.pbc.gov.cn` + v
err = GetHttpHtmlContent(GetSocialFinancingStockHTML(url, `tbody`, `table`, &htmlContent))
if err != nil {
config.G_LOG.Error("GetHttpHtmlContent", zap.Any("err", err))
return err
}
datalist, err := ProcessedSocialFinancingStockTable(htmlContent)
if err != nil {
config.G_LOG.Error("ProcessedSocialFinancingStockTable", zap.Any("err", err))
return err
}
do.CreateInBatches(datalist, 512)
}
}
return err
}
// 获取社会融资url map
func getSocialFinancingStockUrlMap() ([]string, error) {
htmlContent := ""
urlSli := make([]string, 0)
url := `http://www.pbc.gov.cn/diaochatongjisi/116219/116319/index.html`
err := GetHttpHtmlContent(GetSocialFinancingStockHTML(url, `#l_con`, `.qhkuang2 > tbody`, &htmlContent))
if err != nil {
log.Fatal(err)
return urlSli, err
}
// dom选择器
dom, err := goquery.NewDocumentFromReader(strings.NewReader(htmlContent))
if err != nil {
log.Fatal(err)
return urlSli, err
}
dom.Find(`.guidlevel02_style2`).Each(func(i int, s *goquery.Selection) {
var html string
href, e := s.Attr("href")
if e {
content := strings.TrimSpace(s.Text())
if content == "社会融资规模" {
href = strings.TrimSpace(href)
// 进一步获取 所有talbe 的url
url := `http://www.pbc.gov.cn` + href
err := GetHttpHtmlContent(GetSocialFinancingStockHTML(url, `tbody`, `.border_nr > tbody`, &html))
if err != nil {
log.Fatal(err)
}
dom, err = goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
log.Fatal(err)
}
dom.Find("table:nth-child(2) a").Each(func(i int, s *goquery.Selection) {
content := strings.TrimSpace(s.Text())
href, e := s.Attr("href")
if e && content == "htm" {
if h := strings.TrimSpace(href); h != "" {
urlSli = append(urlSli, h)
}
}
})
}
}
})
return urlSli, nil
}
// 动态加载js
func GetHttpHtmlContent(tasks chromedp.Tasks) error {
options := []chromedp.ExecAllocatorOption{
chromedp.Flag("headless", true),
chromedp.Flag("blink-settings", "imagesEnabled=false"),
}
//初始化参数,先传一个空的数据
options = append(chromedp.DefaultExecAllocatorOptions[:], options...)
c, _ := chromedp.NewExecAllocator(context.Background(), options...)
// create context
chromeCtx, cancel := chromedp.NewContext(c, chromedp.WithLogf(log.Printf))
defer cancel()
// 执行一个空task, 用提前创建Chrome实例
chromedp.Run(chromeCtx, make([]chromedp.Action, 0, 1)...)
//创建一个上下文,超时时间为20s
timeoutCtx, cancel := context.WithTimeout(chromeCtx, 40*time.Second)
defer cancel()
// var htmlContent string
err := chromedp.Run(timeoutCtx, tasks)
if err != nil {
log.Fatalf("Run err : %v\n", err)
return err
}
return nil
}
// 获取html
func GetSocialFinancingStockHTML(url, waitVisible, sel string, htmlContent *string) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(url),
chromedp.WaitVisible(waitVisible),
chromedp.OuterHTML(sel, htmlContent, chromedp.ByQuery),
chromedp.Stop(),
}
}
func ProcessedSocialFinancingFlowTable(htmlDom string) ([]*model.SocialFinancingFlow, error) {
result := []*model.SocialFinancingFlow{}
dom, err := goquery.NewDocumentFromReader(strings.NewReader(htmlDom))
if err != nil {
config.G_LOG.Error(err.Error())
return nil, err
}
for i := 8; i < 20; i++ {
// list := make([]string, 0)
m := model.SocialFinancingFlow{}
ivalue := reflect.ValueOf(&m).Elem()
dom.Find(fmt.Sprintf("tbody > tr:nth-child(%d) td", i)).Each(func(i int, s *goquery.Selection) {
str := strings.TrimSpace(s.Text())
if s.Text() != "" && len(str) != 0 {
if i == 0 {
elem := ivalue.Field(0)
ti, err := time.Parse("2006.01", str)
if err != nil {
config.G_LOG.Error(err.Error())
}
elem.SetString(ti.Format(utils.DATE_FORMAT))
} else if i == 9 {
elem := ivalue.Field(8)
float, _ := strconv.ParseFloat(str, 32)
elem.SetFloat(float)
} else if i == 8 || i == 10 || i == 11 {
return
} else {
elem := ivalue.Field(i)
float, _ := strconv.ParseFloat(str, 32)
elem.SetFloat(float)
}
}
})
if m.Date != "" {
result = append(result, &m)
}
}
return result, err
}
// 处理 社会融资规模存量 HTML table
// return 二维数组
func ProcessedSocialFinancingStockTable(htmlDom string) ([]*model.SocialFinancingStock, error) {
dataList := make([][]string, 0)
macroChinaShrzgmList := []*model.SocialFinancingStock{}
dom, err := goquery.NewDocumentFromReader(strings.NewReader(htmlDom))
if err != nil {
config.G_LOG.Error(err.Error())
return macroChinaShrzgmList, err
}
// 获取日期
list := make([]string, 0)
dom.Find("tbody > tr:nth-child(5) td").Each(func(i int, s *goquery.Selection) {
if i == 0 {
return
}
str := strings.TrimSpace(s.Text())
if s.Text() != " " && len(str) != 0 {
list = append(list, str)
}
})
dataList = append(dataList, list)
timeSplit := strings.Split(list[0], ".")
// 22、21、20年 9~19 td
// 19年需要特殊处理
// 18年多了一项地方政府专项债券
// 17、16年 9~16 td
// 15年时间需要特殊处理
if timeSplit[0] == "2018" {
for i := 9; i < 18; i++ {
list := make([]string, 0)
dom.Find(fmt.Sprintf("tbody > tr:nth-child(%d) td", i)).Each(func(i int, s *goquery.Selection) {
if i == 0 {
return
}
str := strings.TrimSpace(s.Text())
if s.Text() != " " && len(str) != 0 {
list = append(list, str)
}
})
dataList = append(dataList, list)
}
socialFinancingStockConvertToObject1(¯oChinaShrzgmList, dataList, true)
} else if timeSplit[0] == "2015" {
dataList[0] = []string{"2015.3", "2015.6", "2015.9", "2015.12"}
for i := 9; i < 17; i++ {
list := make([]string, 0)
dom.Find(fmt.Sprintf("tbody > tr:nth-child(%d) td", i)).Each(func(i int, s *goquery.Selection) {
if i == 0 {
return
}
str := strings.TrimSpace(s.Text())
if s.Text() != " " && len(str) != 0 {
list = append(list, str)
}
})
dataList = append(dataList, list)
}
socialFinancingStockConvertToObject1(¯oChinaShrzgmList, dataList, false)
} else if timeSplit[0] == "2017" || timeSplit[0] == "2016" {
for i := 9; i < 17; i++ {
list := make([]string, 0)
dom.Find(fmt.Sprintf("tbody > tr:nth-child(%d) td", i)).Each(func(i int, s *goquery.Selection) {
if i == 0 {
return
}
str := strings.TrimSpace(s.Text())
if s.Text() != " " && len(str) != 0 {
list = append(list, str)
}
})
dataList = append(dataList, list)
}
// 转换为对象
socialFinancingStockConvertToObject1(¯oChinaShrzgmList, dataList, false)
} else if timeSplit[0] == "2019" {
for i := 9; i < 30; i += 2 {
list := make([]string, 0)
dom.Find(fmt.Sprintf("tbody > tr:nth-child(%d) td", i)).Each(func(i int, s *goquery.Selection) {
if i == 0 {
return
}
str := strings.TrimSpace(s.Text())
if s.Text() != " " && len(str) != 0 {
list = append(list, str)
}
})
dataList = append(dataList, list)
}
// 转换为对象
socialFinancingStockConvertToObject(¯oChinaShrzgmList, dataList)
} else {
for i := 9; i < 20; i++ {
list := make([]string, 0)
dom.Find(fmt.Sprintf("tbody > tr:nth-child(%d) td", i)).Each(func(i int, s *goquery.Selection) {
if i == 0 {
return
}
// fmt.Println("--->" + s.Text() + "<---")
if s.Text() != " " {
list = append(list, s.Text())
}
})
dataList = append(dataList, list)
}
// 转换为对象
socialFinancingStockConvertToObject(¯oChinaShrzgmList, dataList)
}
for _, v := range macroChinaShrzgmList {
te, err := time.Parse("2006.1", v.Date)
if err != nil {
config.G_LOG.Error(err.Error())
}
v.Date = te.Format("2006-01-02 15:04:05")
}
return macroChinaShrzgmList, err
}
// 将表格转换为对象
func socialFinancingStockConvertToObject(macroChinaShrzgmList *[]*model.SocialFinancingStock, dataList [][]string) {
// slice需要扩容的情况下,需要传入slice
count := len(dataList[1]) / 2
for i := 0; i < len(dataList[0]); i++ {
if i == count {
return
}
data := &model.SocialFinancingStock{
Date: dataList[0][i],
Tiosfs: dataList[1][i*2],
TiosfsGrowthRate: dataList[1][i*2+1],
Rmblaon: dataList[2][i*2],
RmblaonGrowthRate: dataList[2][i*2+1],
Forcloan: dataList[3][i*2],
ForcloanGrowthRate: dataList[3][i*2+1],
Entrustloan: dataList[4][i*2],
EntrustloanGrowthRate: dataList[4][i*2+1],
Trustloan: dataList[5][i*2],
TrustloanGrowthRate: dataList[5][i*2+1],
Ndbab: dataList[6][i*2],
NdbabGrowthRate: dataList[6][i*2+1],
Bibae: dataList[7][i*2],
BibaeGrowthRate: dataList[7][i*2+1],
GovernmentBonds: dataList[8][i*2],
GovernmentBondsGrowthRate: dataList[8][i*2+1],
Sfinfe: dataList[9][i*2],
SfinfeGrowthRate: dataList[9][i*2+1],
AssetBackedSecurities: dataList[10][i*2],
AssetBackedSecuritiesGrowthRate: dataList[10][i*2+1],
LoansWrittenOff: dataList[11][i*2],
LoansWrittenOffGrowthRate: dataList[11][i*2+1],
}
*macroChinaShrzgmList = append(*macroChinaShrzgmList, data)
}
}
func socialFinancingStockConvertToObject1(macroChinaShrzgmList *[]*model.SocialFinancingStock, dataList [][]string, is2018 bool) {
index := 8
if is2018 {
index = 9 // 除去地方政府专项债券
}
for i := 0; i < len(dataList[0]); i++ {
data := &model.SocialFinancingStock{
Date: dataList[0][i],
Tiosfs: dataList[1][i*2],
TiosfsGrowthRate: dataList[1][i*2+1],
Rmblaon: dataList[2][i*2],
RmblaonGrowthRate: dataList[2][i*2+1],
Forcloan: dataList[3][i*2],
ForcloanGrowthRate: dataList[3][i*2+1],
Entrustloan: dataList[4][i*2],
EntrustloanGrowthRate: dataList[4][i*2+1],
Trustloan: dataList[5][i*2],
TrustloanGrowthRate: dataList[5][i*2+1],
Ndbab: dataList[6][i*2],
NdbabGrowthRate: dataList[6][i*2+1],
Bibae: dataList[7][i*2],
BibaeGrowthRate: dataList[7][i*2+1],
Sfinfe: dataList[index][i*2],
SfinfeGrowthRate: dataList[index][i*2+1],
}
*macroChinaShrzgmList = append(*macroChinaShrzgmList, data)
}
}