dao.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package bvc
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "io/ioutil"
  8. "net"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "go-common/app/job/main/videoup/conf"
  15. "go-common/library/log"
  16. "go-common/library/xstr"
  17. )
  18. // Dao is message dao.
  19. type Dao struct {
  20. c *conf.Config
  21. httpCli *http.Client
  22. capableURL string
  23. }
  24. // New new a activity dao.
  25. func New(c *conf.Config) (d *Dao) {
  26. // http://bvc-playurl.bilibili.co/video_capable?cid=18669677&capable=0&ts=1497426598&sign=e822b4ce02fdcf91eb29fd47dcbbc3c8
  27. d = &Dao{
  28. c: c,
  29. httpCli: &http.Client{
  30. Transport: &http.Transport{
  31. Dial: (&net.Dialer{
  32. Timeout: 5 * time.Second,
  33. }).Dial,
  34. DisableKeepAlives: true,
  35. },
  36. Timeout: 10 * time.Second,
  37. },
  38. capableURL: c.Host.Bvc.Bvc + "/video_capable",
  39. }
  40. return
  41. }
  42. // VideoCapable sync cid audit result to bvc.
  43. func (d *Dao) VideoCapable(c context.Context, aid int64, allCids []int64, capable int) (err error) {
  44. var times int
  45. if len(allCids)%50 == 0 {
  46. times = len(allCids) / 50
  47. } else {
  48. times = len(allCids)/50 + 1
  49. }
  50. var cids []int64
  51. for i := 0; i < times; i++ {
  52. if i == times-1 {
  53. cids = allCids[i*50:]
  54. } else {
  55. cids = allCids[i*50 : (i+1)*50]
  56. }
  57. if err = d.call(aid, cids, capable); err != nil {
  58. return
  59. }
  60. }
  61. return
  62. }
  63. func (d *Dao) call(aid int64, cids []int64, capable int) (err error) {
  64. var (
  65. // http params
  66. cidStr = xstr.JoinInts(cids)
  67. capableStr = strconv.Itoa(capable)
  68. ts = strconv.FormatInt(time.Now().Unix(), 10)
  69. signs = []string{cidStr, ts, capableStr, d.c.Host.Bvc.AppendKey}
  70. )
  71. log.Info("VideoCapable aid(%d) cid(%s) capable(%d) process start", aid, cidStr, capable)
  72. params := url.Values{}
  73. params.Set("ts", ts)
  74. params.Set("cid", cidStr)
  75. params.Set("capable", capableStr)
  76. // sign = md5(cid + gap_key + ts + gap_key + capable + gap_key + append_key)
  77. // gap_key(d.c.Host.Bvc.GapKey) = "--->"
  78. signStr := strings.Join(signs, d.c.Host.Bvc.GapKey)
  79. mh := md5.Sum([]byte(signStr))
  80. sign := hex.EncodeToString(mh[:])
  81. params.Set("sign", sign)
  82. url := d.capableURL + "?" + params.Encode()
  83. // http
  84. var (
  85. req *http.Request
  86. resp *http.Response
  87. )
  88. if req, err = http.NewRequest("POST", url, nil); err != nil {
  89. log.Error("VideoCapable cid(%s) http.NewRequest(POST,%s) error(%v)", cidStr, url, err)
  90. return
  91. }
  92. if resp, err = d.httpCli.Do(req); err != nil {
  93. log.Error("VideoCapable cid(%s) d.httpCli.Do() error(%v)", cidStr, err)
  94. return
  95. }
  96. defer resp.Body.Close()
  97. // Update successfully only when response is HTTP/200.(bvc said this)
  98. if resp.StatusCode != http.StatusOK {
  99. log.Error("VideoCapable cid(%s) sync cid result faild. StatusCode(%d)", cidStr, resp.StatusCode)
  100. return
  101. }
  102. var rb []byte
  103. if rb, err = ioutil.ReadAll(resp.Body); err != nil {
  104. log.Error("VideoCapable cid(%s) ioutil.ReadAll(resp.Body) error(%v)", cidStr, err)
  105. err = nil
  106. return
  107. }
  108. var res struct {
  109. Error int `json:"error"`
  110. Message string `json:"message"`
  111. }
  112. if err = json.Unmarshal(rb, &res); err != nil {
  113. log.Error("VideoCapable cid(%s) json.Unmarshal(%s) error(%v)", cidStr, string(rb), err)
  114. err = nil
  115. return
  116. }
  117. log.Info("VideoCapable cid(%s) capable(%d) res.error(%d) or res.message(%v) process end", cidStr, capable, res.Error, res.Message)
  118. return
  119. }