main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "qfw/util"
  13. "qfw/util/mysql"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. var (
  19. config map[string]string
  20. appid, key, apiurl, day string
  21. httpClient *http.Client
  22. MaxIdleCons int = 100
  23. MaxIdleConsPerHost int = 100
  24. IdleConnTimeout int = 2048
  25. ConnectTimeOut int = 30
  26. KeepAlive int = 30
  27. Tidb *mysql.Mysql
  28. )
  29. func init() {
  30. httpClient = createHttpClient()
  31. Tidb = &mysql.Mysql{
  32. Address: "192.168.3.217:4000",
  33. UserName: "root",
  34. PassWord: "=PDT49#80Z!RVv52_z",
  35. DBName: "createxzh",
  36. MaxOpenConns: 20,
  37. MaxIdleConns: 40,
  38. }
  39. Tidb.Init()
  40. }
  41. func main() {
  42. util.ReadConfig(&config)
  43. appid = config["appid"]
  44. key = config["key"]
  45. apiurl = config["apiurl"]
  46. day = config["day"]
  47. getData()
  48. }
  49. func createHttpClient() *http.Client {
  50. client := &http.Client{
  51. Transport: &http.Transport{
  52. Proxy: http.ProxyFromEnvironment,
  53. DialContext: (&net.Dialer{
  54. Timeout: time.Duration(ConnectTimeOut) * time.Second, //TCP连接超时30s
  55. KeepAlive: time.Duration(KeepAlive) * time.Second, //TCP keepalive保活检测定时30s
  56. }).DialContext,
  57. MaxIdleConns: MaxIdleCons,
  58. MaxIdleConnsPerHost: MaxIdleConsPerHost,
  59. IdleConnTimeout: time.Duration(IdleConnTimeout) * time.Second, //闲置连接超时2048s
  60. ResponseHeaderTimeout: time.Second * 60,
  61. },
  62. }
  63. return client
  64. }
  65. func getToken() (token string) {
  66. tm := fmt.Sprintf("%d", time.Now().Unix())
  67. res := post(apiurl+"/user/access_token", map[string]string{
  68. "appid": appid,
  69. "timestamp": tm,
  70. "signature": MD5(appid + tm + key),
  71. //"key": "6PzV0CUa",
  72. }, nil)
  73. log.Println(tm, MD5(appid+tm+key), res)
  74. if res != nil && res["access_token"] != "" {
  75. token, _ = res["access_token"].(string)
  76. }
  77. return
  78. }
  79. func getData() {
  80. // token := getToken()
  81. log.Println("开始任务")
  82. next := "0"
  83. for {
  84. tm := fmt.Sprint(time.Now().Unix())
  85. data := post(apiurl+"/data/getalldata", map[string]string{
  86. // "access_token": token,
  87. "day": day,
  88. "next": next,
  89. "appid": appid,
  90. }, map[string]string{
  91. "timestamp": tm,
  92. "signature": MD5(appid + tm + key),
  93. })
  94. if data != nil {
  95. log.Println(data["next"])
  96. datas := util.ObjArrToMapArr(data["data"].([]interface{}))
  97. for _, v := range datas {
  98. filehref, _ := json.Marshal(v["filehref"])
  99. v["filehref"] = string(filehref)
  100. purchasinglist, _ := json.Marshal(v["purchasinglist"])
  101. v["purchasinglist"] = string(purchasinglist)
  102. id := util.ObjToString(v["id"])
  103. if Tidb.Count("info", map[string]interface{}{"id": id}) == 0 {
  104. did := Tidb.Insert("info", v)
  105. if did > 0 {
  106. log.Println("保存成功 ", id)
  107. }
  108. } else {
  109. log.Println("库中已有 ", id)
  110. }
  111. }
  112. nexts := util.IntAll(data["next"])
  113. if nexts == 0 {
  114. break
  115. } else {
  116. tm = fmt.Sprint(time.Now().Unix())
  117. next = strconv.Itoa(nexts)
  118. log.Println("第", next, "条")
  119. }
  120. } else {
  121. break
  122. }
  123. }
  124. }
  125. func MD5(str string) string {
  126. h := md5.New()
  127. h.Write([]byte(str))
  128. return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  129. }
  130. func post(urls string, form, header map[string]string) (data map[string]interface{}) {
  131. formValues := make(url.Values)
  132. for k, v := range form {
  133. formValues[k] = []string{v}
  134. }
  135. log.Println(formValues)
  136. header["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
  137. request, err := http.NewRequest("POST", urls, strings.NewReader(formValues.Encode()))
  138. if err != nil {
  139. return
  140. }
  141. for k, v := range header {
  142. request.Header.Add(k, v)
  143. }
  144. response, err := httpClient.Do(request) //前面预处理一些参数,状态,Do执行发送;处理返回结果;Do:发送请求,
  145. if err != nil {
  146. return
  147. }
  148. defer response.Body.Close()
  149. replay, err := ioutil.ReadAll(response.Body)
  150. if err != nil {
  151. log.Println("read reply error:", err)
  152. return
  153. }
  154. // 创建一个空的map来存放解析后的数据
  155. // 使用json.Unmarshal来解析[]byte到map
  156. json.Unmarshal(replay, &data)
  157. return
  158. }