package levelMonitor import ( "context" "encoding/json" "fmt" "github.com/go-redis/redis/v8" "io/ioutil" "log-server/global" "net/http" "strings" "time" ) //Ids = []string{ //"1LVM2db0Ehr931W7gRUimZlF", //"wtipNMdKoeniksPtKGcyqt3N", //"l16wv0XqQW8VyFaFkhb7iIdG", //"OGLYftxvRsut3Mj6DszpZGOH", //"RRfRVDugxEtKGZGbWXQj7joF", //"0vyrRQzhNzmGm4oS5LhPQWAd", //"6tQwK5zGcoDoZReyTjEi6LhO", //"8QnWdj3Bbtkls9uT372oEx8A", //"zv67LjUDA3mOB6XeuxokhN3O", //"k3r4fesYiGCUehA2Zk55TRv3", //} //Secrets = []string{ //"ZZW2xfzC2OAlkInI6YbWGECledb7GLoy", //"FSdfDdsGEeG0FUqmDgfwWzf64bd1ee3c", //"t8fIX8BYGlzZk3xOeGGuUaGo6kXI9qN6", //"2z2UIuKDANFyQSL1V0jSNlH1XkmoGEPQ", //"X41FM9VyxOn9Phx5xevzq8LyOHlM0DzL", //"X3zjn3WiAoY0qGARY02rAL4wioLCeMeB", //"qIc39tGvYEtEVGW2R9X0KxnaKR8nYw2K", //"b50C0BYG7GyhBW5m2zww6oGk9ZAyOBGo", //"vtDazOQPLuWlOqkdZG80w8doqwFM82C7", //"xggNtYdTQI1Qb4CDDUwDbREaosP8C1sI", //} var account1 = map[string]string{ "API_KEY": "1LVM2db0Ehr931W7gRUimZlF", "SECRET_KEY": "ZZW2xfzC2OAlkInI6YbWGECledb7GLoy", } var account2 = map[string]string{ "API_KEY": "wtipNMdKoeniksPtKGcyqt3N", "SECRET_KEY": "FSdfDdsGEeG0FUqmDgfwWzf64bd1ee3c", } var account3 = map[string]string{ "API_KEY": "l16wv0XqQW8VyFaFkhb7iIdG", "SECRET_KEY": "t8fIX8BYGlzZk3xOeGGuUaGo6kXI9qN6", } var account4 = map[string]string{ "API_KEY": "OGLYftxvRsut3Mj6DszpZGOH", "SECRET_KEY": "2z2UIuKDANFyQSL1V0jSNlH1XkmoGEPQ", } var account5 = map[string]string{ "API_KEY": "RRfRVDugxEtKGZGbWXQj7joF", "SECRET_KEY": "X41FM9VyxOn9Phx5xevzq8LyOHlM0DzL", } var account6 = map[string]string{ "API_KEY": "0vyrRQzhNzmGm4oS5LhPQWAd", "SECRET_KEY": "X3zjn3WiAoY0qGARY02rAL4wioLCeMeB", } var account7 = map[string]string{ "API_KEY": "6tQwK5zGcoDoZReyTjEi6LhO", "SECRET_KEY": "qIc39tGvYEtEVGW2R9X0KxnaKR8nYw2K", } var account8 = map[string]string{ "API_KEY": "8QnWdj3Bbtkls9uT372oEx8A", "SECRET_KEY": "b50C0BYG7GyhBW5m2zww6oGk9ZAyOBGo", } var account9 = map[string]string{ "API_KEY": "zv67LjUDA3mOB6XeuxokhN3O", "SECRET_KEY": "vtDazOQPLuWlOqkdZG80w8doqwFM82C7", } var account10 = map[string]string{ "API_KEY": "k3r4fesYiGCUehA2Zk55TRv3", "SECRET_KEY": "xggNtYdTQI1Qb4CDDUwDbREaosP8C1sI", } var Account = map[int]map[string]string{ 1: account1, 2: account2, 3: account3, 4: account4, 5: account5, 6: account6, 7: account7, 8: account8, 9: account9, 10: account10, } const BaiduIdentify = "baiduIdentifyTokenV2:%d" const AccountLimit = 350 const BaiduIdentifyLimit = "%s:baiduIdentifyTokenV2:%d" const BaiduCurrentAccount = "%s:baiduCurrentAccount" const BaiduCurrentAccountLimit = "%s:baiduCurrentAccountLimit" type BaiduAccount struct { CurrentDate string BaiduCurrentAccountKey string BaiduCurrentAccountLimitKdy string } func (m *BaiduAccount) GetCurrentAccountInfo(ctx context.Context) (currentAccountId int, err error) { currentAccountKey := fmt.Sprintf(BaiduCurrentAccount, m.CurrentDate) currentAccountLimitKey := fmt.Sprintf(BaiduCurrentAccountLimit, m.CurrentDate) m.BaiduCurrentAccountKey = currentAccountKey m.BaiduCurrentAccountLimitKdy = currentAccountLimitKey currentAccountId, err = global.GVA_REDIS.Get(ctx, currentAccountKey).Int() if err != nil { if err == redis.Nil { global.GVA_REDIS.Set(ctx, currentAccountKey, 1, time.Hour*24) global.GVA_REDIS.Set(ctx, currentAccountLimitKey, 1, time.Hour*24) return 1, nil } } return } func (m *BaiduAccount) CheckCurrentAccountLimit(ctx context.Context, id int) (currentAccountId int, err error) { limit, err := global.GVA_REDIS.Get(ctx, m.BaiduCurrentAccountLimitKdy).Int() if err != nil { return } if limit >= AccountLimit { if id < 10 { id += 1 global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountKey, id, time.Hour*24) global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountLimitKdy, 1, time.Hour*24) } else { id = 1 global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountKey, id, time.Hour*24) global.GVA_REDIS.Set(ctx, m.BaiduCurrentAccountLimitKdy, 1, time.Hour*24) } } currentAccountId = id return } func (m *BaiduAccount) GetAccessToken(ctx context.Context, id int) (string, error) { key := fmt.Sprintf(BaiduIdentify, id) token, err := global.GVA_REDIS.Get(ctx, key).Result() if err != nil || token == "" { reqUrl := "https://aip.baidubce.com/oauth/2.0/token" postData := fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s", Account[id]["API_KEY"], Account[id]["SECRET_KEY"]) resp, err := http.Post(reqUrl, "application/x-www-form-urlencoded", strings.NewReader(postData)) if err != nil { fmt.Println(err) return "", err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return "", err } accessTokenObj := map[string]string{} json.Unmarshal(body, &accessTokenObj) global.GVA_REDIS.Set(ctx, key, accessTokenObj["access_token"], time.Hour*24*29) return accessTokenObj["access_token"], err } global.GVA_REDIS.Incr(ctx, m.BaiduCurrentAccountLimitKdy) return token, err }