dao.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package player
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-intl/conf"
  8. "go-common/app/interface/main/app-intl/model/player"
  9. httpx "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "github.com/pkg/errors"
  12. )
  13. // Dao is
  14. type Dao struct {
  15. client *httpx.Client
  16. }
  17. // New elec dao
  18. func New(c *conf.Config) (d *Dao) {
  19. d = &Dao{
  20. client: httpx.NewClient(c.HTTPClient),
  21. }
  22. return
  23. }
  24. // Playurl is
  25. func (d *Dao) Playurl(c context.Context, mid, aid, cid, qn int64, npcybs, fnver, fnval, forceHost int, otype, mobiApp, buvid, fp, session, reqURL string) (playurl *player.Playurl, code int, err error) {
  26. ip := metadata.String(c, metadata.RemoteIP)
  27. params := url.Values{}
  28. params.Set("otype", otype)
  29. params.Set("buvid", buvid)
  30. params.Set("platform", mobiApp)
  31. params.Set("mid", strconv.FormatInt(mid, 10))
  32. params.Set("cid", strconv.FormatInt(cid, 10))
  33. params.Set("session", session)
  34. params.Set("force_host", strconv.Itoa(forceHost))
  35. if aid > 0 {
  36. params.Set("avid", strconv.FormatInt(aid, 10))
  37. }
  38. params.Set("fnver", strconv.Itoa(fnver))
  39. params.Set("fnval", strconv.Itoa(fnval))
  40. if qn != 0 {
  41. params.Set("qn", strconv.FormatInt(qn, 10))
  42. }
  43. if npcybs != 0 {
  44. params.Set("npcybs", strconv.Itoa(npcybs))
  45. }
  46. var res struct {
  47. Code int `json:"code"`
  48. *player.Playurl
  49. }
  50. var req *http.Request
  51. if req, err = d.client.NewRequest(http.MethodGet, reqURL, ip, params); err != nil {
  52. err = errors.Wrap(err, "d.client.NewRequest error")
  53. return
  54. }
  55. if fp != "" {
  56. req.Header.Set("X-BVC-FINGERPRINT", fp)
  57. }
  58. if err = d.client.Do(c, req, &res); err != nil {
  59. return
  60. }
  61. playurl = res.Playurl
  62. playurl.FormatDash()
  63. code = res.Code
  64. return
  65. }