common.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package national_data
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "fmt"
  6. "hongze/hongze_data_crawler/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. NationalStatisticsBaseReqUrl = "https://data.stats.gov.cn/easyquery.htm"
  15. )
  16. func NationalHttpPost(reqUrl, payload string) (result []byte, err error) {
  17. // 随机延迟执行
  18. r := utils.RangeRand(3000, 6000)
  19. if r > 5000 && r < 5100 {
  20. time.Sleep(10 * time.Second)
  21. } else {
  22. time.Sleep(time.Duration(r) * time.Millisecond)
  23. }
  24. tr := &http.Transport{
  25. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  26. }
  27. client := &http.Client{
  28. Transport: tr,
  29. }
  30. req, err := http.NewRequest("POST", reqUrl, strings.NewReader(payload))
  31. if err != nil {
  32. return
  33. }
  34. req.Header.Add("Accept", "text/plain, */*; q=0.01")
  35. req.Header.Add("Accept-Encoding", "tgzip, deflate, br")
  36. req.Header.Add("Accept-Language", "zh-CN,zh;q=0.9")
  37. req.Header.Add("Connection", "keep-alive")
  38. req.Header.Add("Content-Length", "37")
  39. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  40. req.Header.Add("Cookie", "wzws_sessionid=gDExNS4xOTQuMTAyLjEyN6BkERzUgmZjNWVlMYFiOWNiZDg=; JSESSIONID=UOri2Cu3f3c-Y3rPgXWJ04E8pfbeyAUGG-s7zJ7Tt0JhlEiLi0EU!412929168; u=5")
  41. req.Header.Add("Host", "data.stats.gov.cn")
  42. req.Header.Add("Origin", "https://data.stats.gov.cn")
  43. req.Header.Set("Referer", "https://data.stats.gov.cn/easyquery.htm?cn=A01")
  44. req.Header.Set("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"")
  45. req.Header.Set("sec-ch-ua-mobile", "?0")
  46. req.Header.Set("sec-ch-ua-platform", "\"Windows\"")
  47. req.Header.Set("Sec-Fetch-Dest", "empty")
  48. req.Header.Set("Sec-Fetch-Mode", "cors")
  49. req.Header.Set("Sec-Fetch-Site", "same-origin")
  50. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
  51. req.Header.Set("X-Requested-With", "XMLHttpRequest")
  52. res, err := client.Do(req)
  53. if err != nil {
  54. return
  55. }
  56. defer res.Body.Close()
  57. body, err := ioutil.ReadAll(res.Body)
  58. if err != nil {
  59. return
  60. }
  61. result = body
  62. return
  63. }
  64. func NationalGet(reqUrl, payload string) (err error) {
  65. tr := &http.Transport{
  66. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  67. }
  68. client := &http.Client{
  69. Transport: tr,
  70. }
  71. req, err := http.NewRequest("GET", reqUrl, strings.NewReader(payload))
  72. if err != nil {
  73. return
  74. }
  75. res, err := client.Do(req)
  76. if err != nil {
  77. return
  78. }
  79. defer res.Body.Close()
  80. _, err = ioutil.ReadAll(res.Body)
  81. if err != nil {
  82. return
  83. }
  84. Cookie := res.Header.Get("Cookie")
  85. fmt.Println(Cookie)
  86. rcookie := req.Header.Get("Cookie")
  87. fmt.Println("rcookie")
  88. fmt.Println(rcookie)
  89. //fmt.Println("body:" + string(body))
  90. cookiesArr := res.Cookies()
  91. fmt.Println("cookiesArrLen:", len(cookiesArr))
  92. for k, v := range cookiesArr {
  93. fmt.Println(k, v)
  94. }
  95. return
  96. }
  97. // DataApiReq 数据接口请求体
  98. type DataApiReq struct {
  99. Method string `description:"方法: QueryData-查询数据; getOtherWds-获取其他维度" json:"method"`
  100. DbCode string `description:"数据库编码" json:"dbcode"`
  101. RowCode string `description:"行-维度: zb; sj; reg" json:"rowcode"`
  102. ColCode string `description:"列-维度: zb; sj; reg" json:"colcode"`
  103. WdsList []Wds `description:"维度列表" json:"wdsList"`
  104. DfwdsList []Wds `description:"df不知道啥意思...反正也是维度相关的" json:"dfwdsList"`
  105. }
  106. // Wds 维度
  107. type Wds struct {
  108. WdCode string `description:"维度: zb-指标; sj-时间; reg-地区" json:"wdcode"`
  109. ValueCode string `description:"维度编码" json:"valuecode"`
  110. }
  111. // CommonDataApiRequest 数据接口请求
  112. func CommonDataApiRequest(req DataApiReq) (resp QuotaListDataResp, err error) {
  113. if req.DbCode == "" {
  114. return
  115. }
  116. if req.Method == "" {
  117. req.Method = "QueryData"
  118. }
  119. if req.RowCode == "" {
  120. req.RowCode = "zb"
  121. }
  122. if req.ColCode == "" {
  123. req.ColCode = "sj"
  124. }
  125. // 构建查询
  126. f := url.Values{}
  127. f.Add("m", req.Method)
  128. f.Add("dbcode", req.DbCode)
  129. f.Add("rowcode", req.RowCode)
  130. f.Add("colcode", req.ColCode)
  131. wds := `[]`
  132. if len(req.WdsList) > 0 {
  133. wdsByte, e := json.Marshal(req.WdsList)
  134. if e != nil {
  135. err = fmt.Errorf("wds json marshal err: %s", e.Error())
  136. return
  137. }
  138. wds = string(wdsByte)
  139. }
  140. dfwds := `[]`
  141. if len(req.DfwdsList) > 0 {
  142. dfwdsByte, e := json.Marshal(req.DfwdsList)
  143. if e != nil {
  144. err = fmt.Errorf("dfwds json marshal err: %s", e.Error())
  145. return
  146. }
  147. dfwds = string(dfwdsByte)
  148. }
  149. f.Add("wds", wds)
  150. f.Add("dfwds", dfwds)
  151. f.Add("k1", fmt.Sprint(time.Now().UnixNano()/1e6))
  152. f.Add("h", "1")
  153. // 响应
  154. r, e := NationalHttpPost(NationalStatisticsBaseReqUrl, f.Encode())
  155. if e != nil {
  156. err = fmt.Errorf("http request err: %s", e.Error())
  157. return
  158. }
  159. //utils.FileLog.Info("result: %s", string(r))
  160. if e = json.Unmarshal(r, &resp); e != nil {
  161. err = fmt.Errorf("resp unmarshal err: %s", e.Error())
  162. return
  163. }
  164. if resp.ReturnCode != 200 {
  165. err = fmt.Errorf("resp code err: %d", resp.ReturnCode)
  166. return
  167. }
  168. return
  169. }
  170. // QuotaListDataResp 指标数据列表响应体
  171. type QuotaListDataResp struct {
  172. ReturnCode int `description:"状态码" json:"returncode"`
  173. ReturnData struct {
  174. DataNodes []QuotaDataNode `json:"datanodes"`
  175. WdNodes []QuotaWdNode `json:"wdnodes"`
  176. }
  177. }
  178. // QuotaDataNode 指标数据节点
  179. type QuotaDataNode struct {
  180. Code string `description:"编码"`
  181. Data struct {
  182. Data float64 `description:"指标值"`
  183. HasData bool `description:"是否有值" json:"hasdata"`
  184. StrData string `description:"指标值(字符串)" json:"strdata"`
  185. }
  186. Wds []Wds
  187. }
  188. // QuotaWdNode 维度节点
  189. type QuotaWdNode struct {
  190. WdCode string `description:"示例: zb; sj; reg;" json:"wdcode"`
  191. WdName string `description:"示例: 指标; 时间; 地区" json:"wdname"`
  192. Nodes []QuotaWdNodeData
  193. }
  194. // QuotaWdNodeData 维度节点数据
  195. type QuotaWdNodeData struct {
  196. Code string `description:"指标编码"`
  197. Name string `description:"指标名称"`
  198. Unit string `description:"单位"`
  199. SortCode int `description:"编码排序" json:"sortcode"`
  200. }
  201. // OtherWdResp 其他维度信息响应体
  202. type OtherWdResp struct {
  203. ReturnCode int `description:"状态码" json:"returncode"`
  204. ReturnData []OtherWdData `description:"响应数据" json:"returndata"`
  205. }
  206. // OtherWdData 其他维度数据
  207. type OtherWdData struct {
  208. IsSj bool `description:"是否为时间" json:"issj"`
  209. WdCode string `description:"维度编码" json:"wdcode"`
  210. WdName string `description:"维度名称" json:"wdname"`
  211. Nodes []OtherWdNodes `description:"维度数据" json:"nodes"`
  212. }
  213. type OtherWdNodes struct {
  214. Code string `description:"编码" json:"code"`
  215. Name string `description:"名称" json:"name"`
  216. Sort string `description:"排序" json:"sort"`
  217. }
  218. // formatMonth2YearDateCode 将日期code转为对应日期
  219. func formatMonth2YearDateCode(dateCode string) (date time.Time, err error) {
  220. if dateCode == "" {
  221. return
  222. }
  223. // 根据日期code长度进行区分, 格式为三种: 月度-200601; 季度-2006A; 年度-2006
  224. switch len([]rune(dateCode)) {
  225. case 6:
  226. t, e := time.ParseInLocation("200601", dateCode, time.Local)
  227. if e != nil {
  228. err = fmt.Errorf("月度指标日期转换失败, Err: %s", e.Error())
  229. return
  230. }
  231. date = t
  232. break
  233. case 5:
  234. // 季度ABCD转换成对应日期
  235. dateSuffixMap := map[string]string{
  236. "A": "03-31",
  237. "B": "06-30",
  238. "C": "09-30",
  239. "D": "12-31",
  240. }
  241. dateCode = strings.ToUpper(dateCode)
  242. quarterTab := dateCode[4:]
  243. dateStr := fmt.Sprintf("%s-%s", dateCode[:4], dateSuffixMap[quarterTab])
  244. t, e := time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  245. if e != nil {
  246. err = fmt.Errorf("季度指标日期转换失败, Err: %s", e.Error())
  247. return
  248. }
  249. date = t
  250. break
  251. case 4:
  252. dateStr := fmt.Sprintf("%s-%s", dateCode, "12-31")
  253. t, e := time.ParseInLocation(utils.FormatDate, dateStr, time.Local)
  254. if e != nil {
  255. err = fmt.Errorf("年度指标日期转换失败, Err: %s", e.Error())
  256. return
  257. }
  258. date = t
  259. break
  260. default:
  261. err = fmt.Errorf("日期code格式有误, code: %s", dateCode)
  262. return
  263. }
  264. return
  265. }
  266. // GetOtherWd 获取Db下其他维度信息
  267. func GetOtherWd(dbCode, rowCode, colCode string) (wdList []OtherWdData, err error) {
  268. if dbCode == "" {
  269. return
  270. }
  271. if rowCode == "" {
  272. rowCode = "zb"
  273. }
  274. if colCode == "" {
  275. colCode = "sj"
  276. }
  277. // 构建查询
  278. f := url.Values{}
  279. f.Add("m", "getOtherWds")
  280. f.Add("dbcode", dbCode)
  281. f.Add("rowcode", rowCode)
  282. f.Add("colcode", colCode)
  283. f.Add("wds", `[]`)
  284. f.Add("k1", fmt.Sprint(time.Now().UnixNano()/1e6))
  285. f.Add("h", "1")
  286. r, e := NationalHttpPost(NationalStatisticsBaseReqUrl, f.Encode())
  287. if e != nil {
  288. err = fmt.Errorf("请求其他维度信息失败, Err: %s", e.Error())
  289. return
  290. }
  291. utils.FileLog.Info("GetOtherWdInfo Result: %s", string(r))
  292. // 响应
  293. resp := new(OtherWdResp)
  294. if e = json.Unmarshal(r, &resp); e != nil {
  295. err = fmt.Errorf("其他维度信息Unmarshal Err: %s", e.Error())
  296. return
  297. }
  298. if resp == nil {
  299. err = fmt.Errorf("其他维度信息请求结果为空")
  300. return
  301. }
  302. if resp.ReturnCode != 200 {
  303. err = fmt.Errorf("其他维度信息请求有误, Code: %d", resp.ReturnCode)
  304. return
  305. }
  306. wdList = resp.ReturnData
  307. return
  308. }
  309. func ApiTest() (err error) {
  310. //f := url.Values{}
  311. //f.Add("m", "QueryData")
  312. //f.Add("dbcode", "fsyd")
  313. //f.Add("rowcode", "zb")
  314. //f.Add("colcode", "sj")
  315. //f.Add("wds", `[{"wdcode":"reg","valuecode":"000"}]`)
  316. //f.Add("dfwds", `[{"wdcode":"zb","valuecode":"A01"}]`)
  317. f := url.Values{}
  318. f.Add("m", "QueryData")
  319. f.Add("dbcode", "fsyd")
  320. f.Add("rowcode", "zb")
  321. f.Add("colcode", "sj")
  322. //f.Add("wds", `[{"wdcode":"reg","valuecode":"110000"}]`)
  323. f.Add("wds", `[]`)
  324. f.Add("dfwds", `[{"wdcode":"zb","valuecode":"A010101"}]`)
  325. f.Add("k1", fmt.Sprint(time.Now().UnixNano()/1e6))
  326. f.Add("h", "1")
  327. //f := url.Values{}
  328. //f.Add("m", "QueryData")
  329. //f.Add("dbcode", "gatyd")
  330. //f.Add("rowcode", "sj")
  331. //f.Add("colcode", "reg")
  332. //f.Add("wds", `[{"wdcode":"zb","valuecode":"A010A"}]`)
  333. //f.Add("dfwds", `[{"wdcode":"sj","valuecode":"LAST36"}]`)
  334. //f := url.Values{}
  335. //f.Add("m", "QueryData")
  336. //f.Add("dbcode", "fsyd")
  337. //f.Add("rowcode", "zb")
  338. //f.Add("colcode", "sj")
  339. //f.Add("wds", `[{"wdcode":"reg","valuecode":"000"}]`)
  340. //f.Add("dfwds", `[{"wdcode":"zb","valuecode":"A01"}]`)
  341. //f.Add("k1", fmt.Sprint(time.Now().UnixNano()/1e6))
  342. //f.Add("h", "1")
  343. r, e := NationalHttpPost(NationalStatisticsBaseReqUrl, f.Encode())
  344. if e != nil {
  345. fmt.Println("请求失败, Err: ", e.Error())
  346. return
  347. }
  348. utils.FileLog.Info("result: %s", string(r))
  349. return
  350. }