http_title.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. // api
  12. _apiNotice = "http://api.bilibili.co/x/internal/credit/publish/infos"
  13. _apiBan = "http://api.bilibili.co/x/internal/credit/blocked/infos"
  14. _apiCredit = "http://api.bilibili.co/x/internal/credit/blocked/cases"
  15. _apiLiveVideo = "http://api.vc.bilibili.co/clip/v1/video/detail"
  16. _apiLiveActivity = "http://api.live.bilibili.co/comment/v1/relation/get_by_id"
  17. _apiLiveNotice = "http://api.vc.bilibili.co/news/v1/notice/info"
  18. _apiLivePicture = "http://api.vc.bilibili.co/link_draw/v1/doc/detail"
  19. _apiActivitySub = "http://matsuri.bilibili.co//activity/subject/url"
  20. _apiTopic = "http://matsuri.bilibili.co/activity/page/one/%d"
  21. _apiTopics = "http://matsuri.bilibili.co/activity/pages"
  22. _apiDynamic = "http://api.vc.bilibili.co/dynamic_repost/v0/dynamic_repost/ftch_rp_cont?dynamic_ids[]=%d"
  23. // link
  24. _linkBan = "https://www.bilibili.com/blackroom/ban/%d"
  25. _linkNotice = "https://www.bilibili.com/blackroom/notice/%d"
  26. _linkCredit = "https://www.bilibili.com/judgement/case/%d"
  27. _linkLiveVideo = "http://vc.bilibili.com/video/%d"
  28. _linkLiveNotice = "http://link.bilibili.com/p/eden/news#/newsdetail?id=%d"
  29. _linkLivePicture = "http://h.bilibili.com/ywh/%d"
  30. _linkDynamic = "http://t.bilibili.com/%d"
  31. )
  32. type notice struct {
  33. Title string `json:"title"`
  34. }
  35. type ban struct {
  36. Title string `json:"punishTitle"`
  37. }
  38. type credit struct {
  39. Title string `json:"punishTitle"`
  40. }
  41. // NoticeTitle get blackromm notice info.
  42. func (d *Dao) NoticeTitle(c context.Context, oid int64) (title, link string, err error) {
  43. params := url.Values{}
  44. params.Set("ids", strconv.FormatInt(oid, 10))
  45. var res struct {
  46. Code int `json:"code"`
  47. Data map[int64]*notice `json:"data"`
  48. }
  49. if err = d.httpClient.Get(c, _apiNotice, "", params, &res); err != nil {
  50. log.Error("httpNotice(%s) error(%v)", _apiNotice, err)
  51. return
  52. }
  53. if r := res.Data[oid]; r != nil {
  54. title = r.Title
  55. }
  56. link = fmt.Sprintf(_linkNotice, oid)
  57. return
  58. }
  59. // BanTitle get ban info.
  60. func (d *Dao) BanTitle(c context.Context, oid int64) (title, link string, err error) {
  61. params := url.Values{}
  62. params.Set("ids", strconv.FormatInt(oid, 10))
  63. var res struct {
  64. Code int `json:"code"`
  65. Data map[int64]*ban `json:"data"`
  66. }
  67. if err = d.httpClient.Get(c, _apiBan, "", params, &res); err != nil {
  68. log.Error("httpBan(%s) error(%v)", _apiBan, err)
  69. return
  70. }
  71. if r := res.Data[oid]; r != nil {
  72. title = r.Title
  73. }
  74. link = fmt.Sprintf(_linkBan, oid)
  75. return
  76. }
  77. // CreditTitle return link.
  78. func (d *Dao) CreditTitle(c context.Context, oid int64) (title, link string, err error) {
  79. params := url.Values{}
  80. params.Set("ids", strconv.FormatInt(oid, 10))
  81. var res struct {
  82. Code int `json:"code"`
  83. Data map[int64]*credit `json:"data"`
  84. }
  85. if err = d.httpClient.Get(c, _apiCredit, "", params, &res); err != nil {
  86. log.Error("d.httpClient.Get(%s?%s) error(%v)", _apiCredit, params.Encode(), err)
  87. return
  88. }
  89. if res.Code != 0 || res.Data == nil {
  90. err = fmt.Errorf("url:%s?%s code:%d", _apiCredit, params.Encode(), res.Code)
  91. return
  92. }
  93. if r := res.Data[oid]; r != nil {
  94. title = r.Title
  95. }
  96. link = fmt.Sprintf(_linkCredit, oid)
  97. return
  98. }
  99. // LiveVideoTitle get live video title.
  100. func (d *Dao) LiveVideoTitle(c context.Context, oid int64) (title, link string, err error) {
  101. params := url.Values{}
  102. params.Set("video_id", strconv.FormatInt(oid, 10))
  103. var res struct {
  104. Code int `json:"code"`
  105. Data *struct {
  106. Item *struct {
  107. Description string `json:"description"`
  108. } `json:"item"`
  109. } `json:"data"`
  110. }
  111. if err = d.httpClient.Get(c, _apiLiveVideo, "", params, &res); err != nil {
  112. log.Error("httpLiveVideoTitle(%s?%s) error(%v)", _apiLiveVideo, params.Encode(), err)
  113. return
  114. }
  115. if res.Code != 0 || res.Data == nil || res.Data.Item == nil {
  116. err = fmt.Errorf("url:%s?%s code:%d", _apiLiveVideo, params.Encode(), res.Code)
  117. return
  118. }
  119. title = res.Data.Item.Description
  120. link = fmt.Sprintf(_linkLiveVideo, oid)
  121. return
  122. }
  123. // LiveActivityTitle get live activity info.
  124. func (d *Dao) LiveActivityTitle(c context.Context, oid int64) (title, link string, err error) {
  125. params := url.Values{}
  126. params.Set("id", strconv.FormatInt(oid, 10))
  127. var res struct {
  128. Code int `json:"code"`
  129. Data *struct {
  130. Name string `json:"name"`
  131. URL string `json:"url"`
  132. } `json:"data"`
  133. }
  134. if err = d.httpClient.Get(c, _apiLiveActivity, "", params, &res); err != nil {
  135. log.Error("httpLiveActivityTitle(%s?%s) error(%v)", _apiLiveActivity, params.Encode(), err)
  136. return
  137. }
  138. if res.Code != 0 || res.Data == nil {
  139. err = fmt.Errorf("url:%s?%s code:%d", _apiLiveActivity, params.Encode(), res.Code)
  140. return
  141. }
  142. title = res.Data.Name
  143. link = res.Data.URL
  144. return
  145. }
  146. // LiveNoticeTitle get live notice info.
  147. func (d *Dao) LiveNoticeTitle(c context.Context, oid int64) (title, link string, err error) {
  148. params := url.Values{}
  149. params.Set("id", strconv.FormatInt(oid, 10))
  150. var res struct {
  151. Code int `json:"code"`
  152. Data *struct {
  153. Title string `json:"title"`
  154. } `json:"data"`
  155. }
  156. if err = d.httpClient.Get(c, _apiLiveNotice, "", params, &res); err != nil {
  157. log.Error("LiveNoticeTitle(%s?%s) error(%v)", _apiLiveNotice, params.Encode(), err)
  158. return
  159. }
  160. if res.Code != 0 || res.Data == nil {
  161. err = fmt.Errorf("url:%s?%s code:%d", _apiLiveNotice, params.Encode(), res.Code)
  162. return
  163. }
  164. title = res.Data.Title
  165. link = fmt.Sprintf(_linkLiveNotice, oid)
  166. return
  167. }
  168. // LivePictureTitle get live picture info.
  169. func (d *Dao) LivePictureTitle(c context.Context, oid int64) (title, link string, err error) {
  170. params := url.Values{}
  171. params.Set("doc_id", strconv.FormatInt(oid, 10))
  172. var res struct {
  173. Code int `json:"code"`
  174. Data *struct {
  175. Item *struct {
  176. Title string `json:"title"`
  177. Desc string `json:"description"`
  178. } `json:"item"`
  179. } `json:"data"`
  180. }
  181. if err = d.httpClient.Get(c, _apiLivePicture, "", params, &res); err != nil {
  182. log.Error("LivePictureTitle(%s?%s) error(%v)", _apiLivePicture, params.Encode(), err)
  183. return
  184. }
  185. if res.Code != 0 || res.Data == nil || res.Data.Item == nil {
  186. err = fmt.Errorf("url:%s?%s code:%d", _apiLivePicture, params.Encode(), res.Code)
  187. return
  188. }
  189. title = res.Data.Item.Title
  190. if title == "" {
  191. title = res.Data.Item.Desc
  192. }
  193. link = fmt.Sprintf(_linkLivePicture, oid)
  194. return
  195. }
  196. // TopicTitle get topic info.
  197. func (d *Dao) TopicTitle(c context.Context, oid int64) (title, link string, err error) {
  198. var res struct {
  199. Code int `json:"code"`
  200. Data *struct {
  201. Title string `json:"name"`
  202. PCLink string `json:"pc_url"`
  203. H5Link string `json:"h5_url"`
  204. } `json:"data"`
  205. }
  206. if err = d.httpClient.Get(c, fmt.Sprintf(_apiTopic, oid), "", nil, &res); err != nil {
  207. log.Error("TopicTitle(%s) error(%v)", fmt.Sprintf(_apiTopic, oid), err)
  208. return
  209. }
  210. if res.Data == nil {
  211. err = fmt.Errorf("url:%s code:%d", fmt.Sprintf(_apiTopic, oid), res.Code)
  212. return
  213. }
  214. title = res.Data.Title
  215. link = res.Data.PCLink
  216. if link == "" {
  217. link = res.Data.H5Link
  218. }
  219. return
  220. }
  221. // TopicsLink get topic info.
  222. func (d *Dao) TopicsLink(c context.Context, links map[int64]string, isTopic bool) (err error) {
  223. if len(links) == 0 {
  224. return
  225. }
  226. var res struct {
  227. Code int `json:"code"`
  228. Data *struct {
  229. List []struct {
  230. ID int64 `json:"id"`
  231. PCURL string `json:"pc_url"`
  232. H5URL string `json:"h5_url"`
  233. } `json:"list"`
  234. } `json:"data"`
  235. }
  236. var ids []int64
  237. for oid := range links {
  238. ids = append(ids, oid)
  239. }
  240. params := url.Values{}
  241. params.Set("pids", xstr.JoinInts(ids))
  242. params.Set("all", "isOne")
  243. if isTopic {
  244. params.Set("mold", "1")
  245. }
  246. if err = d.httpClient.Get(c, _apiTopics, "", params, &res); err != nil {
  247. log.Error("TopicsTitle(%s) error(%v)", _apiTopics, err)
  248. return
  249. }
  250. if res.Data == nil {
  251. err = fmt.Errorf("url:%s code:%d", _apiTopics, res.Code)
  252. return
  253. }
  254. for _, data := range res.Data.List {
  255. if data.PCURL != "" {
  256. links[data.ID] = data.PCURL
  257. } else {
  258. links[data.ID] = data.H5URL
  259. }
  260. }
  261. return
  262. }
  263. // ActivitySub return activity sub link.
  264. func (d *Dao) ActivitySub(c context.Context, oid int64) (title, link string, err error) {
  265. params := url.Values{}
  266. params.Set("oid", strconv.FormatInt(oid, 10))
  267. var res struct {
  268. Code int `json:"code"`
  269. Data *struct {
  270. Title string `json:"name"`
  271. Link string `json:"act_url"`
  272. } `json:"data"`
  273. }
  274. if err = d.httpClient.Get(c, _apiActivitySub, "", params, &res); err != nil {
  275. log.Error("d.httpClient.Get(%s?%s) error(%v)", _apiActivitySub, params.Encode(), err)
  276. return
  277. }
  278. if res.Data == nil {
  279. err = fmt.Errorf("url:%s code:%d", _apiActivitySub, res.Code)
  280. return
  281. }
  282. title = res.Data.Title
  283. link = res.Data.Link
  284. return
  285. }
  286. // DynamicTitle return link and title.
  287. func (d *Dao) DynamicTitle(c context.Context, oid int64) (title, link string, err error) {
  288. params := url.Values{}
  289. uri := fmt.Sprintf(_apiDynamic, oid)
  290. var res struct {
  291. Code int `json:"code"`
  292. Data *struct {
  293. Pairs []struct {
  294. DynamicID int64 `json:"dynamic_id"`
  295. Content string `json:"rp_cont"`
  296. Type int32 `json:"type"`
  297. } `json:"pairs"`
  298. TotalCount int64 `json:"total_count"`
  299. } `json:"data,omitempty"`
  300. Message string `json:"message"`
  301. }
  302. if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
  303. log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
  304. return
  305. }
  306. if res.Code != 0 || res.Data == nil || len(res.Data.Pairs) == 0 {
  307. err = fmt.Errorf("get dynamic failed!url:%s?%s code:%d message:%s pairs:%v", uri, params.Encode(), res.Code, res.Message, res.Data.Pairs)
  308. return
  309. }
  310. title = res.Data.Pairs[0].Content
  311. link = fmt.Sprintf(_linkDynamic, oid)
  312. return
  313. }