dao.go 1.9 KB

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