main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "net/http"
  10. "qfw/util"
  11. "strings"
  12. "time"
  13. )
  14. var (
  15. config map[string]string
  16. // appid = "jyNjdXQgUDAwdaTklMPz5i"
  17. // key = "404M0v2j"
  18. // apiurl = "http://127.0.0.1:8801"
  19. // appid = "jyMi1XQgMABQNcSkBMIhBq"
  20. // key = "6PzV0CUa"
  21. // apiurl = "https://testapi.jianyu360.com"
  22. appid, key, apiurl, day string
  23. )
  24. func main() {
  25. util.ReadConfig(&config)
  26. appid = config["appid"]
  27. key = config["key"]
  28. apiurl = config["apiurl"]
  29. day = config["day"]
  30. getData()
  31. }
  32. func getToken() (token string) {
  33. tm := fmt.Sprintf("%d", time.Now().Unix())
  34. res := post(apiurl+"/user/access_token", map[string]string{
  35. "appid": appid,
  36. "timestamp": tm,
  37. "signature": MD5(appid + tm + key),
  38. //"key": "6PzV0CUa",
  39. })
  40. log.Println(tm, MD5(appid+tm+key), res)
  41. if res != nil && res["access_token"] != "" {
  42. token, _ = res["access_token"].(string)
  43. }
  44. return
  45. }
  46. func getData() {
  47. token := getToken()
  48. data := post(apiurl+"/data/getalldata", map[string]string{
  49. "access_token": token,
  50. "day": day,
  51. "next": "92",
  52. })
  53. //s, _ := json.Marshal(data["data"])
  54. //delete(data, "data")
  55. log.Println(token, data)
  56. }
  57. func MD5(str string) string {
  58. h := md5.New()
  59. h.Write([]byte(str))
  60. return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  61. }
  62. func post(url string, form map[string]string) (data map[string]interface{}) {
  63. str := ""
  64. for k, v := range form {
  65. str += "&" + k + "=" + v
  66. }
  67. log.Println(str)
  68. res, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str))
  69. if err != nil {
  70. log.Println("post err:", err.Error())
  71. } else if res.Body != nil {
  72. defer res.Body.Close()
  73. bs, _ := ioutil.ReadAll(res.Body)
  74. json.Unmarshal(bs, &data)
  75. }
  76. return
  77. }