pick.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package ugc
  2. import (
  3. "context"
  4. "fmt"
  5. ugcmdl "go-common/app/job/main/tv/model/ugc"
  6. arccli "go-common/app/service/main/archive/api"
  7. arcmdl "go-common/app/service/main/archive/model/archive"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. // VideoApi calls the rpc of video, pick videos of an archive
  12. func (s *Service) videoPick(c context.Context, aid int64) (resp *arccli.ViewReply, err error) {
  13. if err = Retry(func() (err error) {
  14. if resp, err = s.arcClient.View(c, &arccli.ViewRequest{
  15. Aid: aid,
  16. }); err != nil {
  17. log.Error("ArcRPC For Aid: %d, Error: %v", aid, err)
  18. }
  19. return
  20. }, _arcRetry, _sleep); err != nil {
  21. log.Error("upArchives Error %+v", err)
  22. return
  23. }
  24. return
  25. }
  26. // arcAllowImport tells whether the archive is allowed to import into TV database
  27. func (s *Service) arcAllowImport(arc *ugcmdl.ArcAllow) (allowed bool) {
  28. if !arc.CanPlay() {
  29. log.Warn("arcAllowImport Aid %d Not allowed Due to State %d", arc.Aid, arc.State)
  30. return
  31. }
  32. if arc.Ugcpay == arcmdl.AttrYes {
  33. log.Warn("arcAllowImport Aid %d Not allowed Due to Ugcpay %d", arc.Aid, arc.Ugcpay)
  34. return
  35. }
  36. if s.hitPGC(arc.Typeid) {
  37. log.Warn("arcAllowImport Aid %d Not allowed Due to HitPGC %d", arc.Aid, arc.Typeid)
  38. return
  39. }
  40. if !arc.IsOrigin() {
  41. log.Warn("arcAllowImport Aid %d Not allowed Due to Not Origin", arc.Aid, arc.Copyright)
  42. }
  43. allowed = true
  44. return
  45. }
  46. // Archive calls the api of Archive, pick the archive data
  47. func (s *Service) arcPick(c context.Context, aid int64) (arc *arccli.Arc, err error) {
  48. var arcReply *arccli.ArcReply
  49. for i := 0; i < _arcRetry; i++ {
  50. if arcReply, err = s.arcClient.Arc(c, &arccli.ArcRequest{Aid: aid}); err == nil {
  51. break
  52. }
  53. }
  54. if err != nil {
  55. log.Error("upArchives Aid %d Error %v", aid, err)
  56. return
  57. }
  58. if arcReply == nil || arcReply.Arc == nil {
  59. err = ecode.NothingFound
  60. return
  61. }
  62. arc = arcReply.Arc
  63. return
  64. }
  65. // ArcCount counts the mid's archive, pick the number
  66. func (s *Service) arcCount(mid int64) (count int, err error) {
  67. if err = Retry(func() (err error) {
  68. if count, err = s.arcRPC.UpCount2(ctx, &arcmdl.ArgUpCount2{
  69. Mid: mid,
  70. }); err != nil {
  71. log.Error("ArcCount For Mid: %d, Error: %v", mid, err)
  72. }
  73. return
  74. }, _arcRetry, _sleep); err != nil {
  75. log.Error("upArchives Error %+v", err)
  76. }
  77. return
  78. }
  79. // arcViews picks the views of the given page of aids
  80. func (s *Service) arcViews(aids []int64) (res map[int64]*arccli.ViewReply, err error) {
  81. var resp *arccli.ViewsReply
  82. if err = Retry(func() (err error) {
  83. if resp, err = s.arcClient.Views(ctx, &arccli.ViewsRequest{
  84. Aids: aids,
  85. }); err != nil {
  86. log.Error("%+v", err)
  87. }
  88. return
  89. }, _arcRetry, _sleep); err != nil {
  90. log.Error("upArchives Error %+v", err)
  91. return
  92. } else if len(resp.Views) == 0 {
  93. err = fmt.Errorf("result empty")
  94. return
  95. }
  96. res = resp.Views
  97. return
  98. }
  99. // VideoApi calls the rpc of video, pick videos of an archive
  100. func (s *Service) pagePick(c context.Context, cid int64, aid int64, ip string) (res *arccli.Page, err error) {
  101. if err = Retry(func() (err error) {
  102. if res, err = s.arcRPC.Video3(c, &arcmdl.ArgVideo2{
  103. Aid: aid,
  104. Cid: cid,
  105. RealIP: ip,
  106. }); err != nil {
  107. log.Error("ArcRPC For Aid: %d, Cid: %d, Error: %v", aid, cid, err)
  108. }
  109. return
  110. }, _arcRetry, _sleep); err != nil {
  111. log.Error("upArchives Error %+v", err)
  112. return
  113. } else if res == nil {
  114. err = fmt.Errorf("result empty")
  115. }
  116. return
  117. }