commodity_trade_ine.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_crawler_local/services/alarm_msg"
  5. "eta/eta_crawler_local/utils"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. type JSONData struct {
  13. OCursor []OCursor `json:"o_cursor"`
  14. OCode interface{} `json:"o_code"`
  15. OMsg string `json:"o_msg"`
  16. ReportDate string `json:"report_date"`
  17. UpdateDate string `json:"update_date"`
  18. }
  19. type OCursor struct {
  20. Instrumentid string `json:"INSTRUMENTID"`
  21. Participantid3 string `json:"PARTICIPANTID3"`
  22. Participantid2 string `json:"PARTICIPANTID2"`
  23. Participantid1 string `json:"PARTICIPANTID1"`
  24. Participantabbr3 string `json:"PARTICIPANTABBR3"`
  25. Participantabbr2 string `json:"PARTICIPANTABBR2"`
  26. Rank int `json:"RANK"`
  27. Participantabbr1 string `json:"PARTICIPANTABBR1"`
  28. BuyIn interface{} `json:"CJ2"`
  29. Deal interface{} `json:"CJ1"`
  30. Change1 interface{} `json:"CJ1_CHG"`
  31. Change3 interface{} `json:"CJ3_CHG"`
  32. Productname string `json:"Productname"`
  33. Productsortno interface{} `json:"PRODUCTSORTNO"`
  34. SoldOut interface{} `json:"CJ3"`
  35. Change2 interface{} `json:"CJ2_CHG"`
  36. }
  37. // SyncRankingFromIne 上海能源交易中心持单排名
  38. func SyncRankingFromIne() {
  39. var err error
  40. defer func() {
  41. if err != nil {
  42. msg := "失败提醒" + "SyncRankingFromIne ErrMsg:" + err.Error()
  43. go alarm_msg.SendAlarmMsg(msg, 3)
  44. }
  45. }()
  46. for i := 25; i >= 0; i-- {
  47. var resp BaseResponse
  48. var message JSONData
  49. zzUrl := "https://www.ine.com.cn/data/tradedata/future/dailydata/pm%s.dat"
  50. date := time.Now().AddDate(0, 0, -i)
  51. dateStr := date.Format(utils.FormatDateUnSpace)
  52. zzUrl = fmt.Sprintf(zzUrl, dateStr)
  53. fmt.Println(zzUrl)
  54. utils.FileLog.Info("zzUrl:" + zzUrl)
  55. req, _ := http.NewRequest("GET", zzUrl, nil)
  56. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36")
  57. client := http.Client{}
  58. res, err := client.Do(req)
  59. if err != nil {
  60. fmt.Println(err)
  61. return
  62. }
  63. defer res.Body.Close()
  64. fmt.Println("resp:", res.Body)
  65. // 读取响应的内容
  66. body, err := ioutil.ReadAll(res.Body)
  67. if err != nil {
  68. fmt.Println(err)
  69. return
  70. }
  71. err = json.Unmarshal(body, &message)
  72. if err != nil {
  73. fmt.Println("Unmarshal Err:", err)
  74. utils.FileLog.Info("Unmarshal Err:" + err.Error())
  75. continue
  76. }
  77. //fmt.Println("body:",string(body))
  78. param := make(map[string]interface{})
  79. param["Date"] = date.Format(utils.FormatDate)
  80. param["Data"] = message
  81. urlStr := `exchange_crawler/refresh/ine`
  82. postUrl := utils.EDB_LIB_URL + urlStr
  83. postData, err := json.Marshal(param)
  84. if err != nil {
  85. utils.FileLog.Info("Marshal Err:" + err.Error())
  86. fmt.Println(err)
  87. return
  88. }
  89. result, err := HttpPost(postUrl, string(postData), "application/json")
  90. if err != nil {
  91. fmt.Println(err)
  92. utils.FileLog.Info("HttpPost Err:" + err.Error())
  93. return
  94. }
  95. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  96. err = json.Unmarshal(result, &resp)
  97. if err != nil {
  98. fmt.Println(err)
  99. utils.FileLog.Info("Unmarshal resp Err:" + err.Error())
  100. return
  101. }
  102. }
  103. utils.FileLog.Info("end")
  104. fmt.Println("end")
  105. }
  106. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  107. body := ioutil.NopCloser(strings.NewReader(postData))
  108. client := &http.Client{}
  109. req, err := http.NewRequest("POST", url, body)
  110. if err != nil {
  111. return nil, err
  112. }
  113. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  114. if len(params) > 0 && params[0] != "" {
  115. contentType = params[0]
  116. }
  117. req.Header.Set("Content-Type", contentType)
  118. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  119. resp, err := client.Do(req)
  120. defer resp.Body.Close()
  121. b, err := ioutil.ReadAll(resp.Body)
  122. fmt.Println("HttpPost:" + string(b))
  123. return b, err
  124. }