dao.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package live
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "go-common/app/interface/main/app-interface/conf"
  9. "go-common/app/interface/main/app-interface/model/live"
  10. "go-common/library/ecode"
  11. httpx "go-common/library/net/http/blademaster"
  12. "go-common/library/net/metadata"
  13. "go-common/library/xstr"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. _live = "/AppRoom/getRoomInfo"
  18. _medalStatus = "/fans_medal/v1/medal/get_medal_opened"
  19. _appMRoom = "/room/v1/Room/rooms_for_app_index"
  20. _statusInfo = "/room/v1/Room/get_status_info_by_uids"
  21. _visibleInfo = "/rc/v1/Glory/get_visible"
  22. _usersInfo = "/user/v3/User/getMultiple"
  23. _LiveByRID = "/room/v2/Room/get_by_ids"
  24. )
  25. // Dao is space dao
  26. type Dao struct {
  27. client *httpx.Client
  28. live string
  29. medalStatus string
  30. appMRoom string
  31. statusInfo string
  32. visibleInfo string
  33. userInfo string
  34. liveByRID string
  35. }
  36. // New initial space dao
  37. func New(c *conf.Config) (d *Dao) {
  38. d = &Dao{
  39. client: httpx.NewClient(c.HTTPLive),
  40. live: c.Host.APILiveCo + _live,
  41. medalStatus: c.Host.APILiveCo + _medalStatus,
  42. appMRoom: c.Host.APILiveCo + _appMRoom,
  43. statusInfo: c.Host.APILiveCo + _statusInfo,
  44. visibleInfo: c.Host.APILiveCo + _visibleInfo,
  45. userInfo: c.Host.APILiveCo + _usersInfo,
  46. liveByRID: c.Host.APILiveCo + _LiveByRID,
  47. }
  48. return
  49. }
  50. // Live is space live data.
  51. func (d *Dao) Live(c context.Context, mid int64, platform string) (live json.RawMessage, err error) {
  52. ip := metadata.String(c, metadata.RemoteIP)
  53. params := url.Values{}
  54. params.Set("mid", strconv.FormatInt(mid, 10))
  55. params.Set("platform", platform)
  56. var res struct {
  57. Code int `json:"code"`
  58. Data json.RawMessage `json:"data"`
  59. }
  60. if err = d.client.Get(c, d.live, ip, params, &res); err != nil {
  61. return
  62. }
  63. if res.Code != ecode.OK.Code() {
  64. err = errors.Wrap(ecode.Int(res.Code), d.live+"?"+params.Encode())
  65. return
  66. }
  67. live = res.Data
  68. return
  69. }
  70. // MedalStatus for live
  71. func (d *Dao) MedalStatus(c context.Context, mid int64) (status int, err error) {
  72. var (
  73. req *http.Request
  74. res struct {
  75. Code int `json:"code"`
  76. Data *struct {
  77. MasterStatus int `json:"master_status"`
  78. } `json:"data"`
  79. }
  80. ip = metadata.String(c, metadata.RemoteIP)
  81. )
  82. if req, err = d.client.NewRequest("GET", d.medalStatus, ip, nil); err != nil {
  83. return
  84. }
  85. req.Header.Set("X-BILILIVE-UID", strconv.FormatInt(mid, 10))
  86. if err = d.client.Do(c, req, &res); err != nil {
  87. err = errors.Wrapf(err, "url(%s) header(X-BiliLive-UID:%s)", req.URL.String(), req.Header.Get("X-BiliLive-UID"))
  88. return
  89. }
  90. if res.Code != ecode.OK.Code() {
  91. err = errors.Wrapf(ecode.Int(res.Code), "url(%s) header(X-BiliLive-UID:%s)", req.URL.String(), req.Header.Get("X-BiliLive-UID"))
  92. return
  93. }
  94. if res.Data != nil {
  95. status = res.Data.MasterStatus
  96. }
  97. return
  98. }
  99. // AppMRoom for live
  100. func (d *Dao) AppMRoom(c context.Context, roomids []int64) (rs map[int64]*live.Room, err error) {
  101. ip := metadata.String(c, metadata.RemoteIP)
  102. params := url.Values{}
  103. params.Set("room_ids", xstr.JoinInts(roomids))
  104. var res struct {
  105. Code int `json:"code"`
  106. Data []*live.Room `json:"data"`
  107. }
  108. if err = d.client.Get(c, d.appMRoom, ip, params, &res); err != nil {
  109. return
  110. }
  111. if res.Code != ecode.OK.Code() {
  112. err = errors.Wrap(err, d.appMRoom+"?"+params.Encode())
  113. return
  114. }
  115. rs = make(map[int64]*live.Room, len(res.Data))
  116. for _, r := range res.Data {
  117. rs[r.RoomID] = r
  118. }
  119. return
  120. }
  121. // StatusInfo for live
  122. func (d *Dao) StatusInfo(c context.Context, mids []int64) (status map[int64]*live.Status, err error) {
  123. ip := metadata.String(c, metadata.RemoteIP)
  124. params := url.Values{}
  125. for _, mid := range mids {
  126. params.Add("uids[]", strconv.FormatInt(mid, 10))
  127. }
  128. params.Set("filter_offline", "1")
  129. var res struct {
  130. Code int `json:"code"`
  131. Data map[int64]*live.Status `json:"data"`
  132. }
  133. if err = d.client.Get(c, d.statusInfo, ip, params, &res); err != nil {
  134. return
  135. }
  136. if res.Code != ecode.OK.Code() {
  137. err = errors.Wrap(err, d.statusInfo+"?"+params.Encode())
  138. return
  139. }
  140. status = res.Data
  141. return
  142. }
  143. // Glory for live search
  144. func (d *Dao) Glory(c context.Context, uid int64) (glory []*live.Glory, err error) {
  145. ip := metadata.String(c, metadata.RemoteIP)
  146. params := url.Values{}
  147. params.Set("uid", strconv.FormatInt(uid, 10))
  148. var res struct {
  149. Code int `json:"code"`
  150. Data []*live.Glory `json:"data"`
  151. }
  152. if err = d.client.Get(c, d.visibleInfo, ip, params, &res); err != nil {
  153. return
  154. }
  155. if res.Code != ecode.OK.Code() {
  156. err = errors.Wrap(err, d.visibleInfo+"?"+params.Encode())
  157. return
  158. }
  159. glory = res.Data
  160. return
  161. }
  162. // UserInfo for live search
  163. func (d *Dao) UserInfo(c context.Context, uids []int64) (userInfo map[int64]map[string]*live.Exp, err error) {
  164. ip := metadata.String(c, metadata.RemoteIP)
  165. params := url.Values{}
  166. for _, uid := range uids {
  167. params.Set("uids[]", strconv.FormatInt(uid, 10))
  168. }
  169. params.Set("attributes[]", "exp")
  170. var res struct {
  171. Code int `json:"code"`
  172. Data map[int64]map[string]*live.Exp `json:"data"`
  173. }
  174. if err = d.client.Get(c, d.userInfo, ip, params, &res); err != nil {
  175. return
  176. }
  177. if res.Code != ecode.OK.Code() {
  178. err = errors.Wrap(err, d.userInfo+"?"+params.Encode())
  179. return
  180. }
  181. userInfo = res.Data
  182. return
  183. }
  184. // LiveByRIDs get live info by room_ids.
  185. func (d *Dao) LiveByRIDs(c context.Context, roomIDs []int64) (info map[int64]*live.RoomInfo, err error) {
  186. ip := metadata.String(c, metadata.RemoteIP)
  187. params := url.Values{}
  188. for _, id := range roomIDs {
  189. params.Add("ids[]", strconv.FormatInt(id, 10))
  190. }
  191. params.Add("fields[]", "roomid")
  192. params.Add("fields[]", "title")
  193. params.Add("fields[]", "cover")
  194. params.Add("fields[]", "user_cover")
  195. params.Add("fields[]", "uid")
  196. params.Add("fields[]", "uname")
  197. params.Add("fields[]", "area_v2_name")
  198. params.Add("fields[]", "live_status")
  199. params.Add("fields[]", "broadcast_type")
  200. params.Add("fields[]", "short_id")
  201. params.Add("need_broadcast_type", "1")
  202. var res struct {
  203. Code int `json:"code"`
  204. Data map[int64]*live.RoomInfo `json:"data"`
  205. }
  206. if err = d.client.Get(c, d.liveByRID, ip, params, &res); err != nil {
  207. return
  208. }
  209. if res.Code != ecode.OK.Code() {
  210. err = errors.Wrap(ecode.Int(res.Code), d.liveByRID+"?"+params.Encode())
  211. return
  212. }
  213. info = res.Data
  214. return
  215. }