dao.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package search
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. "go-common/app/interface/main/app-intl/conf"
  10. arcdao "go-common/app/interface/main/app-intl/dao/archive"
  11. bgmdao "go-common/app/interface/main/app-intl/dao/bangumi"
  12. "go-common/app/interface/main/app-intl/model"
  13. "go-common/app/interface/main/app-intl/model/bangumi"
  14. "go-common/app/interface/main/app-intl/model/search"
  15. "go-common/app/service/main/archive/api"
  16. "go-common/library/ecode"
  17. "go-common/library/log"
  18. httpx "go-common/library/net/http/blademaster"
  19. "go-common/library/net/metadata"
  20. "go-common/library/sync/errgroup"
  21. "github.com/pkg/errors"
  22. )
  23. const (
  24. _main = "/main/search"
  25. _suggest3 = "/main/suggest/new"
  26. )
  27. // Dao is search dao
  28. type Dao struct {
  29. client *httpx.Client
  30. arcDao *arcdao.Dao
  31. bgmDao *bgmdao.Dao
  32. main string
  33. suggest3 string
  34. }
  35. // New initial search dao
  36. func New(c *conf.Config) (d *Dao) {
  37. d = &Dao{
  38. client: httpx.NewClient(c.HTTPSearch),
  39. arcDao: arcdao.New(c),
  40. bgmDao: bgmdao.New(c),
  41. main: c.Host.Search + _main,
  42. suggest3: c.Host.Search + _suggest3,
  43. }
  44. return
  45. }
  46. // Search app all search .
  47. func (d *Dao) Search(c context.Context, mid, zoneid int64, mobiApp, device, platform, buvid, keyword, duration, order, filtered, fromSource, recommend string, plat int8, seasonNum, movieNum, upUserNum, uvLimit, userNum, userVideoLimit, biliUserNum, biliUserVideoLimit, rid, highlight, build, pn, ps int, now time.Time) (res *search.Search, code int, err error) {
  48. var (
  49. req *http.Request
  50. ip = metadata.String(c, metadata.RemoteIP)
  51. )
  52. res = &search.Search{}
  53. params := url.Values{}
  54. params.Set("build", strconv.Itoa(build))
  55. params.Set("keyword", keyword)
  56. params.Set("main_ver", "v3")
  57. params.Set("highlight", strconv.Itoa(highlight))
  58. params.Set("mobi_app", mobiApp)
  59. params.Set("device", device)
  60. params.Set("userid", strconv.FormatInt(mid, 10))
  61. params.Set("tids", strconv.Itoa(rid))
  62. params.Set("page", strconv.Itoa(pn))
  63. params.Set("pagesize", strconv.Itoa(ps))
  64. params.Set("media_bangumi_num", strconv.Itoa(seasonNum))
  65. params.Set("bili_user_num", strconv.Itoa(biliUserNum))
  66. params.Set("bili_user_vl", strconv.Itoa(biliUserVideoLimit))
  67. params.Set("user_num", strconv.Itoa(userNum))
  68. params.Set("user_video_limit", strconv.Itoa(userVideoLimit))
  69. params.Set("query_rec_need", recommend)
  70. params.Set("platform", platform)
  71. params.Set("duration", duration)
  72. params.Set("order", order)
  73. params.Set("search_type", "all")
  74. params.Set("from_source", fromSource)
  75. if filtered == "1" {
  76. params.Set("filtered", filtered)
  77. }
  78. params.Set("zone_id", strconv.FormatInt(zoneid, 10))
  79. params.Set("media_ft_num", strconv.Itoa(movieNum))
  80. params.Set("is_new_pgc", "1")
  81. params.Set("is_internation", "1")
  82. params.Set("no_display_default", "game,live_room")
  83. params.Set("flow_need", "1")
  84. params.Set("app_highlight", "media_bangumi,media_ft")
  85. // new request
  86. if req, err = d.client.NewRequest("GET", d.main, ip, params); err != nil {
  87. return
  88. }
  89. req.Header.Set("Buvid", buvid)
  90. if err = d.client.Do(c, req, res); err != nil {
  91. return
  92. }
  93. b, _ := json.Marshal(res)
  94. log.Error("wocao----%s---%s---%s", d.main+"?"+params.Encode(), buvid, b)
  95. if res.Code != ecode.OK.Code() {
  96. err = errors.Wrap(ecode.Int(res.Code), d.main+"?"+params.Encode())
  97. }
  98. for _, flow := range res.FlowResult {
  99. flow.Change()
  100. }
  101. code = res.Code
  102. return
  103. }
  104. // Season2 search new season data.
  105. func (d *Dao) Season2(c context.Context, mid int64, keyword, mobiApp, device, platform, buvid string, highlight, build, pn, ps int) (st *search.TypeSearch, err error) {
  106. var (
  107. req *http.Request
  108. ip = metadata.String(c, metadata.RemoteIP)
  109. seasonIDs []int64
  110. bangumis map[string]*bangumi.Card
  111. )
  112. params := url.Values{}
  113. params.Set("main_ver", "v3")
  114. params.Set("platform", platform)
  115. params.Set("build", strconv.Itoa(build))
  116. params.Set("keyword", keyword)
  117. params.Set("userid", strconv.FormatInt(mid, 10))
  118. params.Set("mobi_app", mobiApp)
  119. params.Set("device", device)
  120. params.Set("page", strconv.Itoa(pn))
  121. params.Set("pagesize", strconv.Itoa(ps))
  122. params.Set("search_type", "media_bangumi")
  123. params.Set("order", "totalrank")
  124. params.Set("highlight", strconv.Itoa(highlight))
  125. params.Set("app_highlight", "media_bangumi")
  126. params.Set("is_pgc_all", "1")
  127. if req, err = d.client.NewRequest("GET", d.main, ip, params); err != nil {
  128. return
  129. }
  130. req.Header.Set("Buvid", buvid)
  131. var res struct {
  132. Code int `json:"code"`
  133. SeID string `json:"seid"`
  134. Total int `json:"numResults"`
  135. Pages int `json:"numPages"`
  136. List []*search.Media `json:"result"`
  137. }
  138. if err = d.client.Do(c, req, &res); err != nil {
  139. return
  140. }
  141. if res.Code != ecode.OK.Code() {
  142. err = errors.Wrap(ecode.Int(res.Code), d.main+"?"+params.Encode())
  143. return
  144. }
  145. for _, v := range res.List {
  146. seasonIDs = append(seasonIDs, v.SeasonID)
  147. }
  148. if len(seasonIDs) > 0 {
  149. if bangumis, err = d.bgmDao.Card(c, mid, seasonIDs); err != nil {
  150. log.Error("%+v", err)
  151. err = nil
  152. }
  153. }
  154. items := make([]*search.Item, 0, len(res.List))
  155. for _, v := range res.List {
  156. si := &search.Item{}
  157. si.FromMedia(v, "", model.GotoBangumi, bangumis)
  158. items = append(items, si)
  159. }
  160. st = &search.TypeSearch{TrackID: res.SeID, Pages: res.Pages, Total: res.Total, Items: items}
  161. return
  162. }
  163. // MovieByType2 search new movie data from api .
  164. func (d *Dao) MovieByType2(c context.Context, mid int64, keyword, mobiApp, device, platform, buvid string, highlight, build, pn, ps int) (st *search.TypeSearch, err error) {
  165. var (
  166. req *http.Request
  167. ip = metadata.String(c, metadata.RemoteIP)
  168. seasonIDs []int64
  169. bangumis map[string]*bangumi.Card
  170. )
  171. params := url.Values{}
  172. params.Set("keyword", keyword)
  173. params.Set("mobi_app", mobiApp)
  174. params.Set("device", device)
  175. params.Set("platform", platform)
  176. params.Set("userid", strconv.FormatInt(mid, 10))
  177. params.Set("build", strconv.Itoa(build))
  178. params.Set("main_ver", "v3")
  179. params.Set("search_type", "media_ft")
  180. params.Set("page", strconv.Itoa(pn))
  181. params.Set("pagesize", strconv.Itoa(ps))
  182. params.Set("order", "totalrank")
  183. params.Set("highlight", strconv.Itoa(highlight))
  184. params.Set("app_highlight", "media_ft")
  185. params.Set("is_pgc_all", "1")
  186. if req, err = d.client.NewRequest("GET", d.main, ip, params); err != nil {
  187. return
  188. }
  189. req.Header.Set("Buvid", buvid)
  190. var res struct {
  191. Code int `json:"code"`
  192. SeID string `json:"seid"`
  193. Total int `json:"numResults"`
  194. Pages int `json:"numPages"`
  195. List []*search.Media `json:"result"`
  196. }
  197. if err = d.client.Do(c, req, &res); err != nil {
  198. return
  199. }
  200. if res.Code != ecode.OK.Code() {
  201. err = errors.Wrap(ecode.Int(res.Code), d.main+"?"+params.Encode())
  202. return
  203. }
  204. for _, v := range res.List {
  205. seasonIDs = append(seasonIDs, v.SeasonID)
  206. }
  207. if len(seasonIDs) > 0 {
  208. if bangumis, err = d.bgmDao.Card(c, mid, seasonIDs); err != nil {
  209. log.Error("%+v", err)
  210. err = nil
  211. }
  212. }
  213. items := make([]*search.Item, 0, len(res.List))
  214. for _, v := range res.List {
  215. si := &search.Item{}
  216. si.FromMedia(v, "", model.GotoMovie, bangumis)
  217. items = append(items, si)
  218. }
  219. st = &search.TypeSearch{TrackID: res.SeID, Pages: res.Pages, Total: res.Total, Items: items}
  220. return
  221. }
  222. // Upper search upper data.
  223. func (d *Dao) Upper(c context.Context, mid int64, keyword, mobiApp, device, platform, buvid, filtered, order string, biliUserVL, highlight, build, userType, orderSort, pn, ps int, now time.Time) (st *search.TypeSearch, err error) {
  224. var (
  225. req *http.Request
  226. avids []int64
  227. avm map[int64]*api.Arc
  228. ip = metadata.String(c, metadata.RemoteIP)
  229. )
  230. params := url.Values{}
  231. params.Set("main_ver", "v3")
  232. params.Set("keyword", keyword)
  233. params.Set("userid", strconv.FormatInt(mid, 10))
  234. params.Set("highlight", strconv.Itoa(highlight))
  235. params.Set("mobi_app", mobiApp)
  236. params.Set("device", device)
  237. params.Set("func", "search")
  238. params.Set("page", strconv.Itoa(pn))
  239. params.Set("pagesize", strconv.Itoa(ps))
  240. params.Set("smerge", "1")
  241. params.Set("platform", platform)
  242. params.Set("build", strconv.Itoa(build))
  243. params.Set("search_type", "bili_user")
  244. params.Set("bili_user_vl", strconv.Itoa(biliUserVL))
  245. params.Set("user_type", strconv.Itoa(userType))
  246. params.Set("order_sort", strconv.Itoa(orderSort))
  247. params.Set("order", order)
  248. params.Set("source_type", "0")
  249. if filtered == "1" {
  250. params.Set("filtered", filtered)
  251. }
  252. // new request
  253. if req, err = d.client.NewRequest("GET", d.main, ip, params); err != nil {
  254. return
  255. }
  256. req.Header.Set("Buvid", buvid)
  257. var res struct {
  258. Code int `json:"code"`
  259. SeID string `json:"seid"`
  260. Pages int `json:"numPages"`
  261. List []*search.User `json:"result"`
  262. }
  263. if err = d.client.Do(c, req, &res); err != nil {
  264. return
  265. }
  266. if res.Code != ecode.OK.Code() {
  267. err = errors.Wrap(ecode.Int(res.Code), d.main+"?"+params.Encode())
  268. return
  269. }
  270. items := make([]*search.Item, 0, len(res.List))
  271. for _, v := range res.List {
  272. for _, vr := range v.Res {
  273. avids = append(avids, vr.Aid)
  274. }
  275. }
  276. g, ctx := errgroup.WithContext(c)
  277. if len(avids) != 0 {
  278. g.Go(func() (err error) {
  279. if avm, err = d.arcDao.Archives(ctx, avids); err != nil {
  280. log.Error("Upper %+v", err)
  281. err = nil
  282. }
  283. return
  284. })
  285. }
  286. if err = g.Wait(); err != nil {
  287. log.Error("%+v", err)
  288. return
  289. }
  290. for _, v := range res.List {
  291. si := &search.Item{}
  292. si.FromUpUser(v, avm)
  293. items = append(items, si)
  294. }
  295. st = &search.TypeSearch{TrackID: res.SeID, Pages: res.Pages, Items: items}
  296. return
  297. }
  298. // ArticleByType search article.
  299. func (d *Dao) ArticleByType(c context.Context, mid, zoneid int64, keyword, mobiApp, device, platform, buvid, filtered, order, sType string, plat int8, categoryID, build, highlight, pn, ps int, now time.Time) (st *search.TypeSearch, err error) {
  300. var (
  301. req *http.Request
  302. ip = metadata.String(c, metadata.RemoteIP)
  303. )
  304. params := url.Values{}
  305. params.Set("keyword", keyword)
  306. params.Set("mobi_app", mobiApp)
  307. params.Set("device", device)
  308. params.Set("platform", platform)
  309. params.Set("userid", strconv.FormatInt(mid, 10))
  310. params.Set("build", strconv.Itoa(build))
  311. params.Set("main_ver", "v3")
  312. params.Set("highlight", strconv.Itoa(highlight))
  313. params.Set("search_type", sType)
  314. params.Set("category_id", strconv.Itoa(categoryID))
  315. params.Set("page", strconv.Itoa(pn))
  316. params.Set("pagesize", strconv.Itoa(ps))
  317. params.Set("order", order)
  318. if filtered == "1" {
  319. params.Set("filtered", filtered)
  320. }
  321. if model.IsOverseas(plat) {
  322. params.Set("use_area", "1")
  323. params.Set("zone_id", strconv.FormatInt(zoneid, 10))
  324. }
  325. if req, err = d.client.NewRequest("GET", d.main, ip, params); err != nil {
  326. return
  327. }
  328. req.Header.Set("Buvid", buvid)
  329. var res struct {
  330. Code int `json:"code"`
  331. SeID string `json:"seid"`
  332. Pages int `json:"numPages"`
  333. List []*search.Article `json:"result"`
  334. }
  335. if err = d.client.Do(c, req, &res); err != nil {
  336. return
  337. }
  338. if res.Code != ecode.OK.Code() {
  339. if res.Code != model.ForbidCode && res.Code != model.NoResultCode {
  340. err = errors.Wrap(ecode.Int(res.Code), d.main+"?"+params.Encode())
  341. }
  342. return
  343. }
  344. items := make([]*search.Item, 0, len(res.List))
  345. for _, v := range res.List {
  346. si := &search.Item{}
  347. si.FromArticle(v)
  348. items = append(items, si)
  349. }
  350. st = &search.TypeSearch{TrackID: res.SeID, Pages: res.Pages, Items: items}
  351. return
  352. }
  353. // Channel for search channel
  354. func (d *Dao) Channel(c context.Context, mid int64, keyword, mobiApp, platform, buvid, device, order, sType string, build, pn, ps, highlight int) (st *search.TypeSearch, err error) {
  355. var (
  356. req *http.Request
  357. ip = metadata.String(c, metadata.RemoteIP)
  358. )
  359. params := url.Values{}
  360. params.Set("keyword", keyword)
  361. params.Set("mobi_app", mobiApp)
  362. params.Set("platform", platform)
  363. params.Set("userid", strconv.FormatInt(mid, 10))
  364. params.Set("build", strconv.Itoa(build))
  365. params.Set("main_ver", "v3")
  366. params.Set("search_type", sType)
  367. params.Set("page", strconv.Itoa(pn))
  368. params.Set("pagesize", strconv.Itoa(ps))
  369. params.Set("device", device)
  370. params.Set("order", order)
  371. params.Set("highlight", strconv.Itoa(highlight))
  372. // new request
  373. if req, err = d.client.NewRequest("GET", d.main, ip, params); err != nil {
  374. return
  375. }
  376. req.Header.Set("Buvid", buvid)
  377. var res struct {
  378. Code int `json:"code"`
  379. SeID string `json:"seid"`
  380. Pages int `json:"numPages"`
  381. List []*search.Channel `json:"result"`
  382. }
  383. if err = d.client.Do(c, req, &res); err != nil {
  384. return
  385. }
  386. if res.Code != ecode.OK.Code() {
  387. err = errors.Wrap(ecode.Int(res.Code), d.main+"?"+params.Encode())
  388. return
  389. }
  390. items := make([]*search.Item, 0, len(res.List))
  391. for _, v := range res.List {
  392. si := &search.Item{}
  393. si.FromChannel(v)
  394. items = append(items, si)
  395. }
  396. st = &search.TypeSearch{TrackID: res.SeID, Pages: res.Pages, Items: items}
  397. return
  398. }
  399. // Suggest3 suggest data.
  400. func (d *Dao) Suggest3(c context.Context, mid int64, platform, buvid, term string, build, highlight int, mobiApp string, now time.Time) (res *search.Suggest3, err error) {
  401. var (
  402. req *http.Request
  403. ip = metadata.String(c, metadata.RemoteIP)
  404. )
  405. params := url.Values{}
  406. params.Set("suggest_type", "accurate")
  407. params.Set("platform", platform)
  408. params.Set("mobi_app", mobiApp)
  409. params.Set("clientip", ip)
  410. params.Set("highlight", strconv.Itoa(highlight))
  411. params.Set("build", strconv.Itoa(build))
  412. if mid != 0 {
  413. params.Set("userid", strconv.FormatInt(mid, 10))
  414. }
  415. params.Set("term", term)
  416. params.Set("sug_num", "10")
  417. params.Set("buvid", buvid)
  418. if req, err = d.client.NewRequest("GET", d.suggest3, ip, params); err != nil {
  419. return
  420. }
  421. req.Header.Set("Buvid", buvid)
  422. res = &search.Suggest3{}
  423. if err = d.client.Do(c, req, &res); err != nil {
  424. return
  425. }
  426. if res.Code != ecode.OK.Code() {
  427. err = errors.Wrap(ecode.Int(res.Code), d.suggest3+"?"+params.Encode())
  428. }
  429. return
  430. }