playurl.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/player/model"
  8. "go-common/library/ecode"
  9. "go-common/library/net/metadata"
  10. "github.com/pkg/errors"
  11. )
  12. // Playurl get playurl data.
  13. func (d *Dao) Playurl(c context.Context, mid int64, arg *model.PlayurlArg, playurl, token string) (data *model.PlayurlRes, err error) {
  14. params := url.Values{}
  15. params.Set("cid", strconv.FormatInt(arg.Cid, 10))
  16. params.Set("avid", strconv.FormatInt(arg.Aid, 10))
  17. params.Set("qn", strconv.Itoa(arg.Qn))
  18. if arg.Type != "" {
  19. params.Set("type", arg.Type)
  20. }
  21. if arg.MaxBackup > 0 {
  22. params.Set("max_backup", strconv.Itoa(arg.MaxBackup))
  23. }
  24. if arg.Npcybs > 0 {
  25. params.Set("npcybs", strconv.Itoa(arg.Npcybs))
  26. }
  27. if mid > 0 {
  28. params.Set("mid", strconv.FormatInt(mid, 10))
  29. }
  30. if arg.Platform != "" {
  31. params.Set("platform", arg.Platform)
  32. }
  33. if arg.Buvid != "" {
  34. params.Set("buvid", arg.Buvid)
  35. }
  36. if arg.Resolution != "" {
  37. params.Set("resolution", arg.Resolution)
  38. }
  39. if arg.Model != "" {
  40. params.Set("model", arg.Model)
  41. }
  42. if arg.Build > 0 {
  43. params.Set("build", strconv.Itoa(arg.Build))
  44. }
  45. params.Set("fnver", strconv.Itoa(arg.Fnver))
  46. if arg.Fnval > 0 {
  47. params.Set("fnval", strconv.Itoa(arg.Fnval))
  48. }
  49. if arg.Session != "" {
  50. params.Set("session", arg.Session)
  51. }
  52. if arg.HTML5 > 0 && arg.H5GoodQuality > 0 {
  53. params.Set("h5_good_quality", strconv.Itoa(arg.H5GoodQuality))
  54. }
  55. params.Set("otype", "json")
  56. var req *http.Request
  57. if req, err = d.client.NewRequest(http.MethodGet, playurl, metadata.String(c, metadata.RemoteIP), params); err != nil {
  58. return
  59. }
  60. if token != "" {
  61. req.Header.Set("X-BVC-FINGERPRINT", token)
  62. }
  63. var res struct {
  64. Code int `json:"code"`
  65. *model.PlayurlRes
  66. }
  67. if err = d.client.Do(c, req, &res); err != nil {
  68. err = errors.Wrapf(err, "d.client.Do(%s) (%+v)", playurl+"?"+params.Encode(), arg)
  69. return
  70. }
  71. if res.Code != ecode.OK.Code() {
  72. err = errors.Wrap(ecode.Int(res.Code), playurl+"?"+params.Encode())
  73. }
  74. data = res.PlayurlRes
  75. return
  76. }