commodity_trade_zhengzhou.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/mozillazg/go-pinyin"
  5. "github.com/rdlucklib/rdluck_tools/http"
  6. "hongze/hongze_data_crawler/models"
  7. "hongze/hongze_data_crawler/utils"
  8. "log"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/PuerkitoBio/goquery"
  13. )
  14. var zIndexCode string
  15. var zIndexCodeMap = make(map[string]string)
  16. var zActionCodeMap = make(map[string]map[string]int)
  17. func zIndexCodeGenerator(shortName, indexName, contractCode, suffix string) string {
  18. if shortName == "" || shortName == "-"{
  19. return ""
  20. }
  21. strResult := ""
  22. if shortName != "top20" {
  23. //取公司全拼
  24. a := pinyin.NewArgs()
  25. rows := pinyin.LazyPinyin(shortName, a)
  26. for i:=0;i<len(rows);i++{
  27. strResult += rows[i]
  28. }
  29. } else {
  30. strResult = "top20"
  31. }
  32. //郑州特殊处理,当合约号有中文时只取英文代号 如 苹果AP ---> AP
  33. if len(contractCode) > 7 {
  34. contractCode = contractCode[len(contractCode)-2:]
  35. }
  36. zIndexCode, _ := zIndexCodeMap[indexName]
  37. if zIndexCode == "" {
  38. zIndexCode = strResult + contractCode + suffix
  39. zIndexCodeMap[indexName] = zIndexCode
  40. err := models.AddBaseFromTradeMapping(indexName, zIndexCode, "Z")
  41. if err != nil {
  42. fmt.Println("add Code err:", err)
  43. }
  44. }
  45. return zIndexCode
  46. }
  47. //郑州商品交易所持单排名
  48. func SyncRankingFromZhengzhou() {
  49. n := utils.GetRandInt(10, 120)
  50. time.Sleep(time.Duration(n) * time.Second)
  51. fmt.Println("start")
  52. var err error
  53. defer func() {
  54. if err != nil {
  55. fmt.Println("Err:" + err.Error())
  56. }
  57. }()
  58. allCode, err := models.GetIndexCodeFromMapping("Z")
  59. if err != nil {
  60. fmt.Println("select Code err:", err)
  61. }
  62. for _, item := range allCode {
  63. zIndexCodeMap[item.IndexName] = item.IndexCode
  64. }
  65. for i := 2; i >= 0; i-- {
  66. var itemVerifyCode int
  67. zzUrl := "http://www.czce.com.cn/cn/DFSStaticFiles/Future/%s/%s/FutureDataHolding.htm"
  68. date := time.Now().AddDate(0, 0, -i)
  69. year := date.Year()
  70. dateStr := date.Format(utils.FormatDateUnSpace)
  71. zzUrl = fmt.Sprintf(zzUrl, strconv.Itoa(year), dateStr)
  72. fmt.Println(zzUrl)
  73. body, err := http.Get(zzUrl)
  74. if err != nil {
  75. fmt.Println("GetData Err:" + err.Error())
  76. return
  77. }
  78. //获取所有指标信息 某一天的
  79. allIndex, err := models.GetBaseFromTradeZhengzhouIndexAll(dateStr)
  80. if err != nil {
  81. return
  82. }
  83. existIndexMap := make(map[string]*models.BaseFromTradeZhengzhouIndex)
  84. for _, v := range allIndex {
  85. indexKey := v.DealName + v.BuyName + v.SoldName
  86. existIndexMap[indexKey] = v
  87. }
  88. bodyStr := string(body)
  89. utils.FileLog.Info(bodyStr)
  90. if strings.Contains(bodyStr, "404.htm") {
  91. continue
  92. }
  93. doc, err := goquery.NewDocumentFromReader(strings.NewReader(bodyStr))
  94. if err != nil {
  95. log.Fatal(err)
  96. }
  97. table := doc.Find("table")
  98. var classifyName, classifyType, tradeDate string
  99. var dealSuffix, dealAddCutSuffix, buySuffix, buyAddCutSuffix, sellSuffix, sellAddCutSuffix string
  100. var dealVal, dealAddCutVal, buyVal, buyAddCutVal, sellVal, sellAddCutVal string
  101. var rank string
  102. table.Find("tr").Each(func(i int, tr *goquery.Selection) {
  103. var memberShortNameArr []string
  104. tds := tr.Find("td")
  105. if tds.Length() == 1 {
  106. tdText := tds.Text()
  107. utils.FileLog.Info(tdText)
  108. if tdText != "" {
  109. tdTextArr := strings.Split(tdText, "    ")
  110. for k, v := range tdTextArr {
  111. //fmt.Println(k, v)
  112. if k == 0 {
  113. classifyName = v
  114. } else {
  115. tradeDate = v
  116. }
  117. }
  118. }
  119. } else {
  120. tds.Each(func(tk int, td *goquery.Selection) {
  121. tdText := td.Text()
  122. tdText = strings.Trim(tdText, " ")
  123. if tdText == "" {
  124. return
  125. }
  126. //fmt.Println("tdText: ", tdText)
  127. if tk == 0 {
  128. if !(strings.Contains(tdText, "名次") || strings.Contains(tdText, "合计")) {
  129. rank = tdText
  130. }
  131. }
  132. if tk == 1 {
  133. if !strings.Contains(tdText, "会员简称") {
  134. memberShortNameArr = append(memberShortNameArr, tdText)
  135. }
  136. }
  137. if tk == 4 {
  138. if !strings.Contains(tdText, "会员简称") {
  139. memberShortNameArr = append(memberShortNameArr, tdText)
  140. }
  141. }
  142. if tk == 7 {
  143. if !strings.Contains(tdText, "会员简称") {
  144. memberShortNameArr = append(memberShortNameArr, tdText)
  145. }
  146. }
  147. if tk == 2 {
  148. if strings.Contains(tdText, "成交量") {
  149. dealSuffix = tdText
  150. } else {
  151. dealVal = tdText
  152. }
  153. }
  154. if tk == 3 {
  155. if strings.Contains(tdText, "增减量") {
  156. dealAddCutSuffix = tdText
  157. } else {
  158. dealAddCutVal = tdText
  159. }
  160. }
  161. if tk == 5 {
  162. if strings.Contains(tdText, "持买仓量") {
  163. buySuffix = tdText
  164. } else {
  165. buyVal = tdText
  166. }
  167. }
  168. if tk == 6 {
  169. if strings.Contains(tdText, "增减量") {
  170. buyAddCutSuffix = tdText
  171. } else {
  172. buyAddCutVal = tdText
  173. }
  174. }
  175. if tk == 8 {
  176. if strings.Contains(tdText, "持卖仓量") {
  177. sellSuffix = tdText
  178. } else {
  179. sellVal = tdText
  180. }
  181. }
  182. if tk == 9 {
  183. if strings.Contains(tdText, "增减量") {
  184. sellAddCutSuffix = tdText
  185. } else {
  186. sellAddCutVal = tdText
  187. }
  188. }
  189. })
  190. //处理指标
  191. dealVal = strings.Replace(dealVal, ",", "", -1)
  192. dealAddCutVal = strings.Replace(dealAddCutVal, ",", "", -1)
  193. buyVal = strings.Replace(buyVal, ",", "", -1)
  194. buyAddCutVal = strings.Replace(buyAddCutVal, ",", "", -1)
  195. sellVal = strings.Replace(sellVal, ",", "", -1)
  196. sellAddCutVal = strings.Replace(sellAddCutVal, ",", "", -1)
  197. tradeDate = strings.Replace(tradeDate, "日期:", "", -1)
  198. classifyNameArr := strings.Split(classifyName, ":")
  199. if len(classifyNameArr) > 1 {
  200. classifyType = classifyNameArr[0]
  201. classifyName = classifyNameArr[1]
  202. }
  203. //fmt.Printf("memberShortNameArr:%s,len:%d\n", memberShortNameArr, len(memberShortNameArr))
  204. if len(memberShortNameArr) != 0 {
  205. if len(memberShortNameArr[0]) != 2 {
  206. if len(classifyName) > 7 {
  207. classifyName = classifyName[len(classifyName)-2:]
  208. }
  209. item := new(models.BaseFromTradeZhengzhouIndex)
  210. item.Rank, _ = strconv.Atoi(rank)
  211. item.DealShortName = memberShortNameArr[0]
  212. item.DealName = memberShortNameArr[0] + "_" + classifyName + "_" + dealSuffix
  213. item.DealCode = zIndexCodeGenerator(item.DealShortName, item.DealName, classifyName, "deal")
  214. item.DealValue, _ = strconv.Atoi(dealVal)
  215. item.DealChange, _ = strconv.Atoi(dealAddCutVal)
  216. item.BuyShortName = memberShortNameArr[1]
  217. item.BuyName = memberShortNameArr[1] + "_" + classifyName + "_" + buySuffix
  218. item.BuyCode = zIndexCodeGenerator(item.BuyShortName, item.BuyName, classifyName, "buy")
  219. item.BuyValue, _ = strconv.Atoi(buyVal)
  220. item.BuyChange, _ = strconv.Atoi(buyAddCutVal)
  221. item.SoldShortName = memberShortNameArr[2]
  222. item.SoldName = memberShortNameArr[2] + "_" + classifyName + "_" + sellSuffix
  223. item.SoldCode = zIndexCodeGenerator(item.SoldShortName, item.SoldName, classifyName, "sold")
  224. item.SoldValue, _ = strconv.Atoi(sellVal)
  225. item.SoldChange, _ = strconv.Atoi(sellAddCutVal)
  226. item.Frequency = "日度"
  227. item.ClassifyName = classifyName
  228. item.ClassifyType = classifyType
  229. item.CreateTime = time.Now()
  230. item.ModifyTime = time.Now()
  231. item.DataTime = tradeDate
  232. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  233. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  234. newID, err := models.AddBaseFromTradeZhengzhouIndex(item)
  235. if err != nil {
  236. fmt.Println("insert error:", err)
  237. }
  238. fmt.Println("insert new indexID:", newID)
  239. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  240. //更新
  241. err := models.ModifyBaseFromTradeZhengzhouIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeZhengzhouIndexId)
  242. if err != nil {
  243. fmt.Println("data update err:", err)
  244. }
  245. }
  246. } else {
  247. //合计
  248. if len(classifyName) > 7 {
  249. classifyName = classifyName[len(classifyName)-2:]
  250. }
  251. item := new(models.BaseFromTradeZhengzhouIndex)
  252. item.Rank = 999
  253. item.DealShortName = ""
  254. item.DealName = "top20" + "_" + classifyName + "_" + dealSuffix
  255. item.DealCode = zIndexCodeGenerator("top20", item.DealName, classifyName, "deal")
  256. item.DealValue, _ = strconv.Atoi(dealVal)
  257. item.DealChange, _ = strconv.Atoi(dealAddCutVal)
  258. item.BuyShortName = ""
  259. item.BuyName = "top20" + "_" + classifyName + "_" + buySuffix
  260. item.BuyCode = zIndexCodeGenerator("top20", item.BuyName, classifyName, "buy")
  261. item.BuyValue, _ = strconv.Atoi(buyVal)
  262. item.BuyChange, _ = strconv.Atoi(buyAddCutVal)
  263. item.SoldShortName = ""
  264. item.SoldName = "top20" + "_" + classifyName + "_" + sellSuffix
  265. item.SoldCode = zIndexCodeGenerator("top20", item.SoldName, classifyName, "sold")
  266. item.SoldValue, _ = strconv.Atoi(sellVal)
  267. item.SoldChange, _ = strconv.Atoi(sellAddCutVal)
  268. item.Frequency = "日度"
  269. item.ClassifyName = classifyName
  270. item.ClassifyType = classifyType
  271. item.CreateTime = time.Now()
  272. item.ModifyTime = time.Now()
  273. item.DataTime = tradeDate
  274. itemVerifyCode = item.BuyValue + item.DealValue + item.SoldValue
  275. if existIndex, ok := existIndexMap[item.DealName+item.BuyName+item.SoldName]; !ok {
  276. newID, err := models.AddBaseFromTradeZhengzhouIndex(item)
  277. if err != nil {
  278. fmt.Println("insert error:", err)
  279. }
  280. fmt.Println("insert new indexID:", newID)
  281. } else if existIndex != nil && itemVerifyCode != (existIndex.DealValue+existIndex.BuyValue+existIndex.SoldValue) {
  282. //更新
  283. err := models.ModifyBaseFromTradeZhengzhouIndex(item.DealValue, item.BuyValue, item.SoldValue, existIndex.BaseFromTradeZhengzhouIndexId)
  284. if err != nil {
  285. fmt.Println("data update err:", err)
  286. }
  287. }
  288. }
  289. }
  290. }
  291. })
  292. }
  293. fmt.Println("end")
  294. }