media.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package pgc
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/tv/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. // snAuth picks the season's auth status and validate it
  10. func (s *Service) snAuth(sid int64) (msg string, err error) {
  11. var season *model.SnAuth
  12. if season, err = s.cmsDao.SnAuth(ctx, sid); err != nil {
  13. log.Error("snVerify Sid %d, Err %v", sid, err)
  14. err = ecode.NothingFound
  15. return
  16. }
  17. if !season.CanPlay() {
  18. err = ecode.CopyrightLimit
  19. _, msg = s.cmsDao.SnErrMsg(season) // season auth failure msg
  20. }
  21. return
  22. }
  23. func (s *Service) snDecor(core *model.SnDetailCore) {
  24. if snCMS, err := s.cmsDao.LoadSnCMS(ctx, core.SeasonID); err == nil {
  25. core.CmsInterv(snCMS)
  26. } else {
  27. log.Warn("snDecor LoadSnCMS %d, err %v", core.SeasonID, err)
  28. }
  29. core.StyleLabel = s.styleLabel[core.SeasonID]
  30. }
  31. // epAuthDecor checks epids status, hide auditing episodes
  32. func (s *Service) epAuthDecor(epids []int64) (res map[int64]*model.EpDecor, msg string, err error) {
  33. var (
  34. epsMetaMap map[int64]*model.EpCMS
  35. epsAuthMap map[int64]*model.EpAuth
  36. )
  37. res = make(map[int64]*model.EpDecor, len(epids))
  38. if epsAuthMap, err = s.cmsDao.LoadEpsAuthMap(ctx, epids); err != nil {
  39. log.Warn("FullIntervs LoadEpsAuthMap epids %v, err %v", epids, err)
  40. return
  41. }
  42. if epsMetaMap, err = s.cmsDao.LoadEpsCMS(ctx, epids); err != nil {
  43. log.Warn("FullIntervs LoadEpsCMS epids %v, err %v", epids, err)
  44. return
  45. }
  46. for _, v := range epids {
  47. if epAuth, ok := epsAuthMap[v]; ok {
  48. if epAuth.CanPlay() { // we hide not passed episodes
  49. decor := &model.EpDecor{
  50. Watermark: epAuth.Whitelist(),
  51. }
  52. if epMeta, okMeta := epsMetaMap[v]; okMeta { // ep intervention
  53. decor.EpCMS = epMeta
  54. }
  55. res[v] = decor
  56. }
  57. }
  58. }
  59. if len(res) == 0 { // return err
  60. err = ecode.CopyrightLimit
  61. msg = s.conf.Cfg.AuthMsg.PGCOffline
  62. log.Warn("Epids %v, After filter empty", epids)
  63. }
  64. return
  65. }
  66. // SnDetail validates the season is authorized to play and involve the intervention
  67. func (s *Service) SnDetail(c context.Context, param *model.MediaParam) (detail *model.SeasonDetail, msg string, err error) {
  68. var (
  69. sid = param.SeasonID
  70. epids []int64
  71. decors map[int64]*model.EpDecor
  72. cfg = s.conf.Cfg.VipMark
  73. )
  74. if msg, err = s.snAuth(sid); err != nil {
  75. return
  76. }
  77. if detail, err = s.dao.Media(ctx, param); err != nil { // pgc media api
  78. log.Error("DAO MediaDetail Sid %d, Error (%v)", sid, err)
  79. return
  80. }
  81. // filter auditing eps, and do ep intervention and watermark logic
  82. for _, v := range detail.Episodes {
  83. if cfg.V1HideChargeable { // before vip version goes online, we still hide the chargeable episodes
  84. if v.EpisodeStatus != cfg.EpFree {
  85. continue
  86. }
  87. }
  88. epids = append(epids, v.EPID)
  89. }
  90. if decors, msg, err = s.epAuthDecor(epids); err != nil {
  91. return
  92. }
  93. offAuditing := make([]*model.Episode, 0, len(decors))
  94. for _, v := range detail.Episodes {
  95. if decor, ok := decors[v.EPID]; ok {
  96. if decor.EpCMS != nil {
  97. v.CmsInterv(decor.EpCMS)
  98. }
  99. v.WaterMark = decor.Watermark
  100. offAuditing = append(offAuditing, v)
  101. }
  102. }
  103. detail.Episodes = offAuditing
  104. s.snDecor(&detail.SnDetailCore)
  105. return
  106. }
  107. // SnDetailV2 validates the season is authorized to play and involve the intervention
  108. func (s *Service) SnDetailV2(c context.Context, param *model.MediaParam) (detail *model.SnDetailV2, msg string, err error) {
  109. var (
  110. sid = param.SeasonID
  111. epids []int64
  112. decors map[int64]*model.EpDecor
  113. cmarkCfg = s.conf.Cfg.VipMark
  114. )
  115. if msg, err = s.snAuth(sid); err != nil {
  116. return
  117. }
  118. if detail, err = s.dao.MediaV2(ctx, param); err != nil || detail == nil { // pgc media api v2
  119. log.Error("DAO MediaDetail Sid %d, Error (%v)", sid, err)
  120. return
  121. }
  122. detail.TypeTrans()
  123. if len(detail.Section) > 0 { // pgc media api v2 logic, prevues are in the sections, we need to pick them up and re-insert into the episodes list
  124. for _, v := range detail.Section {
  125. detail.Episodes = append(detail.Episodes, v.Episodes...)
  126. }
  127. }
  128. for _, v := range detail.Episodes {
  129. epids = append(epids, v.ID)
  130. }
  131. if decors, msg, err = s.epAuthDecor(epids); err != nil {
  132. return
  133. }
  134. offAuditing := make([]*model.EpisodeV2, 0, len(decors))
  135. for _, v := range detail.Episodes {
  136. if decor, ok := decors[v.ID]; ok {
  137. if decor.EpCMS != nil {
  138. v.CmsInterv(decor.EpCMS)
  139. }
  140. v.WaterMark = decor.Watermark
  141. if v.Status != cmarkCfg.EpFree { // if ep is not free, put the corner mark
  142. v.CornerMark = &(*cmarkCfg.EP)
  143. }
  144. offAuditing = append(offAuditing, v)
  145. }
  146. }
  147. detail.Episodes = offAuditing
  148. s.snDecor(&detail.SnDetailCore)
  149. return
  150. }
  151. // EpControl validates the ep is authorized to play and involve the intervention
  152. func (s *Service) EpControl(c context.Context, epid int64) (sid int64, msg string, err error) {
  153. var ep *model.EpAuth
  154. if ep, err = s.cmsDao.EpAuth(c, epid); err != nil {
  155. log.Error("LoadEP Epid %d Error(%v)", epid, err)
  156. err = ecode.NothingFound
  157. return
  158. }
  159. if !ep.CanPlay() {
  160. err = ecode.CopyrightLimit
  161. _, msg = s.cmsDao.EpErrMsg(ep) // ep auth failure msg
  162. return
  163. }
  164. sid = ep.SeasonID
  165. return
  166. }
  167. func (s *Service) upStyleCache() {
  168. for {
  169. res, err := s.dao.GetLabelCache(context.Background())
  170. if err != nil {
  171. log.Error("s.dao.GetLabelCache upStyleCache error(%s)", err)
  172. time.Sleep(5 * time.Second)
  173. continue
  174. }
  175. if len(res) > 0 {
  176. s.styleLabel = res
  177. }
  178. time.Sleep(time.Duration(s.conf.Style.LabelSpan))
  179. }
  180. }
  181. func (s *Service) styleCache() {
  182. res, err := s.dao.GetLabelCache(context.Background())
  183. if err != nil {
  184. log.Error("s.dao.GetLabelCache error(%v)", err)
  185. panic(err)
  186. }
  187. s.styleLabel = res
  188. }