智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.1 KiB

  1. package svc
  2. import (
  3. offical "applet/app/db/official"
  4. md2 "applet/app/es/md"
  5. "applet/app/task/md"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/tik_tok"
  10. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/jinzhu/copier"
  14. "github.com/tidwall/gjson"
  15. "strings"
  16. "time"
  17. )
  18. func TikTokTask() {
  19. //doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskEsIndex, "tik_tok_task_tt99fdef4006c36ff001")
  20. //fmt.Println(string(doc.Source))
  21. //return
  22. //es.CreateIndexIfNotExists(md2.ZhiosTikTokTaskEsIndex, md2.ZhiosTikTokTaskEsMapping)
  23. //es.CreateIndexIfNotExists(md2.ZhiosTikTokTaskDetailEsIndex, md2.ZhiosTikTokTaskDetailEsMapping)
  24. appidList := offical.MasterListCfgGetOneData("0", "tik_tok_task_appid")
  25. split := strings.Split(appidList, ";")
  26. for _, v := range split {
  27. endTime := time.Now().Unix()
  28. startTime := endTime - 7*86400
  29. if endTime > time.Now().Unix() {
  30. endTime = time.Now().Unix()
  31. }
  32. param := map[string]interface{}{
  33. "appid": v,
  34. "create_start_time": startTime,
  35. "create_end_time": endTime,
  36. }
  37. api, _ := tik_tok.TaskApi("query_app_task_id/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param))
  38. taskIds := gjson.Get(api, "data.task_ids").String()
  39. fmt.Println(taskIds)
  40. if taskIds == "" || taskIds == "[]" {
  41. fmt.Println(endTime)
  42. offical.MasterListCfgSave("0", "tik_tok_task_appid_time_"+v, utils.Int64ToStr(endTime))
  43. continue
  44. }
  45. commAddTask("tik_tok_task_"+v, taskIds)
  46. var tmp = make([]int64, 0)
  47. json.Unmarshal([]byte(taskIds), &tmp)
  48. getTaskDetail(v, tmp)
  49. fmt.Println(endTime)
  50. offical.MasterListCfgSave("0", "tik_tok_task_appid_time_"+v, utils.Int64ToStr(endTime))
  51. }
  52. }
  53. func getTaskDetail(appId string, tmp []int64) {
  54. for _, v := range tmp {
  55. param := map[string]interface{}{
  56. "appid": appId,
  57. "query_params_type": "1",
  58. "query_params_content": utils.Int64ToStr(v),
  59. "page_no": "1",
  60. "page_size": "10",
  61. }
  62. api, _ := tik_tok.TaskApi("query_task_info/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param))
  63. detail := gjson.Get(api, "data.tasks").String()
  64. if detail == "" {
  65. fmt.Println("失败", v)
  66. continue
  67. }
  68. var tmpDetail = make([]md.TikTokTaskDetail, 0)
  69. json.Unmarshal([]byte(detail), &tmpDetail)
  70. for _, v1 := range tmpDetail {
  71. var tmpIn md.TikTokTaskDetailSecond
  72. err := copier.Copy(&tmpIn, &v1)
  73. fmt.Println(err)
  74. tmpIn.OrientedTalentRelList = utils.SerializeStr(v1.OrientedTalentRelList)
  75. tmpIn.ReferMaCaptures = utils.SerializeStr(v1.ReferMaCaptures)
  76. tmpIn.TaskTags = utils.SerializeStr(v1.TaskTags)
  77. tmpIn.ReferVideoCaptures = utils.SerializeStr(v1.ReferVideoCaptures)
  78. ex := strings.Split(tmpIn.StartPage, "bookId=")
  79. if len(ex) > 1 {
  80. exSecond := strings.Split(ex[1], "&")
  81. tmpIn.VideoId = exSecond[0]
  82. }
  83. if tmpIn.VideoId == "" {
  84. fmt.Println("失败", tmpIn)
  85. continue
  86. }
  87. commAddTaskDetail(utils.Int64ToStr(tmpIn.TaskId), tmpIn)
  88. }
  89. }
  90. }
  91. func commAddTask(uniqueId string, taskIds string) {
  92. doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId)
  93. if doc == nil {
  94. var tmpData = map[string]string{
  95. "task_ids": taskIds,
  96. }
  97. createDocRet, err := es.CreateDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId, tmpData)
  98. if err != nil {
  99. return
  100. }
  101. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  102. } else {
  103. source := doc.Source
  104. tmpData := make(map[string]string)
  105. json.Unmarshal(source, &tmpData)
  106. var tmpOld = make([]int64, 0)
  107. json.Unmarshal([]byte(tmpData["task_ids"]), &tmpOld)
  108. var tmp = make([]int64, 0)
  109. json.Unmarshal([]byte(taskIds), &tmp)
  110. tmp = utils.MergeArr(tmpOld, tmp)
  111. tmp = utils.UniqueArr(tmp)
  112. tmpData = map[string]string{
  113. "task_ids": utils.SerializeStr(tmp),
  114. }
  115. createDocRet, err := es.UpdateDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId, tmpData)
  116. if err != nil {
  117. return
  118. }
  119. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  120. }
  121. }
  122. func commAddTaskDetail(uniqueId string, tmp md.TikTokTaskDetailSecond) {
  123. doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId)
  124. if doc == nil {
  125. createDocRet, err := es.CreateDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId, tmp)
  126. if err != nil {
  127. return
  128. }
  129. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  130. } else {
  131. createDocRet, err := es.UpdateDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId, tmp)
  132. if err != nil {
  133. return
  134. }
  135. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  136. }
  137. }
  138. func TikTokTaskToken() string {
  139. tikTokOpenAppkey := offical.MasterListCfgGetOneData("0", "tik_tok_task_open_appkey")
  140. tikTokOpenAppSecret := offical.MasterListCfgGetOneData("0", "tik_tok_task_open_app_secret")
  141. args := map[string]string{
  142. "appkey": tikTokOpenAppkey,
  143. "appSecret": tikTokOpenAppSecret,
  144. }
  145. key := "tikTok_client_" + args["appkey"]
  146. tokeStr, err := cache.GetString(key)
  147. if tokeStr == "" || err != nil {
  148. token := tik_tok.GetClientToken(args)
  149. if token["tik_tok_acc_token"] != "" {
  150. tokeStr = token["tik_tok_acc_token"]
  151. cache.SetEx(key, token["tik_tok_acc_token"], utils.StrToInt(token["expires_in"]))
  152. }
  153. }
  154. return tokeStr
  155. }