dm.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package danmu
  2. import (
  3. "context"
  4. "math"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "go-common/app/interface/main/creative/conf"
  10. "go-common/app/interface/main/creative/dao/tool"
  11. "go-common/app/interface/main/creative/model/danmu"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. "go-common/library/xstr"
  15. )
  16. const (
  17. _dmSearchURI = "/x/internal/v2/dm/search"
  18. _dmEditURI = "/x/internal/v2/dm/edit/state"
  19. _dmTransferURI = "/x/internal/dm/up/transfer"
  20. _dmPoolURI = "/x/internal/v2/dm/edit/pool"
  21. _dmDistriURI = "/x/internal/v2/dm/distribution"
  22. _dmRecentURI = "/x/internal/v2/dm/recent"
  23. )
  24. // List fn
  25. func (d *Dao) List(c context.Context, cid, mid int64, page, size int, order, pool, midStr, ip string) (dmList *danmu.DmList, err error) {
  26. params := url.Values{}
  27. params.Set("type", "1")
  28. params.Set("oid", strconv.FormatInt(cid, 10))
  29. params.Set("mid", strconv.FormatInt(mid, 10))
  30. params.Set("pn", strconv.FormatInt(int64(page), 10))
  31. params.Set("ps", strconv.FormatInt(int64(size), 10))
  32. params.Set("order", order)
  33. params.Set("pool", pool)
  34. if len(midStr) > 0 {
  35. midss, _ := xstr.SplitInts(midStr)
  36. midsa := make([]int64, 0)
  37. for _, v := range midss {
  38. if v > 0 {
  39. midsa = append(midsa, v)
  40. }
  41. }
  42. if len(midsa) > 0 {
  43. params.Set("mids", xstr.JoinInts(midsa))
  44. }
  45. }
  46. var res struct {
  47. Code int `json:"code"`
  48. SearchDMResult *danmu.SearchDMResult `json:"data"`
  49. }
  50. dmList = &danmu.DmList{
  51. List: make([]*danmu.MemberDM, 0),
  52. }
  53. if err = d.client.Get(c, d.dmSearchURL, ip, params, &res); err != nil {
  54. log.Error("d.DmSearch Get(%s,%s,%s) err(%v)", d.dmSearchURL, ip, params.Encode(), err)
  55. err = ecode.CreativeDanmuErr
  56. return
  57. }
  58. if res.Code != 0 {
  59. err = ecode.Int(res.Code)
  60. log.Error("d.DmSearch Get(%s,%s,%s) err(%v)|code(%d)", d.dmSearchURL, ip, params.Encode(), err, res.Code)
  61. if err == ecode.NothingFound {
  62. log.Error("d.DmSearch Get NothingFound (%s,%s,%s) err(%v)|code(%d)", d.dmSearchURL, ip, params.Encode(), err, res.Code)
  63. err = nil
  64. }
  65. return
  66. }
  67. if res.SearchDMResult != nil {
  68. dmList.Page = res.SearchDMResult.Page.Num
  69. dmList.Size = res.SearchDMResult.Page.Size
  70. dmList.TotalItems = res.SearchDMResult.Page.Total
  71. dmList.TotalPages = int(math.Ceil(float64(res.SearchDMResult.Page.Total) / float64(res.SearchDMResult.Page.Size)))
  72. for _, v := range res.SearchDMResult.Result {
  73. list := &danmu.MemberDM{
  74. ID: v.ID,
  75. FontSize: v.FontSize,
  76. Color: v.Color,
  77. Mode: v.Mode,
  78. Msg: v.Msg,
  79. Oid: v.Oid,
  80. Mid: v.Mid,
  81. Playtime: float64(v.Progress) / 1000,
  82. Pool: v.Pool,
  83. Ctime: v.Ctime,
  84. Attrs: v.Attrs,
  85. }
  86. if d.isProtect(v.Attrs, 1) {
  87. list.State = 2
  88. }
  89. dmList.List = append(dmList.List, list)
  90. }
  91. }
  92. return
  93. }
  94. // Edit fn
  95. func (d *Dao) Edit(c context.Context, mid, cid int64, state int8, dmids []int64, ip string) (err error) {
  96. params := url.Values{}
  97. params.Set("mid", strconv.FormatInt(mid, 10))
  98. params.Set("oid", strconv.FormatInt(cid, 10))
  99. params.Set("type", "1")
  100. params.Set("state", strconv.FormatInt(int64(state), 10))
  101. params.Set("dmids", xstr.JoinInts(dmids))
  102. var res struct {
  103. Code int `json:"code"`
  104. }
  105. if err = d.client.Post(c, d.dmEditURL, ip, params, &res); err != nil {
  106. log.Error("d.DmEdit.Post(%s,%s,%s) err(%v)", d.dmEditURL, ip, params.Encode(), err)
  107. err = ecode.CreativeDanmuErr
  108. return
  109. }
  110. log.Info("d.DmEdit.Post res.Code (%s,%s,%s) err(%v)|code(%d)", d.dmEditURL, ip, params.Encode(), err, res.Code)
  111. if res.Code != 0 {
  112. err = ecode.Int(res.Code)
  113. log.Error("d.DmEdit.Post res.Code (%s,%s,%s) err(%v)|code(%d)", d.dmEditURL, ip, params.Encode(), err, res.Code)
  114. return
  115. }
  116. return
  117. }
  118. // Transfer fn
  119. func (d *Dao) Transfer(c context.Context, mid, fromCID, toCID int64, offset float64, ak, ck, ip string) (err error) {
  120. params := url.Values{}
  121. params.Set("from", strconv.FormatInt(fromCID, 10))
  122. params.Set("to", strconv.FormatInt(toCID, 10))
  123. params.Set("mid", strconv.FormatInt(mid, 10))
  124. params.Set("offset", strconv.FormatFloat(offset, 'f', 3, 64))
  125. params.Set("appkey", conf.Conf.App.Key)
  126. params.Set("appsecret", conf.Conf.App.Secret)
  127. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  128. var (
  129. query, _ = tool.Sign(params)
  130. url string
  131. )
  132. url = d.dmTransferURL + "?" + query
  133. var res struct {
  134. Code int `json:"code"`
  135. }
  136. req, err := http.NewRequest("POST", url, nil)
  137. if err != nil {
  138. log.Error("http.NewRequest(%s) error(%v)|mid(%d),fromCID(%d),toCID(%d),ak(%s),ck(%s),ip(%s)", url, err, mid, fromCID, toCID, ak, ck, ip)
  139. err = ecode.CreativeDanmuErr
  140. return
  141. }
  142. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  143. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  144. req.Header.Set("Cookie", ck)
  145. if err = d.client.Do(c, req, &res); err != nil {
  146. log.Error("d.client.Do(%s) error(%v)|mid(%d),fromCID(%d),toCID(%d),ak(%s),ck(%s),ip(%s)", url, err, mid, fromCID, toCID, ak, ck, ip)
  147. err = ecode.CreativeDanmuErr
  148. return
  149. }
  150. log.Info("dao Transfer(%s)mid(%d),fromCID(%d),toCID(%d),ak(%s),ck(%s),ip(%s)", url, mid, fromCID, toCID, ak, ck, ip)
  151. if res.Code != 0 {
  152. log.Error("dm Transfer(%s) res(%v); mid(%d), ip(%s), code(%d)", url, res, mid, ip, res.Code)
  153. err = ecode.Int(res.Code)
  154. return
  155. }
  156. return
  157. }
  158. // UpPool fn
  159. func (d *Dao) UpPool(c context.Context, mid, cid int64, dmids []int64, pool int8) (err error) {
  160. params := url.Values{}
  161. params.Set("mid", strconv.FormatInt(mid, 10))
  162. params.Set("oid", strconv.FormatInt(cid, 10))
  163. params.Set("type", "1")
  164. params.Set("pool", strconv.FormatInt(int64(pool), 10))
  165. params.Set("dmids", xstr.JoinInts(dmids))
  166. var res struct {
  167. Code int `json:"code"`
  168. }
  169. if err = d.client.Post(c, d.dmPoolURL, "", params, &res); err != nil {
  170. log.Error("d.Pool.Post(%s,%s,%s) err(%v)", d.dmPoolURL, "", params.Encode(), err)
  171. err = ecode.CreativeDanmuErr
  172. return
  173. }
  174. log.Info("d.Pool.Post res.Code (%s,%s,%s) err(%v)|code(%d)", d.dmPoolURL, "", params.Encode(), err, res.Code)
  175. if res.Code != 0 {
  176. err = ecode.Int(res.Code)
  177. log.Error("d.DmEdit.Post res.Code (%s,%s,%s) err(%v)|code(%d)", d.dmPoolURL, "", params.Encode(), err, res.Code)
  178. return
  179. }
  180. return
  181. }
  182. // Distri fn
  183. func (d *Dao) Distri(c context.Context, mid, cid int64, ip string) (distri map[int64]int64, err error) {
  184. params := url.Values{}
  185. params.Set("oid", strconv.FormatInt(cid, 10))
  186. params.Set("type", "1")
  187. params.Set("interval", "1")
  188. var res struct {
  189. Code int `json:"code"`
  190. Distri map[int64]int64 `json:"data"`
  191. }
  192. res.Distri = make(map[int64]int64)
  193. if err = d.client.Get(c, d.dmDistriURL, ip, params, &res); err != nil {
  194. log.Error("d.DmDistri.Get(%s,%s,%s) err(%v)", d.dmDistriURL, ip, params.Encode(), err)
  195. err = ecode.CreativeDanmuErr
  196. return
  197. }
  198. if res.Code != 0 {
  199. err = ecode.Int(res.Code)
  200. log.Error("d.DmDistri.Get(%s,%s,%s) err(%v)|code(%d)", d.dmDistriURL, ip, params.Encode(), err, res.Code)
  201. return
  202. }
  203. distri = res.Distri
  204. return
  205. }
  206. // Recent fn
  207. func (d *Dao) Recent(c context.Context, mid, pn, ps int64, ip string) (dmRecent *danmu.DmRecent, aids []int64, err error) {
  208. params := url.Values{}
  209. params.Set("mid", strconv.FormatInt(mid, 10))
  210. params.Set("pn", strconv.FormatInt(pn, 10))
  211. params.Set("ps", strconv.FormatInt(ps, 10))
  212. dmRecent = &danmu.DmRecent{
  213. List: make([]*danmu.Recent, 0),
  214. }
  215. aids = make([]int64, 0)
  216. var res struct {
  217. Code int `json:"code"`
  218. ResNewRecent *danmu.ResNewRecent `json:"data"`
  219. }
  220. if err = d.client.Get(c, d.dmRecentURL, ip, params, &res); err != nil {
  221. log.Error("d.DmRecent Get(%s,%s,%s) err(%v)", d.dmRecentURL, ip, params.Encode(), err)
  222. err = ecode.CreativeDanmuErr
  223. return
  224. }
  225. if res.Code != 0 {
  226. err = ecode.Int(res.Code)
  227. log.Error("d.DmRecent Get(%s,%s,%s) err(%v)|code(%d)", d.dmRecentURL, ip, params.Encode(), err, res.Code)
  228. return
  229. }
  230. if res.ResNewRecent != nil {
  231. dmRecent.Page = res.ResNewRecent.Page.Pn
  232. dmRecent.Size = res.ResNewRecent.Page.Ps
  233. dmRecent.TotalItems = res.ResNewRecent.Page.Total
  234. dmRecent.TotalPages = int(math.Ceil(float64(res.ResNewRecent.Page.Total) / float64(res.ResNewRecent.Page.Ps)))
  235. for _, v := range res.ResNewRecent.Result {
  236. list := &danmu.Recent{
  237. ID: v.ID,
  238. Aid: v.Aid,
  239. Type: v.Type,
  240. Oid: v.Oid,
  241. Mid: v.Mid,
  242. Msg: v.Msg,
  243. Playtime: float64(v.Progress) / 1000,
  244. FontSize: v.FontSize,
  245. Color: v.Color,
  246. Mode: v.Mode,
  247. Pool: v.Pool,
  248. Title: v.Title,
  249. Ctime: v.Ctime,
  250. Mtime: v.Ctime,
  251. Attrs: v.Attrs,
  252. }
  253. if d.isProtect(v.Attrs, 1) {
  254. list.State = 2
  255. }
  256. dmRecent.List = append(dmRecent.List, list)
  257. aids = append(aids, v.Aid)
  258. }
  259. }
  260. return
  261. }
  262. func (d *Dao) isProtect(attrs string, num int64) bool {
  263. if len(attrs) == 0 {
  264. return false
  265. }
  266. attrInts, _ := xstr.SplitInts(attrs)
  267. for _, v := range attrInts {
  268. if v == num {
  269. return true
  270. }
  271. }
  272. return false
  273. }