sci99.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package sci99
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "eta/eta_crawler/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/mozillazg/go-pinyin"
  14. )
  15. var IndexCodeMap = make(map[string]string)
  16. var PriceTypeIDs = []int{34320, 34318}
  17. var requestList = []ListRequestBody{
  18. {
  19. Ppname: "天然橡胶原料",
  20. PPIDs: "13676",
  21. Navid: "593",
  22. }, {
  23. Ppname: "混合胶",
  24. PPIDs: "12973",
  25. Navid: "591",
  26. }, {
  27. Ppname: "标准胶",
  28. PPIDs: "12960",
  29. Navid: "590",
  30. }, {
  31. Ppname: "烟片胶",
  32. PPIDs: "12985",
  33. Navid: "592",
  34. }, {
  35. Ppname: "天然乳胶",
  36. PPIDs: "12947",
  37. Navid: "595",
  38. }, {
  39. Ppname: "丁苯橡胶",
  40. PPIDs: "12951",
  41. Navid: "596",
  42. }, {
  43. Ppname: "顺丁橡胶",
  44. PPIDs: "12964",
  45. Navid: "597",
  46. }, {
  47. Ppname: "SBS",
  48. PPIDs: "12948",
  49. Navid: "598",
  50. }, {
  51. Ppname: "丁腈橡胶",
  52. PPIDs: "12945",
  53. Navid: "605",
  54. },
  55. }
  56. func Scii99(cont context.Context) (err error) {
  57. filePath := utils.SCI99_COOKIE_PATH
  58. // 打开文件
  59. file, err := ioutil.ReadFile(filePath)
  60. if err != nil {
  61. utils.FileLog.Info("ReadFile err:", err)
  62. return
  63. }
  64. cookie := string(file)
  65. // 定义请求的URL
  66. listUrl := "https://prices.sci99.com/api/zh-cn/product/datavalue"
  67. detailUrl := "https://prices.sci99.com/api/zh-cn/dataitem/datavalue"
  68. for _, request := range requestList {
  69. classifyName := request.Ppname
  70. // 分别获取国内价格和国际价格
  71. for _, priceTypeID := range PriceTypeIDs {
  72. // 创建请求体实例并填充数据,这里的参数都是一样的,所以这边统一写
  73. request.CycleType = "day"
  74. request.SiteType = 1
  75. request.PageNo = 1
  76. request.PageSize = "1000"
  77. request.PriceTypeID = priceTypeID
  78. // 将请求体序列化为JSON
  79. jsonData, e := json.Marshal(request)
  80. if e != nil {
  81. err = e
  82. fmt.Println("Error marshalling JSON:", err)
  83. utils.FileLog.Info("Error marshalling JSON:", err)
  84. return
  85. }
  86. // 创建一个HTTP POST请求
  87. req, e := http.NewRequest("POST", listUrl, bytes.NewBuffer(jsonData))
  88. if e != nil {
  89. err = e
  90. fmt.Println("Error creating request:", err)
  91. utils.FileLog.Info("Error creating request:", err)
  92. return
  93. }
  94. // 设置请求头信息
  95. req.Header.Set("Content-Type", "application/json")
  96. req.Header.Set("Accept", "application/json")
  97. req.Header.Set("Cookie", cookie)
  98. // 发送请求
  99. client := &http.Client{}
  100. resp, e := client.Do(req)
  101. if e != nil {
  102. err = e
  103. fmt.Println("Error sending request:", err)
  104. utils.FileLog.Info("Error sending request:", err)
  105. return
  106. }
  107. defer resp.Body.Close()
  108. // 检查响应状态码
  109. if resp.StatusCode != http.StatusOK {
  110. fmt.Printf("Server returned non-200 status: %d\n", resp.StatusCode)
  111. utils.FileLog.Info("Server returned non-200 status: %d", resp.StatusCode)
  112. return
  113. }
  114. // 打印响应体
  115. body, _ := ioutil.ReadAll(resp.Body)
  116. //fmt.Println("Response body:", string(body))
  117. var listResponse ListResponse
  118. err = json.Unmarshal(body, &listResponse)
  119. if err != nil {
  120. fmt.Println(err)
  121. utils.FileLog.Info("Error unmarshalling JSON:", err)
  122. return
  123. }
  124. fmt.Println("获取到的产品数量:", len(listResponse.FirstData.SecondData.Items))
  125. for _, v := range listResponse.FirstData.SecondData.Items {
  126. if v.FromArea != "" && len(listResponse.FirstData.SecondData.Items) > 1 {
  127. continue
  128. }
  129. // 组成指标
  130. var indexName string
  131. if v.FactorySampleName == "" || len(listResponse.FirstData.SecondData.Items) == 1 {
  132. indexName = v.ProductName + ":" + v.PriceType + ":" + v.MarketSampleName + ":" + v.Model
  133. } else {
  134. indexName = v.ProductName + ":" + v.PriceType + ":" + v.MarketSampleName + ":" + v.FactorySampleName + ":" + v.Model
  135. }
  136. ppid := strconv.Itoa(v.ProductID)
  137. dataTypeID := strconv.Itoa(v.DataTypeID)
  138. diidd := strconv.Itoa(v.DIID)
  139. deailReq := DetailRequest{
  140. PPID: ppid,
  141. DataTypeID: dataTypeID,
  142. DIIDD: diidd,
  143. APIStart: "2021/01/01",
  144. APIEnd: time.Now().AddDate(1, 0, 0).Format(utils.FormatDate2),
  145. CycleType: "day",
  146. }
  147. // 初始化是不加日期获取全部,初始化后只拿前三天的
  148. if utils.IS_INIT_SCI99 != "true" {
  149. deailReq.Start = time.Now().AddDate(0, 0, -5).Format(utils.FormatDate2)
  150. deailReq.End = time.Now().Format(utils.FormatDate2)
  151. }
  152. // 将请求体序列化为JSON
  153. reqJson, e := json.Marshal(deailReq)
  154. if e != nil {
  155. err = e
  156. fmt.Println("Error marshalling JSON:", err)
  157. utils.FileLog.Info("Error marshalling JSON:", err)
  158. return
  159. }
  160. // 创建一个HTTP POST请求
  161. req, err = http.NewRequest("POST", detailUrl, bytes.NewBuffer(reqJson))
  162. if err != nil {
  163. fmt.Println("Error creating request:", err)
  164. utils.FileLog.Info("Error creating request:", err)
  165. return
  166. }
  167. // 设置请求头信息
  168. req.Header.Set("Content-Type", "application/json")
  169. req.Header.Set("Accept", "application/json")
  170. req.Header.Set("Cookie", cookie)
  171. // 发送请求
  172. resp, err = client.Do(req)
  173. if err != nil {
  174. fmt.Println("Error sending request:", err)
  175. utils.FileLog.Info("Error sending request:", err)
  176. return
  177. }
  178. defer resp.Body.Close()
  179. // 检查响应状态码
  180. if resp.StatusCode != http.StatusOK {
  181. fmt.Printf("Server returned non-200 status: %d\n", resp.StatusCode)
  182. utils.FileLog.Info("Server returned non-200 status: %d", resp.StatusCode)
  183. return
  184. }
  185. // 打印响应体
  186. body, _ = ioutil.ReadAll(resp.Body)
  187. //fmt.Println("Response Detail body:", string(body))
  188. var detailResponse DetailResponse
  189. err = json.Unmarshal(body, &detailResponse)
  190. if err != nil {
  191. fmt.Println(err)
  192. utils.FileLog.Info("Error unmarshalling JSON:", err)
  193. return
  194. }
  195. fmt.Println("classifyName:", classifyName)
  196. fmt.Println("indexName:", indexName)
  197. fmt.Println("数据长度:", len(detailResponse.Data.List))
  198. param := make(map[string]interface{})
  199. param["ListData"] = v
  200. param["DetailData"] = detailResponse
  201. param["IndexName"] = indexName
  202. param["ClassifyName"] = classifyName
  203. urlStr := `sci99_crawler/refresh/list`
  204. postUrl := utils.EDB_LIB_URL + urlStr
  205. postData, e := json.Marshal(param)
  206. if e != nil {
  207. err = e
  208. utils.FileLog.Info("Marshal Err:" + err.Error())
  209. fmt.Println(err)
  210. return
  211. }
  212. result, e := HttpPost(postUrl, string(postData), "application/json")
  213. if e != nil {
  214. err = e
  215. fmt.Println(err)
  216. utils.FileLog.Info("HttpPost Err:" + err.Error())
  217. return
  218. }
  219. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  220. err = json.Unmarshal(result, &resp)
  221. if err != nil {
  222. fmt.Println(err)
  223. utils.FileLog.Info("Unmarshal resp Err:" + err.Error())
  224. return
  225. }
  226. time.Sleep(1 * time.Second)
  227. }
  228. }
  229. }
  230. return
  231. }
  232. func Sci99IndexCodeGenerator(indexName, indexCodeStr, marketSampleName, model string) (indexCode string, needAdd bool) {
  233. strResult := ""
  234. indexCode, _ = IndexCodeMap[indexName]
  235. if indexCode == "" {
  236. //首字母
  237. a := pinyin.NewArgs()
  238. a.Fallback = func(r rune, a pinyin.Args) []string {
  239. return []string{string(r)}
  240. }
  241. rows := pinyin.Pinyin(indexCodeStr, a)
  242. for i := 0; i < len(rows); i++ {
  243. //strResult += rows[i][0]
  244. if len(rows[i]) != 0 {
  245. str := rows[i][0]
  246. pi := str[0:1]
  247. strResult += pi
  248. }
  249. }
  250. // 处理市场名称
  251. if province, ok := ProvinceMap[marketSampleName]; ok {
  252. strResult += province
  253. } else {
  254. a := pinyin.NewArgs()
  255. rows := pinyin.LazyPinyin(marketSampleName, a)
  256. for i := 0; i < len(rows); i++ {
  257. strResult += rows[i]
  258. }
  259. }
  260. // 去除特殊符号
  261. model = strings.Replace(model, " ", "", -1)
  262. model = strings.Replace(model, "-", "", -1)
  263. model = strings.Replace(model, "/", "", -1)
  264. model = strings.Replace(model, "#", "", -1)
  265. model = strings.Replace(model, ":", "", -1)
  266. model = strings.Replace(model, "(", "", -1)
  267. model = strings.Replace(model, ")", "", -1)
  268. // 拼接型号
  269. modelRows := pinyin.Pinyin(model, a)
  270. for i := 0; i < len(modelRows); i++ {
  271. if len(modelRows[i]) != 0 {
  272. str := modelRows[i][0]
  273. pi := str[0:1]
  274. strResult += pi
  275. }
  276. }
  277. needAdd = true
  278. indexCode = strings.Replace(strResult, " ", "", -1)
  279. IndexCodeMap[indexName] = indexCode
  280. }
  281. return
  282. }
  283. var ProvinceMap = map[string]string{
  284. "上海": "shanghai",
  285. "云南": "yunnan",
  286. "内蒙古": "innermongolia",
  287. "北京": "beijing",
  288. "台湾": "taiwan",
  289. "吉林": "jilin",
  290. "四川": "sichuan",
  291. "天津": "tianjin",
  292. "宁夏": "ningxia",
  293. "安徽": "anhui",
  294. "山东": "shandong",
  295. "山西": "shanxi",
  296. "广东": "guangdong",
  297. "广西": "guangxi",
  298. "新疆": "xinjiang",
  299. "江苏": "jiangsu",
  300. "江西": "jiangxi",
  301. "河北": "hebei",
  302. "河南": "henan",
  303. "浙江": "zhejiang",
  304. "海南": "hainan",
  305. "湖北": "hubei",
  306. "湖南": "hunan",
  307. "澳门": "macao",
  308. "甘肃": "gansu",
  309. "福建": "fujian",
  310. "西藏": "tibet",
  311. "贵州": "guizhou",
  312. "辽宁": "liaoning",
  313. "重庆": "chongqing",
  314. "陕西": "shaanxi",
  315. "青海": "qinhai",
  316. "香港": "hongkong",
  317. "黑龙江": "heilongjiang",
  318. }
  319. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  320. body := ioutil.NopCloser(strings.NewReader(postData))
  321. client := &http.Client{}
  322. req, err := http.NewRequest("POST", url, body)
  323. if err != nil {
  324. return nil, err
  325. }
  326. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  327. if len(params) > 0 && params[0] != "" {
  328. contentType = params[0]
  329. }
  330. req.Header.Set("Content-Type", contentType)
  331. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  332. resp, err := client.Do(req)
  333. if err != nil {
  334. return nil, err
  335. }
  336. defer resp.Body.Close()
  337. b, err := ioutil.ReadAll(resp.Body)
  338. fmt.Println("HttpPost:" + string(b))
  339. return b, err
  340. }