dao.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package activity
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/job/main/videoup/conf"
  7. "go-common/app/job/main/videoup/model/archive"
  8. "go-common/library/log"
  9. xhttp "go-common/library/net/http/blademaster"
  10. )
  11. // Dao is message dao.
  12. type Dao struct {
  13. c *conf.Config
  14. client *xhttp.Client
  15. AddUri string
  16. UpUri string
  17. }
  18. // New new a activity dao.
  19. func New(c *conf.Config) (d *Dao) {
  20. // http://act.bilibili.com/api/likes/video/add
  21. d = &Dao{
  22. c: c,
  23. client: xhttp.NewClient(c.HTTPClient),
  24. AddUri: c.Host.Act + "/api/likes/video/add/",
  25. UpUri: c.Host.Act + "/api/likes/upbyaid/",
  26. }
  27. return
  28. }
  29. // AddVideo add video to activity.
  30. func (d *Dao) AddVideo(c context.Context, a *archive.Archive, missionID int64) (err error) {
  31. params := url.Values{}
  32. params.Set("aid", strconv.FormatInt(a.Aid, 10))
  33. params.Set("mid", strconv.FormatInt(a.Mid, 10))
  34. params.Set("message", a.Title)
  35. params.Set("image", a.Cover)
  36. params.Set("type", strconv.FormatInt(int64(a.TypeID), 10))
  37. var res struct {
  38. Code int `json:"code"`
  39. Msg string `json:"msg"`
  40. }
  41. if err = d.client.Post(c, d.AddUri+strconv.FormatInt(missionID, 10), "", params, &res); err != nil {
  42. log.Error("d.client.Post error(%v)", err)
  43. return
  44. }
  45. if res.Code != 0 {
  46. log.Error("url(%s) res code(%d) or res.result(%v)", d.AddUri+strconv.FormatInt(missionID, 10)+"?"+params.Encode(), res.Code, res.Msg)
  47. }
  48. return
  49. }
  50. // UpVideo update video to activity.
  51. func (d *Dao) UpVideo(c context.Context, a *archive.Archive, missionID int64) (err error) {
  52. params := url.Values{}
  53. params.Set("aid", strconv.FormatInt(a.Aid, 10))
  54. params.Set("mission_id", strconv.FormatInt(missionID, 10))
  55. params.Set("state", "-1")
  56. var res struct {
  57. Code int `json:"code"`
  58. Msg string `json:"msg"`
  59. }
  60. if err = d.client.Post(c, d.UpUri+strconv.FormatInt(missionID, 10), "", params, &res); err != nil {
  61. log.Error("d.client.Post error(%v)", err)
  62. return
  63. }
  64. if res.Code != 0 {
  65. log.Error("url(%s) res code(%d) or res.result(%v)", d.UpUri+strconv.FormatInt(missionID, 10)+"?"+params.Encode(), res.Code, res.Msg)
  66. }
  67. return
  68. }