baidu_account.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package levelMonitor
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/go-redis/redis/v8"
  7. "io/ioutil"
  8. "log-server/global"
  9. "net/http"
  10. "strings"
  11. "time"
  12. )
  13. //Ids = []string{
  14. //"1LVM2db0Ehr931W7gRUimZlF",
  15. //"wtipNMdKoeniksPtKGcyqt3N",
  16. //"l16wv0XqQW8VyFaFkhb7iIdG",
  17. //"OGLYftxvRsut3Mj6DszpZGOH",
  18. //"RRfRVDugxEtKGZGbWXQj7joF",
  19. //"0vyrRQzhNzmGm4oS5LhPQWAd",
  20. //"6tQwK5zGcoDoZReyTjEi6LhO",
  21. //"8QnWdj3Bbtkls9uT372oEx8A",
  22. //"zv67LjUDA3mOB6XeuxokhN3O",
  23. //"k3r4fesYiGCUehA2Zk55TRv3",
  24. //}
  25. //Secrets = []string{
  26. //"ZZW2xfzC2OAlkInI6YbWGECledb7GLoy",
  27. //"FSdfDdsGEeG0FUqmDgfwWzf64bd1ee3c",
  28. //"t8fIX8BYGlzZk3xOeGGuUaGo6kXI9qN6",
  29. //"2z2UIuKDANFyQSL1V0jSNlH1XkmoGEPQ",
  30. //"X41FM9VyxOn9Phx5xevzq8LyOHlM0DzL",
  31. //"X3zjn3WiAoY0qGARY02rAL4wioLCeMeB",
  32. //"qIc39tGvYEtEVGW2R9X0KxnaKR8nYw2K",
  33. //"b50C0BYG7GyhBW5m2zww6oGk9ZAyOBGo",
  34. //"vtDazOQPLuWlOqkdZG80w8doqwFM82C7",
  35. //"xggNtYdTQI1Qb4CDDUwDbREaosP8C1sI",
  36. //}
  37. var account1 = map[string]string{
  38. "API_KEY": "1LVM2db0Ehr931W7gRUimZlF",
  39. "SECRET_KEY": "ZZW2xfzC2OAlkInI6YbWGECledb7GLoy",
  40. }
  41. var account2 = map[string]string{
  42. "API_KEY": "wtipNMdKoeniksPtKGcyqt3N",
  43. "SECRET_KEY": "FSdfDdsGEeG0FUqmDgfwWzf64bd1ee3c",
  44. }
  45. var account3 = map[string]string{
  46. "API_KEY": "l16wv0XqQW8VyFaFkhb7iIdG",
  47. "SECRET_KEY": "t8fIX8BYGlzZk3xOeGGuUaGo6kXI9qN6",
  48. }
  49. var account4 = map[string]string{
  50. "API_KEY": "OGLYftxvRsut3Mj6DszpZGOH",
  51. "SECRET_KEY": "2z2UIuKDANFyQSL1V0jSNlH1XkmoGEPQ",
  52. }
  53. var account5 = map[string]string{
  54. "API_KEY": "RRfRVDugxEtKGZGbWXQj7joF",
  55. "SECRET_KEY": "X41FM9VyxOn9Phx5xevzq8LyOHlM0DzL",
  56. }
  57. var account6 = map[string]string{
  58. "API_KEY": "0vyrRQzhNzmGm4oS5LhPQWAd",
  59. "SECRET_KEY": "X3zjn3WiAoY0qGARY02rAL4wioLCeMeB",
  60. }
  61. var account7 = map[string]string{
  62. "API_KEY": "6tQwK5zGcoDoZReyTjEi6LhO",
  63. "SECRET_KEY": "qIc39tGvYEtEVGW2R9X0KxnaKR8nYw2K",
  64. }
  65. var account8 = map[string]string{
  66. "API_KEY": "8QnWdj3Bbtkls9uT372oEx8A",
  67. "SECRET_KEY": "b50C0BYG7GyhBW5m2zww6oGk9ZAyOBGo",
  68. }
  69. var account9 = map[string]string{
  70. "API_KEY": "zv67LjUDA3mOB6XeuxokhN3O",
  71. "SECRET_KEY": "vtDazOQPLuWlOqkdZG80w8doqwFM82C7",
  72. }
  73. var account10 = map[string]string{
  74. "API_KEY": "k3r4fesYiGCUehA2Zk55TRv3",
  75. "SECRET_KEY": "xggNtYdTQI1Qb4CDDUwDbREaosP8C1sI",
  76. }
  77. var Account = map[int]map[string]string{
  78. 1: account1,
  79. 2: account2,
  80. 3: account3,
  81. 4: account4,
  82. 5: account5,
  83. 6: account6,
  84. 7: account7,
  85. 8: account8,
  86. 9: account9,
  87. 10: account10,
  88. }
  89. const BaiduIdentify = "baiduIdentifyTokenV2:%d"
  90. const AccountLimit = 350
  91. const BaiduIdentifyLimit = "%s:baiduIdentifyTokenV2:%d"
  92. const BaiduCurrentAccount = "%s:baiduCurrentAccount"
  93. const BaiduCurrentAccountLimit = "%s:baiduCurrentAccountLimit"
  94. type BaiduAccount struct {
  95. CurrentDate string
  96. BaiduCurrentAccountKey string
  97. BaiduCurrentAccountLimitKdy string
  98. }
  99. func (m *BaiduAccount) GetCurrentAccountInfo(ctx context.Context) (currentAccountId int, err error) {
  100. currentAccountKey := fmt.Sprintf(BaiduCurrentAccount, m.CurrentDate)
  101. currentAccountLimitKey := fmt.Sprintf(BaiduCurrentAccountLimit, m.CurrentDate)
  102. m.BaiduCurrentAccountKey = currentAccountKey
  103. m.BaiduCurrentAccountLimitKdy = currentAccountLimitKey
  104. currentAccountId, err = global.GVA_REDIS.Get(ctx, currentAccountKey).Int()
  105. if err != nil {
  106. if err == redis.Nil {
  107. global.GVA_REDIS.Set(ctx, currentAccountKey, 1, time.Hour*24)
  108. global.GVA_REDIS.Set(ctx, currentAccountLimitKey, 1, time.Hour*24)
  109. return 1, nil
  110. }
  111. }
  112. return
  113. }
  114. func (m *BaiduAccount) CheckCurrentAccountLimit(ctx context.Context, id int) (currentAccountId int, err error) {
  115. limit, err := global.GVA_REDIS.Get(ctx, m.BaiduCurrentAccountLimitKdy).Int()
  116. if err != nil {
  117. return
  118. }
  119. if limit >= AccountLimit {
  120. if id < 10 {
  121. id += 1
  122. global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountKey, id, time.Hour*24)
  123. global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountLimitKdy, 1, time.Hour*24)
  124. } else {
  125. id = 1
  126. global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountKey, id, time.Hour*24)
  127. global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountLimitKdy, 1, time.Hour*24)
  128. }
  129. }
  130. currentAccountId = id
  131. return
  132. }
  133. func (m *BaiduAccount) GetAccessToken(ctx context.Context, id int) (string, error) {
  134. key := fmt.Sprintf(BaiduIdentify, id)
  135. token, err := global.GVA_REDIS.Get(ctx, key).Result()
  136. if err != nil || token == "" {
  137. reqUrl := "https://aip.baidubce.com/oauth/2.0/token"
  138. postData := fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s", Account[id]["API_KEY"], Account[id]["SECRET_KEY"])
  139. resp, err := http.Post(reqUrl, "application/x-www-form-urlencoded", strings.NewReader(postData))
  140. if err != nil {
  141. fmt.Println(err)
  142. return "", err
  143. }
  144. defer resp.Body.Close()
  145. body, err := ioutil.ReadAll(resp.Body)
  146. if err != nil {
  147. fmt.Println(err)
  148. return "", err
  149. }
  150. accessTokenObj := map[string]string{}
  151. json.Unmarshal(body, &accessTokenObj)
  152. global.GVA_REDIS.Set(ctx, key, accessTokenObj["access_token"], time.Hour*24*29)
  153. return accessTokenObj["access_token"], err
  154. }
  155. global.GVA_REDIS.Incr(ctx, m.BaiduCurrentAccountLimitKdy)
  156. return token, err
  157. }