123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package live
- import (
- "context"
- "net/http"
- "net/url"
- "strconv"
- "go-common/app/interface/main/app-card/model/card/live"
- "go-common/app/interface/main/app-feed/conf"
- model "go-common/app/interface/main/app-feed/model/live"
- "go-common/library/ecode"
- httpx "go-common/library/net/http/blademaster"
- "go-common/library/net/metadata"
- "go-common/library/xstr"
- "github.com/pkg/errors"
- )
- const (
- _appMRoom = "/room/v1/Room/rooms_for_app_index"
- _feedList = "/feed/v1/feed/getList"
- _card = "/room/v1/RoomRecommend/getInfoByCardId"
- )
- // Dao is show dao.
- type Dao struct {
- // http client
- client *httpx.Client
- clientAsyn *httpx.Client
- // live
- appMRoom string
- feedList string
- card string
- }
- // New new a bangumi dao.
- func New(c *conf.Config) (d *Dao) {
- d = &Dao{
- // http client
- client: httpx.NewClient(c.HTTPClient),
- clientAsyn: httpx.NewClient(c.HTTPClientAsyn),
- appMRoom: c.Host.LiveAPI + _appMRoom,
- feedList: c.Host.LiveAPI + _feedList,
- card: c.Host.LiveAPI + _card,
- }
- return
- }
- func (d *Dao) AppMRoom(c context.Context, roomids []int64) (rs map[int64]*live.Room, err error) {
- ip := metadata.String(c, metadata.RemoteIP)
- params := url.Values{}
- params.Set("room_ids", xstr.JoinInts(roomids))
- var res struct {
- Code int `json:"code"`
- Data []*live.Room `json:"data"`
- }
- if err = d.client.Get(c, d.appMRoom, ip, params, &res); err != nil {
- return
- }
- if res.Code != ecode.OK.Code() {
- err = errors.Wrap(err, d.appMRoom+"?"+params.Encode())
- return
- }
- rs = make(map[int64]*live.Room, len(res.Data))
- for _, r := range res.Data {
- rs[r.RoomID] = r
- }
- return
- }
- func (d *Dao) FeedList(c context.Context, mid int64, pn, ps int) (fs []*model.Feed, count int, err error) {
- var req *http.Request
- params := url.Values{}
- params.Set("page", strconv.Itoa(pn))
- params.Set("pagesize", strconv.Itoa(ps))
- if req, err = d.client.NewRequest("GET", d.feedList, "", params); err != nil {
- return
- }
- req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
- var res struct {
- Code int `json:"code"`
- Data struct {
- Rooms []*model.Feed `json:"rooms"`
- Count int `json:"count"`
- } `json:"data"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- return
- }
- if res.Code != ecode.OK.Code() {
- err = errors.Wrap(err, d.feedList+"?"+params.Encode())
- return
- }
- fs = res.Data.Rooms
- count = res.Data.Count
- return
- }
- func (d *Dao) Card(c context.Context) (csm map[int64][]*live.Card, err error) {
- var res struct {
- Code int `json:"code"`
- Data map[int64][]*live.Card `json:"data"`
- }
- if err = d.clientAsyn.Get(c, d.card, "", nil, &res); err != nil {
- return
- }
- if res.Code != ecode.OK.Code() {
- err = errors.Wrap(err, d.card)
- return
- }
- csm = res.Data
- return
- }
- func (d *Dao) Rooms(c context.Context, roomids []int64, ip string) (rs map[int64]*live.Room, err error) {
- params := url.Values{}
- params.Set("room_ids", xstr.JoinInts(roomids))
- var res struct {
- Code int `json:"code"`
- Data []*live.Room `json:"data"`
- }
- if err = d.clientAsyn.Get(c, d.appMRoom, ip, params, &res); err != nil {
- return
- }
- if res.Code != ecode.OK.Code() {
- err = errors.Wrap(err, d.appMRoom+"?"+params.Encode())
- return
- }
- rs = make(map[int64]*live.Room, len(res.Data))
- for _, r := range res.Data {
- rs[r.RoomID] = r
- }
- return
- }
|