icpi.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_crawler/models"
  6. "eta/eta_crawler/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. "time"
  12. )
  13. func CrawlerIcpi(cont context.Context) (err error) {
  14. dataIndexUrl := `http://www.bdecon.com/dataIndex`
  15. resp, err := http.Get(dataIndexUrl)
  16. fmt.Println(err)
  17. cookieLen := len(resp.Cookies())
  18. var cookieStr string
  19. for i := cookieLen - 1; i >= 0; i-- {
  20. cookie := resp.Cookies()[i]
  21. fmt.Println(i, cookie.Name, cookie.Value)
  22. cookieStr += cookie.Name + "=" + cookie.Value + ";"
  23. }
  24. cookieStr = strings.Trim(cookieStr, ";")
  25. fmt.Println(cookieStr)
  26. //获取分类
  27. CrawlerIcpiClassify(cookieStr)
  28. //大类
  29. CrawlerIcpiMaxClassifyIndex(cookieStr)
  30. //中类
  31. CrawlerIcpiMiddleClassifyIndex(cookieStr)
  32. return err
  33. }
  34. func CrawlerIcpiClassify(cookieStr string) (err error) {
  35. // 创建一个HTTP客户端
  36. client := &http.Client{}
  37. // 创建一个GET请求
  38. //data := strings.NewReader("laber=1")
  39. req, err := http.NewRequest("POST", "http://www.bdecon.com/monthly/LargeWeightListNew1/", nil)
  40. if err != nil {
  41. fmt.Println("创建请求失败:", err)
  42. return
  43. }
  44. // 设置请求头
  45. req.Header.Set("Accept", "*/*")
  46. req.Header.Set("Accept-Encoding", "gzip, deflate")
  47. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
  48. req.Header.Set("Connection", "keep-alive")
  49. req.Header.Set("Content-Length", "7")
  50. req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  51. req.Header.Set("Cookie", cookieStr)
  52. req.Header.Set("Host", "www.bdecon.com")
  53. req.Header.Set("Origin", "http://www.bdecon.com")
  54. req.Header.Set("Referer", "http://www.bdecon.com/dataIndex")
  55. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
  56. req.Header.Set("X-Requested-With", "XMLHttpRequest")
  57. // 发送请求并获取响应
  58. resp, err := client.Do(req)
  59. if err != nil {
  60. fmt.Println("发送请求失败:", err)
  61. return
  62. }
  63. defer resp.Body.Close()
  64. // 读取响应的内容
  65. body, err := ioutil.ReadAll(resp.Body)
  66. if err != nil {
  67. fmt.Println("读取响应失败:", err)
  68. return
  69. }
  70. utils.FileLog.Info("result:")
  71. utils.FileLog.Info(string(body))
  72. // 打印响应内容
  73. fmt.Println(string(body))
  74. classifyList := make([]models.IcpiClassify, 0)
  75. err = json.Unmarshal(body, &classifyList)
  76. if err != nil {
  77. fmt.Println("CrawlerIcpi json.Unmarshal Err:" + err.Error())
  78. return
  79. }
  80. icpiObj := new(models.BaseFromIcpiIndex)
  81. classifyAll, err := icpiObj.GetBaseFromIcpiClassifyAll()
  82. if err != nil {
  83. fmt.Println("GetBaseFromIcpiClassifyAll Err:" + err.Error())
  84. return
  85. }
  86. classifyMap := make(map[string]*models.BaseFromIcpiClassify)
  87. for _, v := range classifyAll {
  88. classifyMap[v.ClassifyName] = v
  89. }
  90. for _, v := range classifyList {
  91. if v.Category == "总类" {
  92. v.Category = "日总类"
  93. }
  94. if _, ok := classifyMap[v.Category]; !ok {
  95. classifyItem := new(models.BaseFromIcpiClassify)
  96. classifyItem.ClassifyName = v.Category
  97. classifyItem.ClassifyNameEn = v.Categoryenglish
  98. classifyItem.ParentId = 0
  99. classifyItem.CreateTime = time.Now()
  100. classifyItem.ModifyTime = time.Now()
  101. err = icpiObj.AddClassify(classifyItem)
  102. if err != nil {
  103. return err
  104. }
  105. }
  106. }
  107. return err
  108. }
  109. // 爬取ICPI指数大类及指标数据
  110. func CrawlerIcpiMaxClassifyIndex(cookieStr string) (err error) {
  111. // 创建一个HTTP客户端
  112. client := &http.Client{}
  113. // 创建一个GET请求
  114. icpiObj := new(models.BaseFromIcpiIndex)
  115. classifyAll, err := icpiObj.GetBaseFromIcpiClassifyAll()
  116. if err != nil {
  117. fmt.Println("GetBaseFromIcpiClassifyAll Err:" + err.Error())
  118. return
  119. }
  120. classifyMap := make(map[string]*models.BaseFromIcpiClassify)
  121. for _, v := range classifyAll {
  122. classifyMap[v.ClassifyName] = v
  123. }
  124. param := strings.NewReader("laber=1")
  125. req, err := http.NewRequest("POST", "http://www.bdecon.com/indexqueryiCPI/", param)
  126. if err != nil {
  127. fmt.Println("创建请求失败:", err)
  128. return
  129. }
  130. // 设置请求头
  131. req.Header.Set("Accept", "*/*")
  132. req.Header.Set("Accept-Encoding", "gzip, deflate")
  133. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
  134. req.Header.Set("Connection", "keep-alive")
  135. req.Header.Set("Content-Length", "7")
  136. req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  137. req.Header.Set("Cookie", cookieStr)
  138. req.Header.Set("Host", "www.bdecon.com")
  139. req.Header.Set("Origin", "http://www.bdecon.com")
  140. req.Header.Set("Referer", "http://www.bdecon.com/dataIndex")
  141. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
  142. req.Header.Set("X-Requested-With", "XMLHttpRequest")
  143. // 发送请求并获取响应
  144. resp, err := client.Do(req)
  145. if err != nil {
  146. fmt.Println("发送请求失败:", err)
  147. return
  148. }
  149. defer resp.Body.Close()
  150. // 读取响应的内容
  151. body, err := ioutil.ReadAll(resp.Body)
  152. if err != nil {
  153. fmt.Println("读取响应失败:", err)
  154. return
  155. }
  156. utils.FileLog.Info("result:")
  157. utils.FileLog.Info(string(body))
  158. // 打印响应内容
  159. fmt.Println(string(body))
  160. indexList := make([]IcpiItem, 0)
  161. err = json.Unmarshal(body, &indexList)
  162. if err != nil {
  163. fmt.Println("CrawlerIcpiMaxClassifyIndex json.Unmarshal Err:" + err.Error())
  164. return
  165. }
  166. icpiIndexObj := new(models.BaseFromIcpiIndex)
  167. indexAll, err := icpiIndexObj.GetBaseFromIcpiIndexAll()
  168. if err != nil {
  169. fmt.Println("GetBaseFromIcpiIndexAll Err:" + err.Error())
  170. return
  171. }
  172. indexMap := make(map[string]*models.BaseFromIcpiIndex)
  173. for _, v := range indexAll {
  174. indexMap[v.IndexCode] = v
  175. }
  176. limit := 2
  177. for _, v := range indexList {
  178. indexCode = strings.ReplaceAll(v.EnglishName, " ", "_")
  179. if indexItem, ok := indexMap[indexCode]; !ok { //新增指标
  180. icpiIndexObj = new(models.BaseFromIcpiIndex)
  181. icpiIndexObj.IndexCode = indexCode
  182. if classify, ok := classifyMap[v.Name]; ok {
  183. icpiIndexObj.BaseFromIcpiClassifyId = classify.BaseFromIcpiClassifyId
  184. }
  185. icpiIndexObj.IndexName = v.Name
  186. icpiIndexObj.Frequency = "日度"
  187. icpiIndexObj.Unit = "%"
  188. icpiIndexObj.CreateTime = time.Now()
  189. icpiIndexObj.ModifyTime = time.Now()
  190. err = icpiIndexObj.Add()
  191. if err != nil {
  192. fmt.Println("icpiIndexObj Add Err:" + err.Error())
  193. return err
  194. }
  195. //新增指标对应的数据
  196. dataItem := new(models.BaseFromIcpiData)
  197. dataItem.IndexCode = indexCode
  198. dataItem.BaseFromIcpiIndexId = icpiIndexObj.BaseFromIcpiIndexId
  199. dataItem.DataTime = time.Now().Format(utils.FormatDate)
  200. dataItem.Value = v.Today
  201. dataItem.CreateTime = time.Now()
  202. dataItem.ModifyTime = time.Now()
  203. err = icpiIndexObj.AddData(dataItem)
  204. if err != nil {
  205. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  206. return err
  207. }
  208. //新增指标对应的数据
  209. dataItem = new(models.BaseFromIcpiData)
  210. dataItem.IndexCode = indexCode
  211. dataItem.BaseFromIcpiIndexId = icpiIndexObj.BaseFromIcpiIndexId
  212. dataItem.DataTime = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  213. dataItem.Value = v.Yesterday
  214. dataItem.CreateTime = time.Now()
  215. dataItem.ModifyTime = time.Now()
  216. err = icpiIndexObj.AddData(dataItem)
  217. if err != nil {
  218. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  219. return err
  220. }
  221. } else {
  222. dataList, err := icpiIndexObj.GetBaseFromIcpiData(indexCode, limit)
  223. if err != nil {
  224. fmt.Println("GetBaseFromIcpiIndexAll Err:" + err.Error())
  225. return err
  226. }
  227. dataMap := make(map[string]string)
  228. for _, dv := range dataList {
  229. dataMap[dv.DataTime] = dv.Value
  230. }
  231. today := time.Now().Format(utils.FormatDate)
  232. if dataVal, ok := dataMap[today]; !ok {
  233. dataItem := new(models.BaseFromIcpiData)
  234. dataItem.IndexCode = indexCode
  235. dataItem.BaseFromIcpiIndexId = indexItem.BaseFromIcpiIndexId
  236. dataItem.DataTime = today
  237. dataItem.Value = v.Today
  238. dataItem.CreateTime = time.Now()
  239. dataItem.ModifyTime = time.Now()
  240. err = icpiIndexObj.AddData(dataItem)
  241. if err != nil {
  242. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  243. return err
  244. }
  245. } else {
  246. if dataVal != v.Today {
  247. err = icpiIndexObj.ModifyData(indexCode, today, v.Today)
  248. if err != nil {
  249. fmt.Println("icpiIndexObj ModifyData Err:" + err.Error())
  250. return err
  251. }
  252. }
  253. }
  254. yesterday := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  255. if dataVal, ok := dataMap[yesterday]; !ok {
  256. dataItem := new(models.BaseFromIcpiData)
  257. dataItem.IndexCode = indexCode
  258. dataItem.BaseFromIcpiIndexId = indexItem.BaseFromIcpiIndexId
  259. dataItem.DataTime = yesterday
  260. dataItem.Value = v.Yesterday
  261. dataItem.CreateTime = time.Now()
  262. dataItem.ModifyTime = time.Now()
  263. err = icpiIndexObj.AddData(dataItem)
  264. if err != nil {
  265. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  266. return err
  267. }
  268. } else {
  269. if dataVal != v.Yesterday {
  270. err = icpiIndexObj.ModifyData(indexCode, yesterday, v.Yesterday)
  271. if err != nil {
  272. fmt.Println("icpiIndexObj ModifyData Err:" + err.Error())
  273. return err
  274. }
  275. }
  276. }
  277. }
  278. maxItem, err := icpiIndexObj.GetBaseFromIcpiIndexMaxAndMinInfo(indexCode)
  279. if err != nil {
  280. fmt.Println("GetBaseFromIcpiIndexMaxAndMinInfo Err:" + err.Error())
  281. return err
  282. }
  283. err = icpiIndexObj.ModifyEdbInfoMaxAndMinInfo(indexCode, maxItem)
  284. if err != nil {
  285. fmt.Println("ModifyEdbInfoMaxAndMinInfo Err:" + err.Error())
  286. return err
  287. }
  288. }
  289. return err
  290. }
  291. // 爬取ICPI中类指标信息
  292. func CrawlerIcpiMiddleClassifyIndex(cookieStr string) (err error) {
  293. // 创建一个HTTP客户端
  294. client := &http.Client{}
  295. // 创建一个GET请求
  296. icpiObj := new(models.BaseFromIcpiIndex)
  297. classifyAll, err := icpiObj.GetBaseFromIcpiClassifyAll()
  298. if err != nil {
  299. fmt.Println("GetBaseFromIcpiClassifyAll Err:" + err.Error())
  300. return
  301. }
  302. classifyMap := make(map[string]*models.BaseFromIcpiClassify)
  303. for _, v := range classifyAll {
  304. classifyMap[v.ClassifyName] = v
  305. }
  306. params := strings.NewReader("name=yueda=总类,食品烟酒,衣着,居住,生活用品及服务,交通和通信,教育文化和娱乐,医疗保健,其他用品和服务&yuezh=衣着——服装,衣着——服装材料,衣着——其他衣着及配件,衣着——衣着加工服务费,衣着——鞋类,交通和通信——交通,交通和通信——通信,生活用品及服务——家具及室内装饰品,生活用品及服务——家用器具,生活用品及服务——家用纺织品,生活用品及服务——家庭日用杂品,生活用品及服务——个人护理用品,生活用品及服务——家庭服务,居住——租赁房房租,居住——住房保养维修及管理,居住——水电燃料,居住——自有住房,医疗保健——药品及医疗器具,医疗保健——医疗服务,食品烟酒——食品,食品烟酒——茶及饮料,食品烟酒——烟酒,食品烟酒——在外餐饮,教育文化和娱乐——教育,教育文化和娱乐——文化娱乐,其他用品和服务——其他用品类,其他用品和服务——其他服务类&yuexi=&yuezi=&zhishu=日总类")
  307. req, err := http.NewRequest("POST", "http://www.bdecon.com/indexqueryNew/", params)
  308. if err != nil {
  309. fmt.Println("创建请求失败:", err)
  310. return
  311. }
  312. // 设置请求头
  313. req.Header.Set("Accept", "*/*")
  314. req.Header.Set("Accept-Encoding", "gzip, deflate")
  315. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
  316. req.Header.Set("Connection", "keep-alive")
  317. req.Header.Set("Content-Length", "7")
  318. req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  319. req.Header.Set("Cookie", cookieStr)
  320. req.Header.Set("Host", "www.bdecon.com")
  321. req.Header.Set("Origin", "http://www.bdecon.com")
  322. req.Header.Set("Referer", "http://www.bdecon.com/dataIndex")
  323. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
  324. req.Header.Set("X-Requested-With", "XMLHttpRequest")
  325. // 发送请求并获取响应
  326. resp, err := client.Do(req)
  327. if err != nil {
  328. fmt.Println("发送请求失败:", err)
  329. return
  330. }
  331. defer resp.Body.Close()
  332. // 读取响应的内容
  333. body, err := ioutil.ReadAll(resp.Body)
  334. if err != nil {
  335. fmt.Println("读取响应失败:", err)
  336. return
  337. }
  338. utils.FileLog.Info("result:")
  339. utils.FileLog.Info(string(body))
  340. // 打印响应内容
  341. fmt.Println(string(body))
  342. indexList := make([]*IcpiItem, 0)
  343. err = json.Unmarshal(body, &indexList)
  344. if err != nil {
  345. fmt.Println("CrawlerIcpiMiddleClassifyIndex json.Unmarshal Err:" + err.Error())
  346. return
  347. }
  348. icpiIndexObj := new(models.BaseFromIcpiIndex)
  349. indexAll, err := icpiIndexObj.GetBaseFromIcpiIndexAll()
  350. if err != nil {
  351. fmt.Println("GetBaseFromIcpiIndexAll Err:" + err.Error())
  352. return
  353. }
  354. indexMap := make(map[string]*models.BaseFromIcpiIndex)
  355. for _, v := range indexAll {
  356. indexMap[v.IndexCode] = v
  357. }
  358. limit := 2
  359. for _, v := range indexList {
  360. var indexCode string
  361. if v.EnglishName == "" {
  362. indexCode = utils.ChineseToPinyinInitials(v.Name)
  363. } else {
  364. indexCode = strings.ReplaceAll(v.EnglishName, " ", "_")
  365. }
  366. if indexCode == "" {
  367. continue
  368. }
  369. dataList, err := icpiIndexObj.GetBaseFromIcpiData(indexCode, limit)
  370. if err != nil {
  371. fmt.Println("GetBaseFromIcpiIndexAll Err:" + err.Error())
  372. return err
  373. }
  374. dataMap := make(map[string]string)
  375. for _, dv := range dataList {
  376. dataMap[dv.DataTime] = dv.Value
  377. }
  378. if indexItem, ok := indexMap[indexCode]; !ok { //新增指标
  379. classifyNameArr := strings.Split(v.Name, "-")
  380. classifyName := classifyNameArr[0]
  381. icpiIndexObj = new(models.BaseFromIcpiIndex)
  382. icpiIndexObj.IndexCode = indexCode
  383. icpiIndexObj.IndexName = v.Name
  384. if classifyItem, ok := classifyMap[classifyName]; ok {
  385. icpiIndexObj.BaseFromIcpiClassifyId = classifyItem.BaseFromIcpiClassifyId
  386. }
  387. icpiIndexObj.Unit = "%"
  388. icpiIndexObj.Frequency = "日度"
  389. icpiIndexObj.CreateTime = time.Now()
  390. icpiIndexObj.ModifyTime = time.Now()
  391. err = icpiIndexObj.Add()
  392. if err != nil {
  393. fmt.Println("icpiIndexObj Add Err:" + err.Error())
  394. return err
  395. }
  396. //新增指标对应的数据
  397. dataItem := new(models.BaseFromIcpiData)
  398. dataItem.IndexCode = indexCode
  399. dataItem.BaseFromIcpiIndexId = icpiIndexObj.BaseFromIcpiIndexId
  400. dataItem.DataTime = time.Now().Format(utils.FormatDate)
  401. dataItem.Value = v.Today
  402. dataItem.CreateTime = time.Now()
  403. dataItem.ModifyTime = time.Now()
  404. err = icpiIndexObj.AddData(dataItem)
  405. if err != nil {
  406. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  407. return err
  408. }
  409. //新增指标对应的数据
  410. dataItem = new(models.BaseFromIcpiData)
  411. dataItem.IndexCode = indexCode
  412. dataItem.BaseFromIcpiIndexId = icpiIndexObj.BaseFromIcpiIndexId
  413. dataItem.DataTime = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  414. dataItem.Value = v.Yesterday
  415. dataItem.CreateTime = time.Now()
  416. dataItem.ModifyTime = time.Now()
  417. err = icpiIndexObj.AddData(dataItem)
  418. if err != nil {
  419. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  420. return err
  421. }
  422. } else {
  423. today := time.Now().Format(utils.FormatDate)
  424. if dataVal, ok := dataMap[today]; !ok {
  425. dataItem := new(models.BaseFromIcpiData)
  426. dataItem.IndexCode = indexCode
  427. dataItem.BaseFromIcpiIndexId = indexItem.BaseFromIcpiIndexId
  428. dataItem.DataTime = today
  429. dataItem.Value = v.Today
  430. dataItem.CreateTime = time.Now()
  431. dataItem.ModifyTime = time.Now()
  432. err = icpiIndexObj.AddData(dataItem)
  433. if err != nil {
  434. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  435. return err
  436. }
  437. } else {
  438. if dataVal != v.Today {
  439. err = icpiIndexObj.ModifyData(indexCode, today, v.Today)
  440. if err != nil {
  441. fmt.Println("icpiIndexObj ModifyData Err:" + err.Error())
  442. return err
  443. }
  444. }
  445. }
  446. yesterday := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  447. if dataVal, ok := dataMap[yesterday]; !ok {
  448. dataItem := new(models.BaseFromIcpiData)
  449. dataItem.IndexCode = indexCode
  450. dataItem.BaseFromIcpiIndexId = indexItem.BaseFromIcpiIndexId
  451. dataItem.DataTime = yesterday
  452. dataItem.Value = v.Yesterday
  453. dataItem.CreateTime = time.Now()
  454. dataItem.ModifyTime = time.Now()
  455. err = icpiIndexObj.AddData(dataItem)
  456. if err != nil {
  457. fmt.Println("icpiIndexObj AddData Err:" + err.Error())
  458. return err
  459. }
  460. } else {
  461. if dataVal != v.Yesterday {
  462. err = icpiIndexObj.ModifyData(indexCode, yesterday, v.Yesterday)
  463. if err != nil {
  464. fmt.Println("icpiIndexObj ModifyData Err:" + err.Error())
  465. return err
  466. }
  467. }
  468. }
  469. }
  470. maxItem, err := icpiIndexObj.GetBaseFromIcpiIndexMaxAndMinInfo(indexCode)
  471. if err != nil {
  472. fmt.Println("GetBaseFromIcpiIndexMaxAndMinInfo Err:" + err.Error())
  473. return err
  474. }
  475. err = icpiIndexObj.ModifyEdbInfoMaxAndMinInfo(indexCode, maxItem)
  476. if err != nil {
  477. fmt.Println("ModifyEdbInfoMaxAndMinInfo Err:" + err.Error())
  478. return err
  479. }
  480. }
  481. return err
  482. }
  483. type IcpiItem struct {
  484. Name string `json:"name"`
  485. EnglishName string `json:"englishName"`
  486. Today string `json:"today"`
  487. Yesterday string `json:"yesterday"`
  488. }