commodity_trade_shanghai.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "time"
  10. )
  11. type Position []struct {
  12. ContractCode string `json:"INSTRUMENTID"`
  13. ProductSortNo int `json:"PRODUCTSORTNO"`
  14. Rank int `json:"RANK"`
  15. ParticipantID1 string `json:"PARTICIPANTID1"`
  16. ParticipantName1 string `json:"PARTICIPANTABBR1"`
  17. Deal interface{} `json:"CJ1"`
  18. Change1 interface{} `json:"CJ1_CHG"`
  19. ParticipantID2 string `json:"PARTICIPANTID2"`
  20. ParticipantName2 string `json:"PARTICIPANTABBR2"`
  21. BuyIn interface{} `json:"CJ2"`
  22. Change2 interface{} `json:"CJ2_CHG"`
  23. ParticipantID3 string `json:"PARTICIPANTID3"`
  24. ParticipantName3 string `json:"PARTICIPANTABBR3"`
  25. SoldOut interface{} `json:"CJ3"`
  26. Change3 interface{} `json:"CJ3_CHG"`
  27. ProductName string `json:"PRODUCTNAME"`
  28. }
  29. type Message struct {
  30. Position Position `json:"o_cursor"`
  31. Length string `json:"showlength"`
  32. Code string `json:"o_code"`
  33. Msg string `json:"o_msg"`
  34. ReportDate string `json:"report_date"`
  35. UpdateDate string `json:"update_date"`
  36. PrintDate string `json:"print_date"`
  37. }
  38. // SyncRankingFromShangHai 上海商品交易所持单排名
  39. func SyncRankingFromShangHai() {
  40. var err error
  41. defer func() {
  42. if err != nil {
  43. msg := "失败提醒" + "SyncRankingFromShangHai ErrMsg:" + err.Error()
  44. fmt.Println("msg:", msg)
  45. go alarm_msg.SendAlarmMsg(msg, 3)
  46. }
  47. }()
  48. //获取新的指标信息
  49. for i := 1; i >= 0; i-- {
  50. var message Message
  51. var resp BaseResponse
  52. shUrl := "https://www.shfe.com.cn/data/tradedata/future/dailydata/pm%s.dat"
  53. date := time.Now().AddDate(0, 0, -i)
  54. dateStr := date.Format(utils.FormatDateUnSpace)
  55. shUrl = fmt.Sprintf(shUrl, dateStr)
  56. utils.FileLog.Info("shUrl:" + shUrl)
  57. req, _ := http.NewRequest("GET", shUrl, nil)
  58. 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")
  59. client := http.Client{}
  60. res, err := client.Do(req)
  61. if err != nil {
  62. fmt.Println(err)
  63. return
  64. }
  65. defer res.Body.Close()
  66. // 读取响应的内容
  67. body, err := ioutil.ReadAll(res.Body)
  68. if err != nil {
  69. fmt.Println(err)
  70. return
  71. }
  72. err = json.Unmarshal(body, &message)
  73. if err != nil {
  74. fmt.Println("Unmarshal Err:", err)
  75. utils.FileLog.Info("Unmarshal Err:" + err.Error())
  76. continue
  77. }
  78. param := make(map[string]interface{})
  79. param["Date"] = date.Format(utils.FormatDate)
  80. param["Data"] = message
  81. urlStr := `exchange_crawler/refresh/sh`
  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. utils.FileLog.Info("HttpPost Err:" + err.Error())
  92. fmt.Println(err)
  93. return
  94. }
  95. err = json.Unmarshal(result, &resp)
  96. if err != nil {
  97. utils.FileLog.Info("Unmarshal resp Err:" + err.Error())
  98. return
  99. }
  100. }
  101. utils.FileLog.Info("end")
  102. fmt.Println("end")
  103. }
  104. type BaseResponse struct {
  105. Ret int
  106. Msg string
  107. ErrMsg string
  108. ErrCode string
  109. Data interface{}
  110. Success bool `description:"true 执行成功,false 执行失败"`
  111. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  112. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  113. }