api.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package data
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "strings"
  7. "go-common/app/interface/main/creative/model/data"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _statURL = "/data/member/upinfo/%d.json"
  13. _tagURL = "/tag_predict"
  14. _tagv2URL = "/tag_predict/v2"
  15. _coverURL = "/cover_recomm"
  16. )
  17. // Tags get predict tag.
  18. func (d *Dao) Tags(c context.Context, mid int64, tid uint16, title, filename, desc, cover string) (t *data.Tags, err error) {
  19. params := url.Values{}
  20. params.Set("typeid", strconv.Itoa(int(tid)))
  21. params.Set("title", title)
  22. params.Set("filename", filename)
  23. params.Set("desc", desc)
  24. params.Set("cover", cover)
  25. params.Set("mid", strconv.FormatInt(mid, 10))
  26. var res struct {
  27. Code int `json:"code"`
  28. Data *data.Tags `json:"data"`
  29. }
  30. if err = d.client.Get(c, d.tagURI, "", params, &res); err != nil {
  31. log.Error("data url(%s) error(%v)", d.tagURI+"?"+params.Encode(), err)
  32. err = ecode.CreativeDataErr
  33. return
  34. }
  35. if res.Code != 0 {
  36. log.Error("data url(%s) res(%v)", d.tagURI+"?"+params.Encode(), res)
  37. err = ecode.CreativeDataErr
  38. return
  39. }
  40. t = res.Data
  41. return
  42. }
  43. // TagsWithChecked get predict tag with checked mark.
  44. func (d *Dao) TagsWithChecked(c context.Context, mid int64, tid uint16, title, filename, desc, cover string, tagFrom int8) (t []*data.CheckedTag, err error) {
  45. params := url.Values{}
  46. t = make([]*data.CheckedTag, 0)
  47. params.Set("client_type", strconv.Itoa(int(tagFrom)))
  48. params.Set("typeid", strconv.Itoa(int(tid)))
  49. params.Set("title", title)
  50. params.Set("filename", filename)
  51. params.Set("desc", desc)
  52. params.Set("cover", cover)
  53. params.Set("mid", strconv.FormatInt(mid, 10))
  54. var res struct {
  55. Code int `json:"code"`
  56. Data struct {
  57. Tags []*data.CheckedTag `json:"tags"`
  58. } `json:"data"`
  59. }
  60. log.Info("TagsWithChecked url(%s)", d.tagV2URI+"?"+params.Encode())
  61. if err = d.client.Get(c, d.tagV2URI, "", params, &res); err != nil {
  62. log.Error("data url(%s) error(%v)", d.tagV2URI+"?"+params.Encode(), err)
  63. err = ecode.CreativeDataErr
  64. return
  65. }
  66. if res.Code != 0 {
  67. log.Error("data url(%s) res(%v)", d.tagV2URI+"?"+params.Encode(), res)
  68. err = ecode.CreativeDataErr
  69. return
  70. }
  71. t = res.Data.Tags
  72. return
  73. }
  74. // RecommendCovers get recommend covers from AI.
  75. func (d *Dao) RecommendCovers(c context.Context, mid int64, fns []string) (cvs []string, err error) {
  76. params := url.Values{}
  77. params.Set("mid", strconv.FormatInt(mid, 10))
  78. params.Set("filename", strings.Join(fns, ","))
  79. var res struct {
  80. Code int `json:"code"`
  81. Data []string `json:"data"`
  82. }
  83. if err = d.client.Get(c, d.coverBFSURI, "", params, &res); err != nil {
  84. log.Error("Covers url(%s) error(%v)", d.coverBFSURI+"?"+params.Encode(), err)
  85. err = ecode.CreativeDataErr
  86. return
  87. }
  88. if res.Code != 0 {
  89. log.Error("Covers url(%s) res(%v)", d.coverBFSURI+"?"+params.Encode(), res)
  90. err = ecode.CreativeDataErr
  91. return
  92. }
  93. cvs = res.Data
  94. return
  95. }