common.go 10 KB

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