rpc.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package card
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. arcgrpc "go-common/app/service/main/archive/api"
  7. "go-common/app/service/main/up/dao"
  8. "go-common/app/service/main/up/dao/global"
  9. "go-common/app/service/main/up/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/sync/errgroup"
  13. )
  14. const _avURL = "http://www.bilibili.com/video/av%d"
  15. //ListVideoArchive list videos by avids
  16. func (d *Dao) ListVideoArchive(ctx context.Context, avids []int64) (videos []*model.UpCardVideo, err error) {
  17. archives, err := d.listArchives(ctx, avids)
  18. if err != nil {
  19. log.Error("d.listArchives error(%v) arg(%v)", err, avids)
  20. err = ecode.CreativeArcServiceErr
  21. return
  22. }
  23. for avid, data := range archives {
  24. video := transfer(avid, data)
  25. videos = append(videos, video)
  26. }
  27. return
  28. }
  29. //AvidVideoMap get <avid, video> map by avids
  30. func (d *Dao) AvidVideoMap(ctx context.Context, avids []int64) (avidVideoMap map[int64]*model.UpCardVideo, err error) {
  31. avidVideoMap = make(map[int64]*model.UpCardVideo)
  32. archives, err := d.listArchives(ctx, avids)
  33. if err != nil {
  34. log.Error("d.listArchives error(%v) arg(%v)", err, avids)
  35. err = ecode.CreativeArcServiceErr
  36. return
  37. }
  38. for avid, data := range archives {
  39. video := transfer(avid, data)
  40. avidVideoMap[avid] = video
  41. }
  42. return
  43. }
  44. func (d *Dao) listArchives(ctx context.Context, avids []int64) (archives map[int64]*arcgrpc.Arc, err error) {
  45. archives = make(map[int64]*arcgrpc.Arc)
  46. var (
  47. g errgroup.Group
  48. m sync.Mutex
  49. )
  50. dao.Split(0, len(avids), 300, func(start int, end int) {
  51. g.Go(func() (err error) {
  52. var (
  53. arg = &arcgrpc.ArcsRequest{
  54. Aids: avids[start:end],
  55. }
  56. res *arcgrpc.ArcsReply
  57. )
  58. if res, err = global.GetArcClient().Arcs(ctx, arg); err != nil {
  59. log.Error("d.acc.Archives3 arg(%v) error(%v)", arg, err)
  60. err = nil
  61. } else {
  62. for k, v := range res.Arcs {
  63. m.Lock()
  64. archives[k] = v
  65. m.Unlock()
  66. }
  67. }
  68. return
  69. })
  70. })
  71. if err = g.Wait(); err != nil {
  72. log.Error("g.Wait() error(%v)", err)
  73. }
  74. return
  75. }
  76. func transfer(avid int64, archive *arcgrpc.Arc) (video *model.UpCardVideo) {
  77. return &model.UpCardVideo{
  78. URL: fmt.Sprintf(_avURL, avid),
  79. Title: archive.Title,
  80. Picture: archive.Pic,
  81. Duration: archive.Duration,
  82. CTime: archive.Ctime,
  83. }
  84. }