share.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. v1 "go-common/app/interface/bbq/app-bbq/api/http/v1"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "github.com/Dai0522/go-hash/murmur3"
  11. )
  12. // GetShareURL .
  13. func (s *Service) GetShareURL(ctx context.Context, mid int64, device *bm.Device, req *v1.ShareRequest) (*v1.ShareResponse, error) {
  14. _, err := s.dao.VideoBase(ctx, mid, req.Svid)
  15. if err != nil {
  16. log.Warnw(ctx, "log", "get video base fail", "svid", req.Svid)
  17. return nil, err
  18. }
  19. token := s.dao.GetUserShareToken(ctx, mid)
  20. if token == "" {
  21. hash := murmur3.NewWithSeed(uint32(time.Now().Unix()))
  22. str := fmt.Sprintf("%d:%s", mid, buvid(device))
  23. token = toHex(hash.Murmur3_128([]byte(str)))
  24. s.dao.SetUserShareToken(ctx, mid, token)
  25. }
  26. var url, params []*v1.Tuple
  27. params = append(params, &v1.Tuple{
  28. Key: "mid",
  29. Val: strconv.Itoa(int(mid)),
  30. }, &v1.Tuple{
  31. Key: "svid",
  32. Val: strconv.Itoa(int(req.Svid)),
  33. }, &v1.Tuple{
  34. Key: "token",
  35. Val: token,
  36. })
  37. url = append(url, &v1.Tuple{
  38. Key: "1",
  39. Val: "https://bbq.bilibili.com/video/?id={svid}&token={token}",
  40. })
  41. url = append(url, &v1.Tuple{
  42. Key: "2",
  43. Val: "https://bbq.bilibili.com/user/?id={mid}&token={token}",
  44. })
  45. return &v1.ShareResponse{
  46. URL: url,
  47. Params: params,
  48. }, nil
  49. }
  50. func toHex(b []byte) string {
  51. var res string
  52. pattern := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "e", "f", "g"}
  53. for _, v := range b {
  54. res += pattern[v&15]
  55. res += pattern[(v>>4)&15]
  56. }
  57. return res
  58. }
  59. // ShareCallback .
  60. func (s *Service) ShareCallback(ctx context.Context, mid int64, device *bm.Device, args *v1.ShareCallbackRequest) (resp *v1.ShareCallbackResponse, err error) {
  61. // 增加分享数
  62. share := int32(0)
  63. if args.Svid != int64(0) {
  64. share, err = s.dao.IncrVideoStatisticsShare(ctx, args.Svid)
  65. if err != nil {
  66. log.Errorv(ctx, log.KV("log", err))
  67. }
  68. }
  69. resp = &v1.ShareCallbackResponse{
  70. ShareCount: share,
  71. }
  72. return
  73. }