live.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package notice
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _liveSmallVideoLink = "http://vc.bilibili.com/video/%d"
  11. _liveNoticeLink = "http://link.bilibili.com/p/eden/news#/newsdetail?id=%d"
  12. _livePictureLink = "http://h.bilibili.com/ywh/%d"
  13. )
  14. // LiveSmallVideo return link.
  15. func (d *Dao) LiveSmallVideo(c context.Context, oid int64) (title, link string, err error) {
  16. params := url.Values{}
  17. params.Set("video_id", strconv.FormatInt(oid, 10))
  18. var res struct {
  19. Code int `json:"code"`
  20. Data *struct {
  21. Item *struct {
  22. Description string `json:"description"`
  23. } `json:"item"`
  24. } `json:"data"`
  25. }
  26. if err = d.httpClient.Get(c, d.urlLiveSmallVideo, "", params, &res); err != nil {
  27. log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLiveSmallVideo, params.Encode(), err)
  28. return
  29. }
  30. if res.Code != 0 || res.Data == nil || res.Data.Item == nil {
  31. err = fmt.Errorf("url:%s?%s code:%d", d.urlLiveSmallVideo, params.Encode(), res.Code)
  32. return
  33. }
  34. title = res.Data.Item.Description
  35. link = fmt.Sprintf(_liveSmallVideoLink, oid)
  36. return
  37. }
  38. // LiveActivity return link.
  39. func (d *Dao) LiveActivity(c context.Context, oid int64) (title, link string, err error) {
  40. params := url.Values{}
  41. params.Set("id", strconv.FormatInt(oid, 10))
  42. var res struct {
  43. Code int `json:"code"`
  44. Data *struct {
  45. Name string `json:"name"`
  46. URL string `json:"url"`
  47. } `json:"data"`
  48. }
  49. if err = d.httpClient.Get(c, d.urlLiveActivity, "", params, &res); err != nil {
  50. log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLiveActivity, params.Encode(), err)
  51. return
  52. }
  53. if res.Code != 0 || res.Data == nil {
  54. err = fmt.Errorf("url:%s?%s code:%d", d.urlLiveActivity, params.Encode(), res.Code)
  55. return
  56. }
  57. title = res.Data.Name
  58. link = res.Data.URL
  59. return
  60. }
  61. // LiveNotice return link.
  62. func (d *Dao) LiveNotice(c context.Context, oid int64) (title, link string, err error) {
  63. params := url.Values{}
  64. params.Set("id", strconv.FormatInt(oid, 10))
  65. var res struct {
  66. Code int `json:"code"`
  67. Data *struct {
  68. Title string `json:"title"`
  69. } `json:"data"`
  70. }
  71. if err = d.httpClient.Get(c, d.urlLiveNotice, "", params, &res); err != nil {
  72. log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLiveNotice, params.Encode(), err)
  73. return
  74. }
  75. if res.Code != 0 || res.Data == nil {
  76. err = fmt.Errorf("url:%s?%s code:%d", d.urlLiveNotice, params.Encode(), res.Code)
  77. return
  78. }
  79. title = res.Data.Title
  80. link = fmt.Sprintf(_liveNoticeLink, oid)
  81. return
  82. }
  83. // LivePicture return link.
  84. func (d *Dao) LivePicture(c context.Context, oid int64) (title, link string, err error) {
  85. params := url.Values{}
  86. params.Set("doc_id", strconv.FormatInt(oid, 10))
  87. var res struct {
  88. Code int `json:"code"`
  89. Data *struct {
  90. Item *struct {
  91. Title string `json:"title"`
  92. Desc string `json:"description"`
  93. } `json:"item"`
  94. } `json:"data"`
  95. }
  96. if err = d.httpClient.Get(c, d.urlLivePicture, "", params, &res); err != nil {
  97. log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLivePicture, params.Encode(), err)
  98. return
  99. }
  100. if res.Code != 0 || res.Data == nil || res.Data.Item == nil {
  101. err = fmt.Errorf("url:%s?%s code:%d", d.urlLivePicture, params.Encode(), res.Code)
  102. return
  103. }
  104. title = res.Data.Item.Title
  105. if title == "" {
  106. title = res.Data.Item.Desc
  107. }
  108. link = fmt.Sprintf(_livePictureLink, oid)
  109. return
  110. }