package svc import ( offical "applet/app/db/official" md2 "applet/app/es/md" "applet/app/task/md" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/tik_tok" zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils" "encoding/json" "fmt" "github.com/jinzhu/copier" "github.com/tidwall/gjson" "strings" "time" ) func TikTokTask() { //doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskEsIndex, "tik_tok_task_tt99fdef4006c36ff001") //fmt.Println(string(doc.Source)) //return //es.CreateIndexIfNotExists(md2.ZhiosTikTokTaskEsIndex, md2.ZhiosTikTokTaskEsMapping) //es.CreateIndexIfNotExists(md2.ZhiosTikTokTaskDetailEsIndex, md2.ZhiosTikTokTaskDetailEsMapping) appidList := offical.MasterListCfgGetOneData("0", "tik_tok_task_appid") split := strings.Split(appidList, ";") for _, v := range split { endTime := time.Now().Unix() startTime := endTime - 7*86400 if endTime > time.Now().Unix() { endTime = time.Now().Unix() } param := map[string]interface{}{ "appid": v, "create_start_time": startTime, "create_end_time": endTime, } api, _ := tik_tok.TaskApi("query_app_task_id/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param)) taskIds := gjson.Get(api, "data.task_ids").String() fmt.Println(taskIds) if taskIds == "" || taskIds == "[]" { fmt.Println(endTime) offical.MasterListCfgSave("0", "tik_tok_task_appid_time_"+v, utils.Int64ToStr(endTime)) continue } commAddTask("tik_tok_task_"+v, taskIds) var tmp = make([]int64, 0) json.Unmarshal([]byte(taskIds), &tmp) getTaskDetail(v, tmp) fmt.Println(endTime) offical.MasterListCfgSave("0", "tik_tok_task_appid_time_"+v, utils.Int64ToStr(endTime)) } } func getTaskDetail(appId string, tmp []int64) { for _, v := range tmp { param := map[string]interface{}{ "appid": appId, "query_params_type": "1", "query_params_content": utils.Int64ToStr(v), "page_no": "1", "page_size": "10", } api, _ := tik_tok.TaskApi("query_task_info/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param)) detail := gjson.Get(api, "data.tasks").String() if detail == "" { fmt.Println("失败", v) continue } var tmpDetail = make([]md.TikTokTaskDetail, 0) json.Unmarshal([]byte(detail), &tmpDetail) for _, v1 := range tmpDetail { var tmpIn md.TikTokTaskDetailSecond err := copier.Copy(&tmpIn, &v1) fmt.Println(err) tmpIn.OrientedTalentRelList = utils.SerializeStr(v1.OrientedTalentRelList) tmpIn.ReferMaCaptures = utils.SerializeStr(v1.ReferMaCaptures) tmpIn.TaskTags = utils.SerializeStr(v1.TaskTags) tmpIn.ReferVideoCaptures = utils.SerializeStr(v1.ReferVideoCaptures) ex := strings.Split(tmpIn.StartPage, "bookId=") if len(ex) > 1 { exSecond := strings.Split(ex[1], "&") tmpIn.VideoId = exSecond[0] } if tmpIn.VideoId == "" { fmt.Println("失败", tmpIn) continue } commAddTaskDetail(utils.Int64ToStr(tmpIn.TaskId), tmpIn) } } } func commAddTask(uniqueId string, taskIds string) { doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId) if doc == nil { var tmpData = map[string]string{ "task_ids": taskIds, } createDocRet, err := es.CreateDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId, tmpData) if err != nil { return } fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) } else { source := doc.Source tmpData := make(map[string]string) json.Unmarshal(source, &tmpData) var tmpOld = make([]int64, 0) json.Unmarshal([]byte(tmpData["task_ids"]), &tmpOld) var tmp = make([]int64, 0) json.Unmarshal([]byte(taskIds), &tmp) tmp = utils.MergeArr(tmpOld, tmp) tmp = utils.UniqueArr(tmp) tmpData = map[string]string{ "task_ids": utils.SerializeStr(tmp), } createDocRet, err := es.UpdateDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId, tmpData) if err != nil { return } fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) } } func commAddTaskDetail(uniqueId string, tmp md.TikTokTaskDetailSecond) { doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId) if doc == nil { createDocRet, err := es.CreateDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId, tmp) if err != nil { return } fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) } else { createDocRet, err := es.UpdateDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId, tmp) if err != nil { return } fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) } } func TikTokTaskToken() string { tikTokOpenAppkey := offical.MasterListCfgGetOneData("0", "tik_tok_task_open_appkey") tikTokOpenAppSecret := offical.MasterListCfgGetOneData("0", "tik_tok_task_open_app_secret") args := map[string]string{ "appkey": tikTokOpenAppkey, "appSecret": tikTokOpenAppSecret, } key := "tikTok_client_" + args["appkey"] tokeStr, err := cache.GetString(key) if tokeStr == "" || err != nil { token := tik_tok.GetClientToken(args) if token["tik_tok_acc_token"] != "" { tokeStr = token["tik_tok_acc_token"] cache.SetEx(key, token["tik_tok_acc_token"], utils.StrToInt(token["expires_in"])) } } return tokeStr }