ugc_playurl.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package goblin
  2. import (
  3. "context"
  4. "encoding/json"
  5. xhttp "net/http"
  6. "net/url"
  7. "go-common/app/interface/main/tv/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. )
  12. const (
  13. _httpHeaderRemoteIP = "x-backend-bili-real-ip"
  14. )
  15. // UgcPlayurl is use for get ugc play url
  16. func (d *Dao) UgcPlayurl(ctx context.Context, p *model.PlayURLReq) (res map[string]interface{}, resp *model.PlayURLResp, err error) {
  17. var (
  18. params = url.Values{}
  19. url = d.conf.Host.UgcPlayURL
  20. bs []byte
  21. req *xhttp.Request
  22. ip = metadata.String(ctx, metadata.RemoteIP)
  23. )
  24. res = make(map[string]interface{})
  25. params.Set("platform", p.Platform)
  26. params.Set("device", p.Device)
  27. params.Set("expire", p.Expire)
  28. params.Set("build", p.Build)
  29. params.Set("mid", p.Mid)
  30. params.Set("qn", p.Qn)
  31. params.Set("npcybs", p.Npcybs)
  32. params.Set("buvid", p.Buvid)
  33. params.Set("otype", "json")
  34. params.Set("trackPath", p.TrackPath)
  35. params.Set("cid", p.Cid)
  36. params.Set("access_key", p.AccessKey)
  37. params.Set("platform", "tvproj")
  38. if req, err = d.client.NewRequest(xhttp.MethodGet, url, ip, params); err != nil {
  39. return
  40. }
  41. if ip != "" { // add ip into header
  42. req.Header.Set(_httpHeaderRemoteIP, ip)
  43. }
  44. log.Info("ugcPlayURL Cid %d, IP %s", p.Cid, ip)
  45. if bs, err = d.client.Raw(ctx, req); err != nil {
  46. log.Error("ugcPl URL %s, Cid %d, Client Raw Err %v", url, p.Cid, err)
  47. return
  48. }
  49. if err = json.Unmarshal(bs, &resp); err != nil { // json unmarshal to struct, to detect error
  50. log.Error("ugcPl URL %s, Cid %d, Json Unmarshal %s, Err %v", url, p.Cid, string(bs), err)
  51. return
  52. }
  53. if resp.Code != ecode.OK.Code() {
  54. log.Error("ugcPl URL %s, Cid %d, Resp Code %d, Msg %s", url, p.Cid, resp.Code, resp.Message)
  55. err = ecode.TvVideoNotFound
  56. return
  57. }
  58. err = json.Unmarshal(bs, &res)
  59. return
  60. }