智盟项目
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.

208 lines
6.5 KiB

  1. package svc
  2. import (
  3. "applet/app/cfg"
  4. offical "applet/app/db/official"
  5. md2 "applet/app/es/md"
  6. "applet/app/task/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/tik_tok"
  11. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  12. "encoding/json"
  13. "fmt"
  14. "github.com/jinzhu/copier"
  15. "github.com/syyongx/php2go"
  16. "github.com/tidwall/gjson"
  17. "strings"
  18. "time"
  19. )
  20. func TikTokTask() {
  21. //doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskEsIndex, "tik_tok_task_tt99fdef4006c36ff001")
  22. //fmt.Println(string(doc.Source))
  23. //return
  24. //es.CreateIndexIfNotExists(md2.ZhiosTikTokTaskEsIndex, md2.ZhiosTikTokTaskEsMapping)
  25. //es.CreateIndexIfNotExists(md2.ZhiosTikTokTaskDetailEsIndex, md2.ZhiosTikTokTaskDetailEsMapping)
  26. appidList := offical.MasterListCfgGetOneData("0", "tik_tok_task_appid")
  27. split := strings.Split(appidList, ";")
  28. for _, v := range split {
  29. endTime := time.Now().Unix()
  30. startTime := endTime - 7*86400
  31. if endTime > time.Now().Unix() {
  32. endTime = time.Now().Unix()
  33. }
  34. param := map[string]interface{}{
  35. "appid": v,
  36. "create_start_time": startTime,
  37. "create_end_time": endTime,
  38. }
  39. api, _ := tik_tok.TaskApi("query_app_task_id/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param))
  40. taskIds := gjson.Get(api, "data.task_ids").String()
  41. fmt.Println(taskIds)
  42. if taskIds == "" || taskIds == "[]" {
  43. fmt.Println(endTime)
  44. offical.MasterListCfgSave("0", "tik_tok_task_appid_time_"+v, utils.Int64ToStr(endTime))
  45. continue
  46. }
  47. CommAddTask("tik_tok_task_"+v, taskIds)
  48. var tmp = make([]int64, 0)
  49. json.Unmarshal([]byte(taskIds), &tmp)
  50. GetTaskDetail(v, tmp)
  51. fmt.Println(endTime)
  52. offical.MasterListCfgSave("0", "tik_tok_task_appid_time_"+v, utils.Int64ToStr(endTime))
  53. }
  54. }
  55. func GetTaskDetail(appId string, tmp []int64) {
  56. for _, v := range tmp {
  57. time.Sleep(time.Microsecond * 1000)
  58. param := map[string]interface{}{
  59. "appid": appId,
  60. "query_params_type": "1",
  61. "query_params_content": utils.Int64ToStr(v),
  62. "page_no": "1",
  63. "page_size": "10",
  64. }
  65. api, _ := tik_tok.TaskApi("query_task_info/", TikTokTaskToken(), zhios_third_party_utils.SerializeStr(param))
  66. detail := gjson.Get(api, "data.tasks").String()
  67. if detail == "" {
  68. fmt.Println("失败", v)
  69. continue
  70. }
  71. var tmpDetail = make([]md.TikTokTaskDetail, 0)
  72. json.Unmarshal([]byte(detail), &tmpDetail)
  73. for _, v1 := range tmpDetail {
  74. var tmpIn md.TikTokTaskDetailSecond
  75. err := copier.Copy(&tmpIn, &v1)
  76. fmt.Println(err)
  77. tmpIn.OrientedTalentRelList = utils.SerializeStr(v1.OrientedTalentRelList)
  78. tmpIn.ReferMaCaptures = utils.SerializeStr(v1.ReferMaCaptures)
  79. tmpIn.TaskTags = utils.SerializeStr(v1.TaskTags)
  80. tmpIn.ReferVideoCaptures = utils.SerializeStr(v1.ReferVideoCaptures)
  81. videoTmpStr := "bookId="
  82. if tmpIn.Appid == "tt38e94e54a7d70c4601" {
  83. videoTmpStr = "cid="
  84. } else if strings.Contains(tmpIn.StartPage, "novelid=") {
  85. videoTmpStr = "novelid="
  86. } else if strings.Contains(tmpIn.StartPage, "video_id=") {
  87. videoTmpStr = "video_id="
  88. }
  89. ex := strings.Split(tmpIn.StartPage, videoTmpStr)
  90. if len(ex) > 1 {
  91. exSecond := strings.Split(ex[1], "&")
  92. tmpIn.VideoId = exSecond[0]
  93. }
  94. if tmpIn.VideoId == "" {
  95. fmt.Println("失败", tmpIn)
  96. continue
  97. }
  98. if tmpIn.Status != 2 {
  99. tmpIn.TaskEndTime = int(time.Now().Unix() - 10)
  100. }
  101. for _, v2 := range md.TaskAddList {
  102. if tmpIn.Appid == v2["name"] { //
  103. tmp2 := md.Cinema5bmsData{
  104. Description: "",
  105. ImageUrl: tmpIn.TaskIcon,
  106. Material: "",
  107. OnlineTime: time.Unix(int64(tmpIn.TaskStartTime), 0).Format("2006-01-02 15:04:05"),
  108. Type: v2["second_type"],
  109. Title: tmpIn.TaskName,
  110. UpdateTime: int(time.Now().Unix()),
  111. Time: tmpIn.TaskStartTime,
  112. Appid: v2["name"],
  113. Id: utils.StrToInt(tmpIn.VideoId),
  114. IsClose: 0,
  115. IsHot: 0,
  116. FirstCate: v2["type"],
  117. SecondCate: "",
  118. Platform: "tikTok",
  119. }
  120. if tmpIn.TaskEndTime < int(time.Now().Unix()) {
  121. tmp2.IsClose = 1
  122. }
  123. uniqueId := php2go.Md5(tmp2.Appid + "_" + tmp2.Type + "_" + tmp2.FirstCate + "_" + utils.IntToStr(tmp2.Id))
  124. fmt.Println(uniqueId)
  125. tmp2.Key = uniqueId
  126. commAddNew(uniqueId, tmp2)
  127. }
  128. }
  129. CommAddTaskDetail(utils.Int64ToStr(tmpIn.TaskId), tmpIn)
  130. }
  131. }
  132. }
  133. func CommAddTask(uniqueId string, taskIds string) {
  134. doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId)
  135. if doc == nil {
  136. var tmpData = map[string]string{
  137. "task_ids": taskIds,
  138. }
  139. createDocRet, err := es.CreateDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId, tmpData)
  140. if err != nil {
  141. return
  142. }
  143. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  144. } else {
  145. source := doc.Source
  146. tmpData := make(map[string]string)
  147. json.Unmarshal(source, &tmpData)
  148. var tmpOld = make([]int64, 0)
  149. json.Unmarshal([]byte(tmpData["task_ids"]), &tmpOld)
  150. var tmp = make([]int64, 0)
  151. json.Unmarshal([]byte(taskIds), &tmp)
  152. tmp = utils.MergeArr(tmpOld, tmp)
  153. tmp = utils.UniqueArr(tmp)
  154. tmpData = map[string]string{
  155. "task_ids": utils.SerializeStr(tmp),
  156. }
  157. createDocRet, err := es.UpdateDoc(md2.ZhiosTikTokTaskEsIndex, uniqueId, tmpData)
  158. if err != nil {
  159. return
  160. }
  161. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  162. }
  163. }
  164. func CommAddTaskDetail(uniqueId string, tmp md.TikTokTaskDetailSecond) {
  165. doc, _ := es.FirstDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId)
  166. if doc == nil {
  167. createDocRet, err := es.CreateDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId, tmp)
  168. if err != nil {
  169. return
  170. }
  171. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  172. } else {
  173. createDocRet, err := es.UpdateDoc(md2.ZhiosTikTokTaskDetailEsIndex, uniqueId, tmp)
  174. if err != nil {
  175. return
  176. }
  177. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  178. }
  179. }
  180. func TikTokTaskToken() string {
  181. tikTokOpenAppkey := offical.MasterListCfgGetOneData("0", "tik_tok_task_open_appkey")
  182. tikTokOpenAppSecret := offical.MasterListCfgGetOneData("0", "tik_tok_task_open_app_secret")
  183. args := map[string]string{
  184. "appkey": tikTokOpenAppkey,
  185. "appSecret": tikTokOpenAppSecret,
  186. }
  187. key := "tikTok_client_" + args["appkey"]
  188. tokeStr, err := cache.GetString(key)
  189. if cfg.Prd == false {
  190. return tokeStr
  191. }
  192. if tokeStr == "" || err != nil {
  193. token := tik_tok.GetClientToken(args)
  194. if token["tik_tok_acc_token"] != "" {
  195. tokeStr = token["tik_tok_acc_token"]
  196. cache.SetEx(key, token["tik_tok_acc_token"], utils.StrToInt(token["expires_in"]))
  197. }
  198. }
  199. return tokeStr
  200. }