sync_video.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package ugc
  2. import (
  3. "fmt"
  4. "go-common/app/job/main/tv/dao/lic"
  5. model "go-common/app/job/main/tv/model/pgc"
  6. ugcmdl "go-common/app/job/main/tv/model/ugc"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _crEnd = "1970-01-01" // copyright end date
  12. _definition = "SD"
  13. )
  14. // syncVideoErr: it logs the error and postpone the videos for the next submit
  15. func (s *Service) syncVideoErr(funcName string, cids []int64, aid int64, err error) {
  16. s.dao.PpVideos(ctx, cids)
  17. errArcVideos("syncLic:"+funcName, aid, cids, err)
  18. }
  19. // syncLic: sync our arc data to License owner
  20. func (s *Service) syncLic(cAid int64, arc *ugcmdl.SimpleArc) (err error) {
  21. var (
  22. skeleton = &ugcmdl.LicSke{}
  23. videoPces [][]*ugcmdl.SimpleVideo
  24. ps = s.c.UgcSync.Batch.SyncPS // sync page size
  25. licData *model.License
  26. xmlBody string
  27. errCall error
  28. )
  29. skeleton.Arc = arc
  30. if videoPces, err = s.dao.ParseVideos(ctx, cAid, ps); err != nil {
  31. log.Error("ParseVideos %d Error %v", cAid, err)
  32. return
  33. }
  34. if len(videoPces) == 0 { // no to audit cids
  35. return
  36. }
  37. for _, videos := range videoPces {
  38. skeleton.Videos = videos
  39. var cids = []int64{}
  40. for _, v := range videos {
  41. cids = append(cids, v.CID)
  42. }
  43. if licData, errCall = s.auditMsg(skeleton); errCall != nil { // build the license data and transform to xml
  44. s.syncVideoErr("AuditMsg ", cids, cAid, errCall)
  45. continue
  46. }
  47. xmlBody = lic.PrepareXML(licData)
  48. if _, errCall = s.licDao.CallRetry(ctx, s.c.Sync.API.AddURL, xmlBody); errCall != nil { // call api
  49. s.syncVideoErr("XmlCall ", cids, cAid, errCall)
  50. continue
  51. }
  52. if errCall = s.dao.FinishVideos(ctx, skeleton.Videos, cAid); errCall != nil { // update the arc & videos' submit status to finish
  53. s.syncVideoErr("FinishVideos ", cids, cAid, errCall)
  54. continue
  55. }
  56. infoArcVideos("syncLic", cAid, cids, "Succ Apply For Audit")
  57. }
  58. return
  59. }
  60. // auditMsg transforms a skeleton to license audit message struct for UGC
  61. func (s *Service) auditMsg(skeleton *ugcmdl.LicSke) (licData *model.License, err error) {
  62. var (
  63. programSets []*model.PS
  64. programs []*model.Program
  65. sign = s.c.Sync.Sign
  66. )
  67. if len(skeleton.Videos) > 0 {
  68. if programs, err = s.videoProgram(skeleton.Arc.AID, skeleton.Videos); err != nil {
  69. log.Error("auditMsg videoProgram Aid %d, Err %v", skeleton.Arc.AID, err)
  70. return
  71. }
  72. }
  73. if programSets, err = s.arcPSet(skeleton.Arc, programs); err != nil {
  74. log.Error("arcPSet Error %v", err)
  75. return
  76. }
  77. licData = lic.BuildLic(sign, programSets, len(programs))
  78. return
  79. }
  80. // videoProgram transforms the videos to license defined program models
  81. func (s *Service) videoProgram(aid int64, videos []*ugcmdl.SimpleVideo) (programs []*model.Program, err error) {
  82. var (
  83. ugcPrefix = s.c.Sync.UGCPrefix
  84. deadCIDs = []int64{}
  85. arcValid bool
  86. )
  87. for _, v := range videos {
  88. playurl, hitDead, errCall := s.playurlDao.Playurl(ctx, int(v.CID))
  89. if errCall != nil {
  90. log.Error("Playurl CID %d, Error %v", v.CID, errCall)
  91. continue
  92. }
  93. if hitDead { // hit playurl dead codes
  94. deadCIDs = append(deadCIDs, v.CID)
  95. continue
  96. }
  97. media := model.MakePMedia(ugcPrefix, playurl, v.CID)
  98. program := &model.Program{
  99. ProgramID: fmt.Sprintf("%s%d", ugcPrefix, v.CID),
  100. ProgramName: v.Eptitle,
  101. ProgramLength: int(v.Duration),
  102. ProgramDesc: v.Description,
  103. PublishDate: _crEnd,
  104. Number: fmt.Sprintf("%d", v.IndexOrder),
  105. DefinitionType: "SD",
  106. ProgramMediaList: &model.PMList{
  107. ProgramMedia: []*model.PMedia{
  108. media,
  109. },
  110. },
  111. }
  112. programs = append(programs, program)
  113. }
  114. if len(deadCIDs) > 0 { // treat deadCIDs, delete them
  115. if arcValid, err = s.dao.DelVideoArc(ctx, &ugcmdl.DelVideos{
  116. AID: aid,
  117. CIDs: deadCIDs,
  118. }); err != nil {
  119. log.Error("VideoProgram DelVideos Aid %d, Cids %v, Err %v", aid, deadCIDs, err)
  120. return
  121. }
  122. if !arcValid {
  123. log.Info("VideoProgram DelVideos Aid %d is empty, delete it also", aid)
  124. err = ecode.NothingFound
  125. return
  126. }
  127. if len(deadCIDs) == len(videos) {
  128. log.Info("VideoProgram Passed Videos Aid %d are dead, Cids %v", aid, deadCIDs)
  129. err = ecode.NothingFound
  130. return
  131. }
  132. log.Info("VideoProgram DelVideos Aid %d, Playurl DeadCids %v", aid, deadCIDs)
  133. }
  134. return
  135. }
  136. // arcPSet transforms an archive model to a license programSet model
  137. func (s *Service) arcPSet(arc *ugcmdl.SimpleArc, programs []*model.Program) (ps []*model.PS, err error) {
  138. var (
  139. secondType string
  140. firstType string
  141. copyright = s.c.UgcSync.Cfg.Copyright
  142. upper *ugcmdl.Upper
  143. )
  144. // get second type name
  145. if tp, ok := s.arcTypes[arc.TypeID]; !ok {
  146. log.Error("For Aid %d, Can't find Second TypeID %d Name", arc.AID, arc.TypeID)
  147. } else {
  148. secondType = tp.Name
  149. }
  150. // get first type name
  151. firstType = s.getPTypeName(arc.TypeID)
  152. // build the programSet structure
  153. var program = &model.PS{
  154. ProgramSetID: fmt.Sprintf("%s%d", s.c.Sync.UGCPrefix, arc.AID),
  155. ProgramSetName: arc.Title,
  156. ProgramSetClass: secondType,
  157. ProgramSetType: firstType,
  158. PublishDate: arc.Pubtime,
  159. Copyright: copyright,
  160. ProgramCount: int(arc.Videos),
  161. CREndData: _crEnd,
  162. DefinitionType: _definition,
  163. CpCode: s.c.Sync.LConf.CPCode,
  164. PayStatus: 0,
  165. ProgramSetDesc: arc.Content,
  166. ProgramSetPoster: arc.Cover,
  167. ProgramList: &model.ProgramList{
  168. Program: programs,
  169. },
  170. }
  171. // upper info
  172. if upper, err = s.upDao.LoadUpMeta(ctx, arc.MID); err != nil { // get upper meta info
  173. log.Error("modLic LoadUpMeta Aid %d, Mid %d, Err %v", arc.AID, arc.MID, err)
  174. err = nil
  175. }
  176. if upper != nil {
  177. program.Producer = upper.OriName
  178. program.Portrait = upper.OriFace
  179. }
  180. ps = append(ps, program)
  181. return
  182. }