playurl.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package playurl
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. model "go-common/app/job/main/tv/model/pgc"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _type = "mp4"
  11. _maxBackup = 0
  12. _otype = "json"
  13. )
  14. // Playurl calls the api of playurl to get the url to play the video
  15. func (d *Dao) Playurl(ctx context.Context, cid int) (playurl string, hitDead bool, err error) {
  16. var (
  17. result = model.PlayurlResp{}
  18. params = url.Values{}
  19. api = d.conf.Sync.PlayURL.API
  20. originURL string
  21. )
  22. params.Set("cid", fmt.Sprintf("%d", cid))
  23. params.Set("type", _type) // to get one piece
  24. params.Set("max_backup", fmt.Sprintf("%d", _maxBackup)) // no backup url needed
  25. params.Set("otype", _otype) // json format response
  26. params.Set("qn", d.conf.Sync.PlayURL.Qn) // quality fix to 16
  27. if err = d.client.Get(ctx, api, "", params, &result); err != nil {
  28. log.Error("ClientGet error[%v]", err)
  29. return
  30. }
  31. if result.Code != 0 { // logic error
  32. for _, v := range d.conf.Sync.PlayURL.Deadcodes {
  33. if result.Code == v { // hit dead code
  34. hitDead = true
  35. return
  36. }
  37. }
  38. err = fmt.Errorf("Resp Code:[%v], Message:[%v]", result.Code, result.Message)
  39. return
  40. }
  41. if len(result.Durl) < 1 { // result empty
  42. err = fmt.Errorf("Playurl Result is Empty! Resp (%v)", result)
  43. return
  44. }
  45. originURL = result.Durl[0].URL
  46. if playurl, err = d.hostChange(originURL); err != nil { // replace the host of the playurl
  47. log.Error("HostChange Origin: %s, Error: %v", originURL, err)
  48. }
  49. return
  50. }
  51. // replace the url's host by TV's dedicated host
  52. func (d *Dao) hostChange(playurl string) (replaced string, err error) {
  53. var host = d.conf.Sync.PlayURL.PlayPath
  54. u, err := url.Parse(playurl)
  55. if err != nil {
  56. log.Error("hostChange ParseURL error (%v)", err)
  57. return
  58. }
  59. log.Info("[hostChange] for URL: %s, Original Host: %s, Now we change it to: %s", playurl, u.Host, host)
  60. u.Host = host // replace the host
  61. u.RawQuery = "" // remove useless query
  62. replaced = u.String()
  63. return
  64. }