commodity_trade_dalian.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package services
  2. import (
  3. "bytes"
  4. "eta/eta_crawler/models"
  5. "eta/eta_crawler/services/alarm_msg"
  6. "eta/eta_crawler/utils"
  7. "fmt"
  8. "github.com/PuerkitoBio/goquery"
  9. "github.com/mozillazg/go-pinyin"
  10. "io/ioutil"
  11. "log"
  12. "mime/multipart"
  13. "net/http"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type SearchList struct {
  19. VarietyName string `description:"商品名称"`
  20. CarietyCode string `description:"商品名称对应的编码"`
  21. List []SearchContractId
  22. ListSearch []*SearchContractId
  23. }
  24. type SearchContractId struct {
  25. ContractId string `description:"商品类型"`
  26. }
  27. // 同步 N天 之内的数据
  28. func SyncRankingFromDalianDo() {
  29. for i := 7; i >= 0; i-- {
  30. SyncRankingFromDalianSearch(i)
  31. }
  32. }
  33. // 大连交易所持单排名
  34. func SyncRankingFromDalianSearch(dayNum int) (err error) {
  35. fmt.Println("start")
  36. n := utils.GetRandInt(10, 120)
  37. time.Sleep(time.Duration(n) * time.Second)
  38. defer func() {
  39. if err != nil {
  40. fmt.Println("RefreshDataFromDalian Err:" + err.Error())
  41. msg := "失败提醒" + "RefreshDataFromDalian SyncRankingFromDalianSearch ErrMsg:" + err.Error()
  42. go alarm_msg.SendAlarmMsg(msg, 3)
  43. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromDalian ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  44. }
  45. }()
  46. //定义爬取时间
  47. endDate := time.Now().AddDate(0, 0, -dayNum).Format(utils.FormatDateTime)
  48. //endDate := time.Now().Format(utils.FormatDateTime)
  49. timeDate := utils.StrTimeToTime(endDate)
  50. //currDate := timeDate.Format(utils.FormatDateUnSpace)
  51. //year := timeDate.Year()
  52. //month := timeDate.Format("01")
  53. //var dayStr string
  54. //day := timeDate.Day()
  55. //if day < 10 {
  56. // dayStr = "0" + strconv.Itoa(day)
  57. //} else {
  58. // dayStr = strconv.Itoa(day)
  59. //}
  60. //monthNum, _ := strconv.Atoi(month)
  61. //month = strconv.Itoa(monthNum - 1) //获取时月份需要减一
  62. list, err := models.GetBaseFromTradeDalianDataList(timeDate.Format(utils.FormatDate))
  63. if err != nil {
  64. fmt.Println(err)
  65. return err
  66. }
  67. listDataMap := make(map[string]int)
  68. for _, v := range list {
  69. listDataMap[v.DealShortName+v.ClassifyType+v.DataTime] = v.BaseFromTradeDalianIndexId
  70. }
  71. var ContractId string
  72. //var CarietyCode string
  73. var VarietyName string
  74. //模拟form表单请求
  75. url := "http://www.dce.com.cn/publicweb/quotesdata/memberDealPosiQuotes.html"
  76. method := "POST"
  77. payload := &bytes.Buffer{}
  78. writer := multipart.NewWriter(payload)
  79. _ = writer.WriteField("memberDealPosiQuotes.variety", "c")
  80. _ = writer.WriteField("memberDealPosiQuotes.trade_type", "0")
  81. _ = writer.WriteField("year", strconv.Itoa(2024))
  82. _ = writer.WriteField("month", "4")
  83. _ = writer.WriteField("day", "10")
  84. _ = writer.WriteField("contract.contract_id", "c2405")
  85. _ = writer.WriteField("contract.variety_id", "c")
  86. //_ = writer.WriteField("currDate", currDate)
  87. err = writer.Close()
  88. if err != nil {
  89. utils.FileLog.Info("获取指标失败:" + VarietyName + ContractId)
  90. return err
  91. }
  92. client := &http.Client{}
  93. req, err := http.NewRequest(method, url, payload)
  94. if err != nil {
  95. return err
  96. }
  97. req.Header.Add("Cookie", "JSESSIONID=36ACF02A59227A3854F9D5D5E2FB5F2E; WMONID=R5ojcAIIcx-")
  98. req.Header.Set("Content-Type", writer.FormDataContentType())
  99. res, err := client.Do(req)
  100. if err != nil {
  101. fmt.Println("post SyncRankingFromDalianSearch err:"+err.Error())
  102. utils.FileLog.Info("post SyncRankingFromDalianSearch err:"+err.Error())
  103. return err
  104. }
  105. defer res.Body.Close()
  106. body, err := ioutil.ReadAll(res.Body)
  107. if err != nil {
  108. msg := "失败提醒" + "RefreshDataFromDalian ErrMsg:" + err.Error() + "获取指标失败:" + VarietyName + ContractId
  109. go alarm_msg.SendAlarmMsg(msg, 3)
  110. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromDalian ErrMsg:"+err.Error()+"获取指标失败:"+currDate+VarietyName+ContractId, utils.EmailSendToUsers)
  111. return err
  112. }
  113. exitProductMaps, _, varietyArrMaps := DoSearch(string(body))
  114. var items []*SearchList
  115. for k, v := range exitProductMaps {
  116. item := new(SearchList)
  117. item.VarietyName = v
  118. item.CarietyCode = varietyArrMaps[k]
  119. htmlBody, err := GetDalianHtmlBody(dayNum, "", item.CarietyCode, item.VarietyName)
  120. if err != nil {
  121. if strings.Contains(err.Error(),"timed out") {
  122. continue
  123. }
  124. return err
  125. }
  126. listContractId := DoSearchMap(htmlBody)
  127. item.ListSearch = listContractId
  128. items = append(items, item)
  129. }
  130. //for k, v := range items {
  131. // for _, v2 := range v.ListSearch {
  132. // fmt.Println(currDate, v.VarietyName, v2.ContractId, v.CarietyCode, k)
  133. // }
  134. //}
  135. SyncRankingFromDalian(dayNum, items)
  136. return err
  137. }
  138. // 处理搜索条件初始
  139. func DoSearch(body string) (exitProductMaps, exitContractIdMaps, varietyArrMaps map[int]string) {
  140. var str string
  141. str = body
  142. doc, err := goquery.NewDocumentFromReader(strings.NewReader(str))
  143. if err != nil {
  144. log.Fatal(err)
  145. }
  146. exitProductMap := make(map[int]string)
  147. exitContractIdMap := make(map[int]string)
  148. varietyArrMap := make(map[int]string)
  149. //var productName string
  150. ul := doc.Find(".selBox ul")
  151. var pNum int
  152. var cidNum int
  153. var vNum int
  154. ul.Each(func(i int, s *goquery.Selection) {
  155. //解析标签
  156. //fmt.Println(i, s.Text())
  157. ulTxt := s.Text()
  158. //fmt.Println(ulTxt)
  159. if ulTxt != "" && (i == 0 || i == 2) {
  160. ulTxtArr := strings.Split(ulTxt, "\n")
  161. for _, v := range ulTxtArr {
  162. v = strings.Replace(v, " ", "", -1)
  163. v = strings.Replace(v, "\n", "", -1)
  164. v = strings.Replace(v, " ", "", -1)
  165. if v != "" && len(v) > 0 {
  166. exitProductMap[pNum] = v
  167. pNum++
  168. }
  169. }
  170. }
  171. if ulTxt != "" && i == 3 {
  172. //fmt.Println(ulTxt)
  173. cidTxtArr := strings.Split(ulTxt, " ")
  174. for _, v := range cidTxtArr {
  175. v = strings.Replace(v, "\n", "", -1)
  176. v = strings.Replace(v, " ", "", -1)
  177. v = strings.Replace(v, " ", "", -1)
  178. if v != "" {
  179. exitContractIdMap[cidNum] = v
  180. cidNum++
  181. }
  182. }
  183. }
  184. })
  185. varietyArr := strings.Split(str, "onclick=\"javascript:setVariety('")
  186. for _, v := range varietyArr {
  187. strnum := strings.Index(v, "');")
  188. if strnum > 0 {
  189. varietyStr := v[0:strnum]
  190. if len(varietyStr) < 10 {
  191. //fmt.Println(strnum, varietyStr)
  192. varietyArrMap[vNum] = varietyStr
  193. vNum++
  194. }
  195. }
  196. }
  197. exitProductMaps = exitProductMap
  198. exitContractIdMaps = exitContractIdMap
  199. varietyArrMaps = varietyArrMap
  200. return
  201. }
  202. // 处理搜索条件
  203. func DoSearchMap(body string) (items []*SearchContractId) {
  204. var str string
  205. str = body
  206. doc, err := goquery.NewDocumentFromReader(strings.NewReader(str))
  207. if err != nil {
  208. log.Fatal(err)
  209. }
  210. exitContractIdMap := make(map[int]string)
  211. ul := doc.Find(".selBox ul")
  212. var cidNum int
  213. ul.Each(func(i int, s *goquery.Selection) {
  214. //解析标签
  215. ulTxt := s.Text()
  216. if ulTxt != "" && i == 3 {
  217. cidTxtArr := strings.Split(ulTxt, " ")
  218. for _, v := range cidTxtArr {
  219. v = strings.Replace(v, "\n", "", -1)
  220. v = strings.Replace(v, " ", "", -1)
  221. v = strings.Replace(v, " ", "", -1)
  222. if v != "" {
  223. exitContractIdMap[cidNum] = v
  224. cidNum++
  225. }
  226. }
  227. }
  228. })
  229. for _, v := range exitContractIdMap {
  230. item := new(SearchContractId)
  231. item.ContractId = v
  232. items = append(items, item)
  233. }
  234. return items
  235. }
  236. // 处理解析Html
  237. func DoHtml(body, name, contractId string, dateTime time.Time, listDataMap map[string]int, listIndexCodeMap map[string]string, listDataMapVal map[string]int) (err error) {
  238. defer func() {
  239. if err != nil {
  240. fmt.Println("RefreshDataFromDaLian Err:" + err.Error())
  241. msg := "失败提醒" + "RefreshDataFromDalian ErrMsg:" + err.Error()
  242. go alarm_msg.SendAlarmMsg(msg, 3)
  243. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromDaLian ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  244. }
  245. }()
  246. str := body
  247. doc, err := goquery.NewDocumentFromReader(strings.NewReader(str))
  248. if err != nil {
  249. log.Fatal(err)
  250. }
  251. var isAdd bool
  252. addSql := ` INSERT INTO base_from_trade_dalian_index(rank,deal_short_name,deal_name,deal_code,deal_value,buy_short_name,deal_change,buy_name,buy_code,buy_value,buy_change,sold_short_name,sold_name,sold_code,sold_value,sold_change,frequency,classify_name,classify_type,create_time,modify_time,data_time) values `
  253. table := doc.Find("table")
  254. var rank, shortName, dealValue, dealChange, buyName, buyValue, buyChange, soldName, soldValue, soldChange string
  255. table.Find("tr").Each(func(i int, tr *goquery.Selection) {
  256. tds := tr.Find("td")
  257. //fmt.Println(tds.Length(), "长度:", i)
  258. if tds.Length() == 0 || tds.Length() == 7 {
  259. tdText := tds.Text()
  260. utils.FileLog.Info(tdText)
  261. } else {
  262. item := new(models.BaseFromTradeDalianIndex)
  263. tds.Each(func(tk int, td *goquery.Selection) {
  264. tdText := td.Text()
  265. tdText = strings.Replace(tdText, "(代客)", "", -1)
  266. if tk == 0 { //名次
  267. if tdText != " " {
  268. rank = tdText
  269. }
  270. }
  271. if tk == 1 { //会员简称
  272. shortName = tdText
  273. }
  274. if tk == 2 { //成交量
  275. dealValue = strings.Replace(tdText, ",", "", -1)
  276. }
  277. if tk == 3 { //增减
  278. dealChange = strings.Replace(tdText, ",", "", -1)
  279. }
  280. if tk == 4 { //名次
  281. if tdText != " " {
  282. rank = tdText
  283. }
  284. }
  285. if tk == 5 { //会员简称
  286. buyName = tdText
  287. }
  288. if tk == 6 { //持买单量
  289. buyValue = strings.Replace(tdText, ",", "", -1)
  290. }
  291. if tk == 7 { //增减
  292. buyChange = strings.Replace(tdText, ",", "", -1)
  293. }
  294. if tk == 8 { //名次
  295. if tdText != " " {
  296. rank = tdText
  297. }
  298. }
  299. if tk == 9 { //会员简称
  300. soldName = tdText
  301. }
  302. if tk == 10 { //持卖单量
  303. soldValue = strings.Replace(tdText, ",", "", -1)
  304. }
  305. if tk == 11 { //增减
  306. soldChange = strings.Replace(tdText, ",", "", -1)
  307. }
  308. })
  309. item.Rank = rank
  310. item.DealShortName = shortName
  311. item.DealName = shortName + "_" + contractId + "_成交量"
  312. item.DealValue = dealValue
  313. item.BuyShortName = buyName
  314. item.DealChange = dealChange
  315. item.BuyName = buyName + "_" + contractId + "_持买单量"
  316. item.BuyValue = buyValue
  317. item.BuyChange = buyChange
  318. item.SoldShortName = soldName
  319. item.SoldName = soldName + "_" + contractId + "_持卖单量"
  320. item.SoldValue = soldValue
  321. item.SoldChange = soldChange
  322. item.Frequency = "日度"
  323. item.ClassifyName = name
  324. item.ClassifyType = contractId
  325. item.CreateTime = time.Now().Format(utils.FormatDateTime)
  326. item.ModifyTime = time.Now().Format(utils.FormatDateTime)
  327. item.DataTime = dateTime.Format(utils.FormatDate)
  328. //处理指标Id
  329. if i != 23 {
  330. if val, ok := listIndexCodeMap[item.DealName]; ok {
  331. item.DealCode = val
  332. } else {
  333. item.DealCode = GetIndexCodeGeneratorPinYing(shortName, item.DealName, contractId, "deal", "DL")
  334. }
  335. if val, ok := listIndexCodeMap[item.BuyName]; ok {
  336. item.BuyCode = val
  337. } else {
  338. item.BuyCode = GetIndexCodeGeneratorPinYing(buyName, item.BuyName, contractId, "buy", "DL")
  339. }
  340. if val, ok := listIndexCodeMap[item.SoldName]; ok {
  341. item.SoldCode = val
  342. } else {
  343. item.SoldCode = GetIndexCodeGeneratorPinYing(soldName, item.SoldName, contractId, "sold", "DL")
  344. }
  345. }
  346. if i == 23 {
  347. item.Rank = "999"
  348. item.DealName = "top20_" + contractId + "_成交量(手)"
  349. if val, ok := listIndexCodeMap[item.DealName]; ok {
  350. item.DealCode = val
  351. } else {
  352. item.DealCode = GetIndexCodeGeneratorPinYing("top20", item.DealName, contractId, "deal", "DL")
  353. }
  354. item.BuyName = "top20_" + contractId + "_持买单量(手)"
  355. if val, ok := listIndexCodeMap[item.BuyName]; ok {
  356. item.BuyCode = val
  357. } else {
  358. item.BuyCode = GetIndexCodeGeneratorPinYing("top20", item.BuyName, contractId, "buy", "DL")
  359. }
  360. item.SoldName = "top20_" + contractId + "_持卖单量(手)"
  361. if val, ok := listIndexCodeMap[item.SoldName]; ok {
  362. item.SoldCode = val
  363. } else {
  364. item.SoldCode = GetIndexCodeGeneratorPinYing("top20", item.SoldName, contractId, "sold", "DL")
  365. }
  366. }
  367. if val, ok := listDataMap[item.DealShortName+item.ClassifyType+item.DataTime]; !ok {
  368. addSql += models.GetAddSql(item)
  369. isAdd = true
  370. } else {
  371. //更新
  372. if listDataMapVal[item.DealValue+item.BuyValue+item.SoldValue] != val {
  373. err := models.UpdateBaseFromTradeDalianIndex(item, val)
  374. if err != nil {
  375. fmt.Println("UpdateBaseFromTradeDalianIndex err:", err)
  376. }
  377. }
  378. }
  379. }
  380. })
  381. addSql = strings.TrimRight(addSql, ",")
  382. if isAdd {
  383. err = models.RefreshEdbDataByDaLian(addSql)
  384. if err != nil {
  385. return err
  386. }
  387. }
  388. return
  389. }
  390. func DlIndexCodeGenerator(indexName, suffix string) (ineIndexCode string) {
  391. ineIndexCode = fmt.Sprintf("DL%s", strconv.FormatInt(time.Now().UnixNano(), 10)+suffix)
  392. err := models.AddBaseFromTradeMapping(indexName, ineIndexCode, "DL")
  393. if err != nil {
  394. fmt.Println("add Code err:", err)
  395. }
  396. return ineIndexCode
  397. }
  398. func GetIndexCodeGeneratorPinYing(shortName, indexName, contractCode, suffix, exchange string) string {
  399. if shortName == "" {
  400. indexCode = ""
  401. return indexCode
  402. }
  403. if shortName == "top20" {
  404. indexCode = "top20" + contractCode + suffix
  405. } else {
  406. //取公司的全拼
  407. a := pinyin.NewArgs()
  408. rows := pinyin.Pinyin(shortName, a)
  409. strResult := ""
  410. for i := 0; i < len(rows); i++ {
  411. if len(rows[i]) != 0 {
  412. strResult += rows[i][0]
  413. }
  414. }
  415. indexCode = strResult + contractCode + suffix
  416. }
  417. err := models.AddBaseFromTradeMapping(indexName, indexCode, exchange)
  418. if err != nil {
  419. fmt.Println("add Code err:", err)
  420. }
  421. return indexCode
  422. }
  423. // 大连交易所持单排名
  424. func GetDalianHtmlBody(dayNum int, contractId, carietyCode, varietyName string) (body string, err error) {
  425. defer func() {
  426. if err != nil {
  427. fmt.Println("GetDalianHtmlBody Err:" + err.Error())
  428. msg := "失败提醒" + "GetDalianHtmlBody ErrMsg:" + err.Error()
  429. go alarm_msg.SendAlarmMsg(msg, 3)
  430. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetDalianHtmlBody ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  431. }
  432. }()
  433. n := utils.GetRandInt(5, 20)
  434. time.Sleep(time.Duration(n) * time.Second)
  435. //定义爬取时间
  436. endDate := time.Now().AddDate(0, 0, -dayNum).Format(utils.FormatDateTime)
  437. //endDate := time.Now().Format(utils.FormatDateTime)
  438. timeDate := utils.StrTimeToTime(endDate)
  439. currDate := timeDate.Format(utils.FormatDateUnSpace)
  440. year := timeDate.Year()
  441. month := timeDate.Format("01")
  442. var dayStr string
  443. day := timeDate.Day()
  444. if day < 10 {
  445. dayStr = "0" + strconv.Itoa(day)
  446. } else {
  447. dayStr = strconv.Itoa(day)
  448. }
  449. monthNum, _ := strconv.Atoi(month)
  450. month = strconv.Itoa(monthNum - 1) //获取时月份需要减一
  451. list, err := models.GetBaseFromTradeDalianDataList(timeDate.Format(utils.FormatDate))
  452. listDataMap := make(map[string]int)
  453. for _, v := range list {
  454. listDataMap[v.DealShortName+v.ClassifyType+v.DataTime] = v.BaseFromTradeDalianIndexId
  455. }
  456. if err != nil {
  457. fmt.Println(err)
  458. return
  459. }
  460. //模拟form表单请求
  461. url := "http://www.dce.com.cn/publicweb/quotesdata/memberDealPosiQuotes.html"
  462. method := "POST"
  463. payload := &bytes.Buffer{}
  464. writer := multipart.NewWriter(payload)
  465. _ = writer.WriteField("memberDealPosiQuotes.variety", carietyCode)
  466. _ = writer.WriteField("memberDealPosiQuotes.trade_type", "0")
  467. _ = writer.WriteField("year", strconv.Itoa(year))
  468. _ = writer.WriteField("month", month)
  469. _ = writer.WriteField("day", dayStr)
  470. _ = writer.WriteField("contract.contract_id", contractId)
  471. _ = writer.WriteField("contract.variety_id", carietyCode)
  472. _ = writer.WriteField("currDate", currDate)
  473. err = writer.Close()
  474. if err != nil {
  475. utils.FileLog.Info("获取指标失败:" + currDate + varietyName + contractId)
  476. return
  477. }
  478. client := &http.Client{}
  479. req, err := http.NewRequest(method, url, payload)
  480. if err != nil {
  481. return
  482. }
  483. req.Header.Add("Cookie", "JSESSIONID=36ACF02A59227A3854F9D5D5E2FB5F2E; WMONID=R5ojcAIIcx-")
  484. req.Header.Set("Content-Type", writer.FormDataContentType())
  485. res, err := client.Do(req)
  486. if err != nil {
  487. return
  488. }
  489. defer res.Body.Close()
  490. htmlBody, err := ioutil.ReadAll(res.Body)
  491. if err != nil {
  492. msg := "失败提醒" + "GetDalianHtmlBody ErrMsg:" + err.Error() + "获取指标失败:" + currDate + varietyName + contractId
  493. go alarm_msg.SendAlarmMsg(msg, 3)
  494. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetDalianHtmlBody ErrMsg:"+err.Error()+"获取指标失败:"+currDate+varietyName+contractId, utils.EmailSendToUsers)
  495. return
  496. }
  497. body = string(htmlBody)
  498. return
  499. }
  500. // 大连交易所持单排名
  501. func SyncRankingFromDalian(dayNum int, searchList []*SearchList) (err error) {
  502. fmt.Println("start")
  503. defer func() {
  504. if err != nil {
  505. fmt.Println("RefreshDataFromDalian SyncRankingFromDalian Err:" + err.Error())
  506. msg := "失败提醒" + "RefreshDataFromDalian SyncRankingFromDalian ErrMsg:" + err.Error()
  507. go alarm_msg.SendAlarmMsg(msg, 3)
  508. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromDalian ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  509. }
  510. }()
  511. //定义爬取时间
  512. endDate := time.Now().AddDate(0, 0, -dayNum).Format(utils.FormatDateTime)
  513. //endDate := time.Now().Format(utils.FormatDateTime)
  514. timeDate := utils.StrTimeToTime(endDate)
  515. currDate := timeDate.Format(utils.FormatDateUnSpace)
  516. year := timeDate.Year()
  517. month := timeDate.Format("01")
  518. var dayStr string
  519. day := timeDate.Day()
  520. if day < 10 {
  521. dayStr = "0" + strconv.Itoa(day)
  522. } else {
  523. dayStr = strconv.Itoa(day)
  524. }
  525. monthNum, _ := strconv.Atoi(month)
  526. month = strconv.Itoa(monthNum - 1) //获取时月份需要减一
  527. list, err := models.GetBaseFromTradeDalianDataList(timeDate.Format(utils.FormatDate))
  528. listDataMap := make(map[string]int)
  529. listDataMapVal := make(map[string]int)
  530. for _, v := range list {
  531. listDataMap[v.DealShortName+v.ClassifyType+v.DataTime] = v.BaseFromTradeDalianIndexId
  532. }
  533. for _, v := range list {
  534. listDataMapVal[v.DealValue+v.BuyValue+v.SoldValue] = v.BaseFromTradeDalianIndexId
  535. }
  536. if err != nil {
  537. fmt.Println(err)
  538. return err
  539. }
  540. listIndexCode, err := models.GetIndexCodeMapList("DL") //获取往期指标
  541. if err != nil {
  542. fmt.Println(err)
  543. return err
  544. }
  545. listIndexCodeMap := make(map[string]string)
  546. for _, v := range listIndexCode {
  547. listIndexCodeMap[v.IndexName] = v.IndexCode
  548. }
  549. for _, v := range searchList {
  550. for _, v2 := range v.ListSearch {
  551. n := utils.GetRandInt(5, 20)
  552. time.Sleep(time.Duration(n) * time.Second)
  553. //模拟form表单请求
  554. url := "http://www.dce.com.cn/publicweb/quotesdata/memberDealPosiQuotes.html"
  555. method := "POST"
  556. payload := &bytes.Buffer{}
  557. writer := multipart.NewWriter(payload)
  558. _ = writer.WriteField("memberDealPosiQuotes.variety", v.CarietyCode)
  559. _ = writer.WriteField("memberDealPosiQuotes.trade_type", "0")
  560. _ = writer.WriteField("year", strconv.Itoa(year))
  561. _ = writer.WriteField("month", month)
  562. _ = writer.WriteField("day", dayStr)
  563. _ = writer.WriteField("contract.contract_id", v2.ContractId)
  564. _ = writer.WriteField("contract.variety_id", v.CarietyCode)
  565. _ = writer.WriteField("currDate", currDate)
  566. err := writer.Close()
  567. fmt.Println(currDate, v.VarietyName, v2.ContractId)
  568. if err != nil {
  569. utils.FileLog.Info("获取指标失败:" + currDate + v.VarietyName + v2.ContractId)
  570. return err
  571. }
  572. client := &http.Client{}
  573. req, err := http.NewRequest(method, url, payload)
  574. if err != nil {
  575. return err
  576. }
  577. req.Header.Add("Cookie", "JSESSIONID=36ACF02A59227A3854F9D5D5E2FB5F2E; WMONID=R5ojcAIIcx-")
  578. req.Header.Set("Content-Type", writer.FormDataContentType())
  579. res, err := client.Do(req)
  580. if err != nil {
  581. if strings.Contains(err.Error(),"timed out") {
  582. continue
  583. }
  584. fmt.Println("post SyncRankingFromDalian err:"+err.Error())
  585. utils.FileLog.Info("post SyncRankingFromDalian err:"+err.Error())
  586. return err
  587. }
  588. defer res.Body.Close()
  589. body, err := ioutil.ReadAll(res.Body)
  590. if err != nil {
  591. msg := "失败提醒" + "RefreshDataFromDalian ErrMsg:" + err.Error() + "获取指标失败:" + currDate + v.VarietyName + v2.ContractId
  592. go alarm_msg.SendAlarmMsg(msg, 3)
  593. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromDalian ErrMsg:"+err.Error()+"获取指标失败:"+currDate+v.VarietyName+v2.ContractId, utils.EmailSendToUsers)
  594. return err
  595. }
  596. err = DoHtml(string(body), v.VarietyName, v2.ContractId, timeDate, listDataMap, listIndexCodeMap, listDataMapVal)
  597. if err != nil {
  598. return err
  599. }
  600. }
  601. }
  602. return err
  603. }