commodity_trade_zhengzhou.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hongze_data_crawler/models"
  5. "hongze/hongze_data_crawler/utils"
  6. "log"
  7. "rdluck_tools/http"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/PuerkitoBio/goquery"
  12. )
  13. var zIndexCode string
  14. var zIndexCodeMap = make(map[string]string)
  15. var zActionCodeMap = make(map[string]map[string]int)
  16. func zIndexCodeGenerator(indexName string) string {
  17. zIndexCode,_ := zIndexCodeMap[indexName]
  18. if zIndexCode == "" {
  19. zIndexCode = fmt.Sprintf("Z%s", time.Now().Format(utils.FormatDateTimeUnSpace)+strconv.Itoa(utils.GetRandInt(1, 100)))
  20. zIndexCodeMap[indexName] = zIndexCode
  21. err := models.AddBaseFromTradeMapping(indexName, zIndexCode, "Z")
  22. if err != nil {
  23. fmt.Println("add Code err:", err)
  24. }
  25. }
  26. return zIndexCode
  27. }
  28. //郑州商品交易所持单排名
  29. func SyncRankingFromZhengzhou() {
  30. fmt.Println("start")
  31. var err error
  32. defer func() {
  33. if err != nil {
  34. fmt.Println("Err:" + err.Error())
  35. }
  36. }()
  37. for i := 180; i > 0; i-- {
  38. var itemVerifyCode int
  39. zzUrl := "http://www.czce.com.cn/cn/DFSStaticFiles/Future/%s/%s/FutureDataHolding.htm"
  40. date := time.Now().AddDate(0, 0, -i)
  41. year := date.Year()
  42. dateStr := date.Format(utils.FormatDateUnSpace)
  43. zzUrl = fmt.Sprintf(zzUrl, strconv.Itoa(year), dateStr)
  44. fmt.Println(zzUrl)
  45. body, err := http.Get(zzUrl)
  46. if err != nil {
  47. fmt.Println("GetData Err:" + err.Error())
  48. return
  49. }
  50. //获取所有指标信息 某一天的
  51. allIndex, err := models.GetBaseFromTradeZhengzhouIndexAll(dateStr)
  52. if err != nil {
  53. return
  54. }
  55. existIndexMap := make(map[string]*models.BaseFromTradeZhengzhouIndex)
  56. for _, v := range allIndex {
  57. indexKey := v.DealName + v.BuyName + v.SoldName
  58. existIndexMap[indexKey] = v
  59. }
  60. bodyStr := string(body)
  61. utils.FileLog.Info(bodyStr)
  62. if strings.Contains(bodyStr, "404.htm") {
  63. continue
  64. }
  65. doc, err := goquery.NewDocumentFromReader(strings.NewReader(bodyStr))
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. table := doc.Find("table")
  70. var classifyName, classifyType, tradeDate string
  71. var dealSuffix, dealAddCutSuffix, buySuffix, buyAddCutSuffix, sellSuffix, sellAddCutSuffix string
  72. var dealVal, dealAddCutVal, buyVal, buyAddCutVal, sellVal, sellAddCutVal string
  73. var rank string
  74. table.Find("tr").Each(func(i int, tr *goquery.Selection) {
  75. var memberShortNameArr []string
  76. tds := tr.Find("td")
  77. if tds.Length() == 1 {
  78. tdText := tds.Text()
  79. utils.FileLog.Info(tdText)
  80. if tdText != "" {
  81. tdTextArr := strings.Split(tdText, "    ")
  82. for k, v := range tdTextArr {
  83. //fmt.Println(k, v)
  84. if k == 0 {
  85. classifyName = v
  86. } else {
  87. tradeDate = v
  88. }
  89. }
  90. }
  91. } else {
  92. tds.Each(func(tk int, td *goquery.Selection) {
  93. tdText := td.Text()
  94. tdText = strings.Trim(tdText, " ")
  95. if tdText == "" {
  96. return
  97. }
  98. //fmt.Println("tdText: ", tdText)
  99. if tk == 0 {
  100. if !(strings.Contains(tdText, "名次") || strings.Contains(tdText, "合计")){
  101. rank = tdText
  102. }
  103. }
  104. if tk == 1 {
  105. if !strings.Contains(tdText, "会员简称") {
  106. memberShortNameArr = append(memberShortNameArr, tdText)
  107. }
  108. }
  109. if tk == 4 {
  110. if !strings.Contains(tdText, "会员简称") {
  111. memberShortNameArr = append(memberShortNameArr, tdText)
  112. }
  113. }
  114. if tk == 7 {
  115. if !strings.Contains(tdText, "会员简称") {
  116. memberShortNameArr = append(memberShortNameArr, tdText)
  117. }
  118. }
  119. if tk == 2 {
  120. if strings.Contains(tdText, "成交量") {
  121. dealSuffix = tdText
  122. } else {
  123. dealVal = tdText
  124. }
  125. }
  126. if tk == 3 {
  127. if strings.Contains(tdText, "增减量") {
  128. dealAddCutSuffix = tdText
  129. } else {
  130. dealAddCutVal = tdText
  131. }
  132. }
  133. if tk == 5 {
  134. if strings.Contains(tdText, "持买仓量") {
  135. buySuffix = tdText
  136. } else {
  137. buyVal = tdText
  138. }
  139. }
  140. if tk == 6 {
  141. if strings.Contains(tdText, "增减量") {
  142. buyAddCutSuffix = tdText
  143. } else {
  144. buyAddCutVal = tdText
  145. }
  146. }
  147. if tk == 8 {
  148. if strings.Contains(tdText, "持卖仓量") {
  149. sellSuffix = tdText
  150. } else {
  151. sellVal = tdText
  152. }
  153. }
  154. if tk == 9 {
  155. if strings.Contains(tdText, "增减量") {
  156. sellAddCutSuffix = tdText
  157. } else {
  158. sellAddCutVal = tdText
  159. }
  160. }
  161. })
  162. //处理指标
  163. dealVal = strings.Replace(dealVal, ",", "", -1)
  164. dealAddCutVal = strings.Replace(dealAddCutVal, ",", "", -1)
  165. buyVal = strings.Replace(buyVal, ",", "", -1)
  166. buyAddCutVal = strings.Replace(buyAddCutVal, ",", "", -1)
  167. sellVal = strings.Replace(sellVal, ",", "", -1)
  168. sellAddCutVal = strings.Replace(sellAddCutVal, ",", "", -1)
  169. tradeDate = strings.Replace(tradeDate, "日期:", "", -1)
  170. classifyNameArr := strings.Split(classifyName, ":")
  171. if len(classifyNameArr) > 1 {
  172. classifyType = classifyNameArr[0]
  173. classifyName = classifyNameArr[1]
  174. }
  175. //fmt.Printf("memberShortNameArr:%s,len:%d\n", memberShortNameArr, len(memberShortNameArr))
  176. if len(memberShortNameArr) != 0 {
  177. if len(memberShortNameArr[0]) != 2 {
  178. item := new(models.BaseFromTradeZhengzhouIndex)
  179. item.Rank, _ = strconv.Atoi(rank)
  180. item.DealShortName = memberShortNameArr[0]
  181. item.DealName = memberShortNameArr[0] + "_" + dealSuffix + "_" + classifyName + "_" + classifyType
  182. item.DealCode = zIndexCodeGenerator(item.DealName)
  183. item.DealValue, _ = strconv.Atoi(dealVal)
  184. item.DealChange, _ = strconv.Atoi(dealAddCutVal)
  185. item.BuyShortName = memberShortNameArr[1]
  186. item.BuyName = memberShortNameArr[1] + "_" + buySuffix + "_" + classifyName + "_" + classifyType
  187. item.BuyCode = zIndexCodeGenerator(item.BuyName)
  188. item.BuyValue, _ = strconv.Atoi(buyVal)
  189. item.BuyChange, _ = strconv.Atoi(buyAddCutVal)
  190. item.SoldShortName = memberShortNameArr[2]
  191. item.SoldName = memberShortNameArr[2] + "_" + sellSuffix + "_" + classifyName + "_" + classifyType
  192. item.SoldCode = zIndexCodeGenerator(item.SoldName)
  193. item.SoldValue, _ = strconv.Atoi(sellVal)
  194. item.SoldChange, _ = strconv.Atoi(sellAddCutVal)
  195. item.Frequency = "日度"
  196. item.ClassifyName = classifyName
  197. item.ClassifyType = classifyType
  198. item.CreateTime = time.Now()
  199. item.ModifyTime = time.Now()
  200. item.DataTime = tradeDate
  201. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  202. if existIndex, ok := existIndexMap[item.DealName + item.BuyName + item.SoldName]; !ok {
  203. newID, err := models.AddBaseFromTradeZhengzhouIndex(item)
  204. if err != nil {
  205. fmt.Println("insert error:", err)
  206. }
  207. fmt.Println("insert new indexID:", newID)
  208. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  209. //更新
  210. err := models.ModifyBaseFromTradeZhengzhouIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeZhengzhouIndexId)
  211. if err != nil {
  212. fmt.Println("data update err:", err)
  213. }
  214. }
  215. }
  216. }
  217. }
  218. })
  219. }
  220. fmt.Println("end")
  221. }