common.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package services
  2. import (
  3. "compress/gzip"
  4. "encoding/json"
  5. "eta/eta_data_analysis/utils"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "os"
  10. "strings"
  11. "time"
  12. )
  13. // savePageHtml 拉取历史报告详情
  14. func savePageHtml(nameKey, saveDir string, historyPage bool, reportMax int) (files []string, err error) {
  15. if nameKey == "" {
  16. return
  17. }
  18. defer func() {
  19. if err != nil {
  20. tips := fmt.Sprintf("GetOilchemEdbHistory ErrMsg: %s", err.Error())
  21. utils.FileLog.Info(tips)
  22. fmt.Println(tips)
  23. }
  24. }()
  25. //fetchRule, e := loadDataRule(nameKey)
  26. //if e != nil {
  27. // err = fmt.Errorf("loadDataRule, err: %v", e)
  28. // return
  29. //}
  30. if saveDir == "" {
  31. saveDir = "static/ccf"
  32. }
  33. // 拉取报告留档
  34. strDate := time.Now().Format("20060102")
  35. url := "https://www.oilchem.net/24-0801-09-4036018c523e4bbc.html"
  36. htm, e := FetchPageHtml(url)
  37. if e != nil {
  38. utils.FileLog.Info("获取页面失败, err: %v", e)
  39. }
  40. dateDir := fmt.Sprintf("%s/%s", saveDir, strDate)
  41. if e = utils.MkDir(dateDir); e != nil {
  42. utils.FileLog.Info(fmt.Sprintf("创建目录失败, err: %v", e))
  43. }
  44. outputPath := fmt.Sprintf("%s/%d-%s.html", dateDir, "", "")
  45. if e = utils.WriteHTMLToFile(string(htm), outputPath); e != nil {
  46. utils.FileLog.Info(fmt.Sprintf("写入HTML出错, err: %v", e))
  47. }
  48. files = append(files, outputPath)
  49. fmt.Println("拉取报告 end")
  50. return
  51. }
  52. // fetchPageHtml 获取网站HTML文本
  53. func FetchPageHtml(baseUrl string) (respBody []byte, err error) {
  54. defer func() {
  55. if err != nil {
  56. tips := fmt.Sprintf("BuildCCFRequest ErrMsg: %s", err.Error())
  57. utils.FileLog.Info(tips)
  58. fmt.Println(tips)
  59. }
  60. }()
  61. // 查询次数
  62. //fetchNum++
  63. if baseUrl == "" {
  64. err = fmt.Errorf("隆众请求地址为空")
  65. return
  66. }
  67. // 获取Cookie
  68. strCookie, e := getCookie()
  69. if e != nil {
  70. err = fmt.Errorf("读取cookie文件失败, err: %s", e.Error())
  71. return
  72. }
  73. // 拉取网站内容
  74. cli := new(http.Client)
  75. req, e := http.NewRequest("GET", baseUrl, nil)
  76. if e != nil {
  77. err = fmt.Errorf("")
  78. return
  79. }
  80. req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
  81. req.Header.Set("Accept-Encoding", "gzip, deflate, br")
  82. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
  83. req.Header.Set("Connection", "keep-alive")
  84. req.Header.Set("Cookie", strCookie)
  85. req.Header.Set("Host", "www.oilchem.net")
  86. req.Header.Set("Referer", "https://chem.oilchem.net/")
  87. req.Header.Set("Sec-Ch-Ua", "\"Not A(Brand\";v=\"99\", \"Microsoft Edge\";v=\"121\", \"Chromium\";v=\"121\"")
  88. req.Header.Set("Sec-Ch-Ua-Mobile", "?0")
  89. req.Header.Set("Sec-Ch-Ua-Platform", "\"Windows\"")
  90. req.Header.Set("Sec-Fetch-Dest", "empty")
  91. req.Header.Set("Sec-Fetch-Mode", "cors")
  92. req.Header.Set("Sec-Fetch-Site", "same-origin")
  93. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0")
  94. req.Header.Set("X-Requested-With", "XMLHttpRequest")
  95. resp, e := cli.Do(req)
  96. if e != nil {
  97. err = fmt.Errorf("HTTP client Do err: %s", e.Error())
  98. return
  99. }
  100. defer func() {
  101. _ = resp.Body.Close()
  102. }()
  103. // 读取响应的内容
  104. reader, e := gzip.NewReader(resp.Body)
  105. if e != nil {
  106. err = fmt.Errorf("gzip NewReader err: %s", e.Error())
  107. return
  108. }
  109. respBody, e = io.ReadAll(reader)
  110. if e != nil {
  111. err = fmt.Errorf("read body err: %s", e.Error())
  112. return
  113. }
  114. return
  115. }
  116. // getCookie
  117. // @Description: 获取cookie
  118. // @author: Roc
  119. // @datetime 2024-07-09 14:00:53
  120. // @return cookieStr string
  121. // @return err error
  122. func getCookie() (cookieStr string, err error) {
  123. // 读取Cookie
  124. if utils.OilchemCookieFile == "" {
  125. err = fmt.Errorf("cookie文件未配置")
  126. return
  127. }
  128. cookieByte, e := os.ReadFile(utils.OilchemCookieFile)
  129. if e != nil {
  130. err = fmt.Errorf("读取cookie文件失败, err: %s", e.Error())
  131. return
  132. }
  133. cookieStr = strings.TrimSpace(string(cookieByte))
  134. if cookieStr == "" {
  135. err = fmt.Errorf("cookie为空")
  136. return
  137. }
  138. return
  139. }
  140. // postEdbLib 调用指标接口
  141. func postEdbLib(param map[string]interface{}, method string) (result []byte, err error) {
  142. postUrl := utils.EDB_LIB_URL + method
  143. postData, err := json.Marshal(param)
  144. if err != nil {
  145. return
  146. }
  147. result, err = httpPost(postUrl, string(postData), "application/json")
  148. if err != nil {
  149. return
  150. }
  151. return
  152. }
  153. // httpPost HTTP请求
  154. func httpPost(url, postData string, params ...string) ([]byte, error) {
  155. fmt.Println("httpPost Url:" + url)
  156. body := io.NopCloser(strings.NewReader(postData))
  157. client := &http.Client{}
  158. req, err := http.NewRequest("POST", url, body)
  159. if err != nil {
  160. return nil, err
  161. }
  162. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  163. if len(params) > 0 && params[0] != "" {
  164. contentType = params[0]
  165. }
  166. req.Header.Set("Content-Type", contentType)
  167. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  168. resp, err := client.Do(req)
  169. if err != nil {
  170. fmt.Println("client.Do err:" + err.Error())
  171. return nil, err
  172. }
  173. defer func() {
  174. _ = resp.Body.Close()
  175. }()
  176. b, err := io.ReadAll(resp.Body)
  177. if err != nil {
  178. fmt.Println("httpPost:" + string(b))
  179. }
  180. return b, err
  181. }