api.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package bplus
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/app-interface/model/bplus"
  8. "go-common/library/ecode"
  9. "go-common/library/net/metadata"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _favorPlus = "/user_ex/v1/Fav/getFavList"
  15. _clips = "/clip/v1/video/blist"
  16. _allbums = "/link_draw/v1/Doc/photo_list_ones"
  17. _allClip = "/clip_ext/v1/video/all"
  18. _allAlbum = "/link_draw/v1/Doc/photo_all_ones"
  19. _clipDetail = "/clip_ext/v0/video/getDetailForSpace"
  20. _albumDetail = "/link_draw_ex/v0/Doc/details"
  21. _groupsCount = "/link_group/v1/member/created_groups_num"
  22. _dynamic = "/dynamic_svr/v0/dynamic_svr/space_intro"
  23. _dunamicCount = "/dynamic_svr/v0/dynamic_svr/space_dy_num"
  24. _dynamicDetail = "/dynamic_detail/v0/Dynamic/details"
  25. )
  26. // DynamicCount return dynamic count
  27. func (d *Dao) DynamicCount(c context.Context, mid int64) (count int64, err error) {
  28. params := url.Values{}
  29. params.Set("uids", strconv.FormatInt(mid, 10))
  30. var res struct {
  31. Code int `json:"code"`
  32. Data struct {
  33. Items []struct {
  34. Mid int64 `json:"uid"`
  35. Num int64 `json:"num"`
  36. } `json:"items"`
  37. } `json:"data"`
  38. }
  39. if err = d.client.Get(c, d.dynamicCount, "", params, &res); err != nil {
  40. return
  41. }
  42. if res.Code != ecode.OK.Code() {
  43. err = errors.Wrap(ecode.Int(res.Code), d.dynamicCount+"?"+params.Encode())
  44. return
  45. }
  46. if len(res.Data.Items) == 0 {
  47. return
  48. }
  49. for _, item := range res.Data.Items {
  50. if item.Mid != mid {
  51. continue
  52. }
  53. count = item.Num
  54. break
  55. }
  56. return
  57. }
  58. // FavClips get fav from B+ api.
  59. func (d *Dao) FavClips(c context.Context, mid int64, accessKey, actionKey, device, mobiApp, platform string, build, pn, ps int) (cs *bplus.Clips, err error) {
  60. ip := metadata.String(c, metadata.RemoteIP)
  61. params := url.Values{}
  62. params.Set("uid", strconv.FormatInt(mid, 10))
  63. params.Set("access_key", accessKey)
  64. params.Set("actionKey", actionKey)
  65. params.Set("build", strconv.Itoa(build))
  66. params.Set("device", device)
  67. params.Set("mobi_app", mobiApp)
  68. params.Set("platform", platform)
  69. params.Set("biz_type", strconv.Itoa(bplus.CLIPS))
  70. params.Set("page", strconv.Itoa(pn))
  71. params.Set("pagesize", strconv.Itoa(ps))
  72. var res struct {
  73. Code int `json:"code"`
  74. Msg string `json:"msg"`
  75. Message string `json:"message"`
  76. Data *bplus.Clips `json:"data"`
  77. }
  78. if err = d.client.Get(c, d.favorPlus, ip, params, &res); err != nil {
  79. return
  80. }
  81. if res.Code != ecode.OK.Code() {
  82. err = errors.Wrap(ecode.Int(res.Code), d.favorPlus+"?"+params.Encode())
  83. return
  84. }
  85. cs = res.Data
  86. return
  87. }
  88. // FavAlbums get fav from B+ api.
  89. func (d *Dao) FavAlbums(c context.Context, mid int64, accessKey, actionKey, device, mobiApp, platform string, build, pn, ps int) (as *bplus.Albums, err error) {
  90. ip := metadata.String(c, metadata.RemoteIP)
  91. params := url.Values{}
  92. params.Set("uid", strconv.FormatInt(mid, 10))
  93. params.Set("access_key", accessKey)
  94. params.Set("actionKey", actionKey)
  95. params.Set("build", strconv.Itoa(build))
  96. params.Set("device", device)
  97. params.Set("mobi_app", mobiApp)
  98. params.Set("platform", platform)
  99. params.Set("biz_type", strconv.Itoa(bplus.ALBUMS))
  100. params.Set("page", strconv.Itoa(pn))
  101. params.Set("pagesize", strconv.Itoa(ps))
  102. var res struct {
  103. Code int `json:"code"`
  104. Msg string `json:"msg"`
  105. Message string `json:"message"`
  106. Data *bplus.Albums `json:"data"`
  107. }
  108. if err = d.client.Get(c, d.favorPlus, ip, params, &res); err != nil {
  109. return
  110. }
  111. if res.Code != ecode.OK.Code() {
  112. err = errors.Wrap(ecode.Int(res.Code), d.favorPlus+"?"+params.Encode())
  113. return
  114. }
  115. as = res.Data
  116. return
  117. }
  118. // Clips .
  119. func (d *Dao) Clips(c context.Context, vmid int64, pos, size int) (cs []*bplus.Clip, more, offset int, err error) {
  120. ip := metadata.String(c, metadata.RemoteIP)
  121. params := url.Values{}
  122. params.Set("uid", strconv.FormatInt(vmid, 10))
  123. params.Set("next_offset", strconv.Itoa(pos))
  124. params.Set("page_size", strconv.Itoa(size))
  125. var res struct {
  126. Code int `json:"code"`
  127. Data *struct {
  128. More int `json:"has_more"`
  129. Offset int `json:"next_offset"`
  130. Item []*bplus.Clip `json:"items"`
  131. }
  132. }
  133. if err = d.client.Get(c, d.clips, ip, params, &res); err != nil {
  134. return
  135. }
  136. if res.Code != ecode.OK.Code() {
  137. err = errors.Wrap(ecode.Int(res.Code), d.clips+"?"+params.Encode())
  138. return
  139. }
  140. if res.Data != nil {
  141. cs = res.Data.Item
  142. more = res.Data.More
  143. offset = res.Data.Offset
  144. }
  145. return
  146. }
  147. // Albums get album list form api .
  148. func (d *Dao) Albums(c context.Context, vmid int64, pos, size int) (as []*bplus.Album, more, offset int, err error) {
  149. ip := metadata.String(c, metadata.RemoteIP)
  150. params := url.Values{}
  151. params.Set("uid", strconv.FormatInt(vmid, 10))
  152. params.Set("next_offset", strconv.Itoa(pos))
  153. params.Set("page_size", strconv.Itoa(size))
  154. var res struct {
  155. Code int `json:"code"`
  156. Data *struct {
  157. More int `json:"has_more"`
  158. Offset int `json:"next_offset"`
  159. Item []*bplus.Album `json:"items"`
  160. }
  161. }
  162. if err = d.client.Get(c, d.albums, ip, params, &res); err != nil {
  163. return
  164. }
  165. if res.Code != ecode.OK.Code() {
  166. err = errors.Wrap(ecode.Int(res.Code), d.albums+"?"+params.Encode())
  167. return
  168. }
  169. if res.Data != nil {
  170. as = res.Data.Item
  171. more = res.Data.More
  172. offset = res.Data.Offset
  173. }
  174. return
  175. }
  176. // AllClip .
  177. func (d *Dao) AllClip(c context.Context, vmid int64, size int) (cs []*bplus.Clip, count int, err error) {
  178. ip := metadata.String(c, metadata.RemoteIP)
  179. params := url.Values{}
  180. params.Set("uid", strconv.FormatInt(vmid, 10))
  181. params.Set("page_size", strconv.Itoa(size))
  182. var res struct {
  183. Code int `json:"code"`
  184. Data *struct {
  185. Count int `json:"total_count"`
  186. Item []*bplus.Clip `json:"items"`
  187. }
  188. }
  189. if err = d.client.Get(c, d.allClip, ip, params, &res); err != nil {
  190. return
  191. }
  192. if res.Code != ecode.OK.Code() {
  193. err = errors.Wrap(ecode.Int(res.Code), d.allClip+"?"+params.Encode())
  194. return
  195. }
  196. if res.Data != nil {
  197. cs = res.Data.Item
  198. count = res.Data.Count
  199. }
  200. return
  201. }
  202. // AllAlbum .
  203. func (d *Dao) AllAlbum(c context.Context, vmid int64, size int) (as []*bplus.Album, count int, err error) {
  204. ip := metadata.String(c, metadata.RemoteIP)
  205. params := url.Values{}
  206. params.Set("uid", strconv.FormatInt(vmid, 10))
  207. params.Set("page_size", strconv.Itoa(size))
  208. var res struct {
  209. Code int `json:"code"`
  210. Data *struct {
  211. Count int `json:"total_count"`
  212. Item []*bplus.Album `json:"items"`
  213. }
  214. }
  215. if err = d.client.Get(c, d.allAlbum, ip, params, &res); err != nil {
  216. return
  217. }
  218. if res.Code != ecode.OK.Code() {
  219. err = errors.Wrap(ecode.Int(res.Code), d.allAlbum+"?"+params.Encode())
  220. return
  221. }
  222. if res.Data != nil {
  223. as = res.Data.Item
  224. count = res.Data.Count
  225. }
  226. return
  227. }
  228. // ClipDetail .
  229. func (d *Dao) ClipDetail(c context.Context, ids []int64) (cs map[int64]*bplus.Clip, err error) {
  230. ip := metadata.String(c, metadata.RemoteIP)
  231. params := url.Values{}
  232. params.Set("ids", xstr.JoinInts(ids))
  233. var res struct {
  234. Code int `json:"code"`
  235. Data *struct {
  236. Item []*bplus.Clip `json:"items"`
  237. }
  238. }
  239. if err = d.client.Get(c, d.clipDetail, ip, params, &res); err != nil {
  240. return
  241. }
  242. if res.Code != ecode.OK.Code() {
  243. err = errors.Wrap(ecode.Int(res.Code), d.clipDetail+"?"+params.Encode())
  244. return
  245. }
  246. if res.Data != nil {
  247. cs = make(map[int64]*bplus.Clip, len(res.Data.Item))
  248. for _, clip := range res.Data.Item {
  249. cs[clip.ID] = clip
  250. }
  251. }
  252. return
  253. }
  254. // AlbumDetail .
  255. func (d *Dao) AlbumDetail(c context.Context, vmid int64, ids []int64) (as map[int64]*bplus.Album, err error) {
  256. ip := metadata.String(c, metadata.RemoteIP)
  257. params := url.Values{}
  258. params.Set("uid", strconv.FormatInt(vmid, 10))
  259. params.Set("ids", xstr.JoinInts(ids))
  260. var res struct {
  261. Code int `json:"code"`
  262. Data *struct {
  263. Item []*bplus.Album `json:"items"`
  264. }
  265. }
  266. if err = d.client.Get(c, d.albumDetail, ip, params, &res); err != nil {
  267. return
  268. }
  269. if res.Code != ecode.OK.Code() {
  270. err = errors.Wrap(ecode.Int(res.Code), d.albumDetail+"?"+params.Encode())
  271. return
  272. }
  273. if res.Data != nil {
  274. as = make(map[int64]*bplus.Album, len(res.Data.Item))
  275. for _, album := range res.Data.Item {
  276. as[album.ID] = album
  277. }
  278. }
  279. return
  280. }
  281. // GroupsCount .
  282. func (d *Dao) GroupsCount(c context.Context, mid, vmid int64) (count int, err error) {
  283. var (
  284. req *http.Request
  285. ip = metadata.String(c, metadata.RemoteIP)
  286. )
  287. params := url.Values{}
  288. params.Set("master_uid", strconv.FormatInt(vmid, 10))
  289. if req, err = d.client.NewRequest(http.MethodGet, d.groupsCount, ip, params); err != nil {
  290. return
  291. }
  292. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  293. var res struct {
  294. Code int `json:"code"`
  295. Data *struct {
  296. Num int `json:"num"`
  297. }
  298. }
  299. if err = d.client.Do(c, req, &res); err != nil {
  300. err = errors.Wrapf(err, "url(%s) header(X-BiliLive-UID:%s)", req.URL.String(), req.Header.Get("X-BiliLive-UID"))
  301. return
  302. }
  303. if res.Code != ecode.OK.Code() {
  304. err = errors.Wrapf(ecode.Int(res.Code), "url(%s) header(X-BiliLive-UID:%s)", req.URL.String(), req.Header.Get("X-BiliLive-UID"))
  305. return
  306. }
  307. if res.Data != nil {
  308. count = res.Data.Num
  309. }
  310. return
  311. }
  312. // Dynamic .
  313. func (d *Dao) Dynamic(c context.Context, uid int64) (has bool, err error) {
  314. ip := metadata.String(c, metadata.RemoteIP)
  315. params := url.Values{}
  316. params.Set("uid", strconv.FormatInt(uid, 10))
  317. var res struct {
  318. Code int `json:"code"`
  319. Data *struct {
  320. Reuslt int `json:"result"`
  321. }
  322. }
  323. if err = d.client.Get(c, d.dynamic, ip, params, &res); err != nil {
  324. return
  325. }
  326. if res.Code != ecode.OK.Code() {
  327. err = errors.Wrap(ecode.Int(res.Code), d.dynamic+"?"+params.Encode())
  328. return
  329. }
  330. if res.Data != nil && res.Data.Reuslt == 1 {
  331. has = true
  332. }
  333. return
  334. }
  335. // DynamicDetails get dynamic details by ids.
  336. func (d *Dao) DynamicDetails(c context.Context, ids []int64, from string) (details map[int64]*bplus.Detail, err error) {
  337. ip := metadata.String(c, metadata.RemoteIP)
  338. params := url.Values{}
  339. params.Set("from", from)
  340. for _, id := range ids {
  341. params.Add("dynamic_ids[]", strconv.FormatInt(id, 10))
  342. }
  343. var res struct {
  344. Code int `json:"code"`
  345. Data *struct {
  346. List []*bplus.Detail `json:"list"`
  347. } `json:"data"`
  348. }
  349. details = make(map[int64]*bplus.Detail)
  350. if err = d.client.Get(c, d.dynamicDetail, ip, params, &res); err != nil {
  351. return
  352. }
  353. if res.Code != ecode.OK.Code() {
  354. err = errors.Wrap(ecode.Int(res.Code), d.dynamicDetail+"?"+params.Encode())
  355. return
  356. }
  357. if res.Data != nil {
  358. for _, detail := range res.Data.List {
  359. if detail.ID != 0 {
  360. details[detail.ID] = detail
  361. }
  362. }
  363. }
  364. return
  365. }