Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"elastic_test.go",
"result_test.go",
"search_sug_test.go",
"wild_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/tv/conf:go_default_library",
"//app/interface/main/tv/model/search:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"elastic.go",
"result.go",
"search_sug.go",
"wild.go",
],
importpath = "go-common/app/interface/main/tv/dao/search",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/tv/conf:go_default_library",
"//app/interface/main/tv/model/search:go_default_library",
"//library/database/elastic:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/sync/errgroup:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,40 @@
package search
import (
"go-common/app/interface/main/tv/conf"
"go-common/library/database/elastic"
bm "go-common/library/net/http/blademaster"
)
const (
_userSearch = "/main/search"
_card = "/pgc/internal/season/search/card"
)
// Dao is search dao.
type Dao struct {
conf *conf.Config
client *bm.Client
resultURL string
esClient *elastic.Elastic
userSearch string
card string
cfgWild *conf.WildSearch
}
// New account dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
client: bm.NewClient(c.SearchClient),
resultURL: c.Search.ResultURL,
conf: c,
esClient: elastic.NewElastic(&elastic.Config{
Host: c.Host.ESHost,
HTTPClient: c.SearchClient,
}),
userSearch: c.Search.UserSearch + _userSearch,
card: c.Host.APICo + _card,
cfgWild: c.Wild.WildSearch,
}
return
}

View File

@@ -0,0 +1,33 @@
package search
import (
"flag"
"go-common/app/interface/main/tv/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.tv-interface")
flag.Set("conf_token", "07c1826c1f39df02a1411cdd6f455879")
flag.Set("tree_id", "15326")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,116 @@
package search
import (
"context"
"fmt"
"regexp"
"strconv"
seaMdl "go-common/app/interface/main/tv/model/search"
"go-common/library/database/elastic"
"go-common/library/ecode"
"go-common/library/log"
)
func yearTrans(year string) (stime, etime string, err error) {
yearExp := regexp.MustCompile(`^([\d]{4})-([\d]{4})$`)
params := yearExp.FindStringSubmatch(year)
if len(params) < 3 {
err = ecode.RequestErr
return
}
return params[1] + "-01-01 00:00:00", params[2] + "-12-31 23:59:59", nil
}
// PgcIdx treats the pgc index request and call the ES to get the result
func (d *Dao) PgcIdx(c context.Context, req *seaMdl.ReqPgcIdx) (data *seaMdl.EsPgcResult, err error) {
var (
syear, eyear string
cfg = d.conf.Cfg.EsIdx.PgcIdx
r = d.esClient.NewRequest(cfg.Business).Index(cfg.Index).
Fields("season_id").WhereEq("is_deleted", 0).WhereEq("status", 0).
WhereRange("season_id", 0, nil, elastic.RangeScopeLoRc)
)
if req.SeasonType > 0 {
r.WhereEq("season_type", req.SeasonType)
}
if req.ProducerID > 0 {
r.WhereEq("producer_id", req.ProducerID)
}
if !req.IsAllStr(req.Year) {
if syear, eyear, err = yearTrans(req.Year); err != nil {
log.Warn("PgcIdx Request Year %s, Err %v", req.Year, err)
return
}
r.WhereRange("release_date", syear, eyear, elastic.RangeScopeLcRc)
}
if !req.IsAllStr(req.PubDate) {
if syear, eyear, err = yearTrans(req.PubDate); err != nil {
log.Warn("PgcIdx Request PubDate %s, Err %v", req.PubDate, err)
return
}
r.WhereRange("pub_time", syear, eyear, elastic.RangeScopeLcRc)
}
if req.StyleID > 0 {
r.WhereEq("style_id", req.StyleID)
}
if req.SeasonMonth > 0 {
r.WhereEq("season_month", req.SeasonMonth)
}
if !req.IsAll(req.SeasonStatus) {
r.WhereIn("pay_status", req.SeasonStatus)
}
if !req.IsAll(req.Copyright) {
r.WhereIn("copyright_info", req.Copyright)
}
if !req.IsAllStr(req.IsFinish) {
isFin, _ := strconv.Atoi(req.IsFinish)
r.WhereEq("is_finish", isFin)
}
if !req.IsAll(req.Area) {
r.WhereIn("area_id", req.Area)
}
if req.SeasonVersion > 0 {
r.WhereEq("season_version", req.SeasonVersion)
}
r.Ps(req.Ps).Pn(int(req.Pn)).Order(req.PgcOrder(), seaMdl.IdxSort(req.Sort))
if err = r.Scan(c, &data); err != nil {
log.Error("PgcIdx:Scan params(%s) error(%v)", r.Params(), err)
return
}
if data == nil || data.Page == nil {
err = fmt.Errorf("data or data.Page nil")
log.Error("PgcIdx params(%s) error(%v)", r.Params(), err)
return
}
data.Page.GetPageNb() // calculate page number
return
}
// UgcIdx treats the ugc index request and call the ES to get the result
func (d *Dao) UgcIdx(c context.Context, req *seaMdl.SrvUgcIdx) (data *seaMdl.EsUgcResult, err error) {
if len(req.TIDs) == 0 {
err = ecode.RequestErr
return
}
var (
cfg = d.conf.Cfg.EsIdx.UgcIdx
r = d.esClient.NewRequest(cfg.Business).Index(cfg.Index).WhereEq("deleted", 0).
WhereEq("valid", 1).WhereEq("result", 1).WhereIn("typeid", req.TIDs)
)
if pub := req.PubTime; pub != nil {
r.WhereRange("pubtime", pub.STime, pub.ETime, elastic.RangeScopeLcRc)
}
r.Ps(req.Ps).Pn(int(req.Pn)).Order(req.UgcOrder(), seaMdl.IdxSort(req.Sort))
if err = r.Scan(c, &data); err != nil {
log.Error("PgcIdx:Scan params(%s) error(%v)", r.Params(), err)
return
}
if data == nil || data.Page == nil {
err = fmt.Errorf("data or data.Page nil")
log.Error("PgcIdx params(%s) error(%v)", r.Params(), err)
return
}
data.Page.GetPageNb() // calculate page number
return
}

View File

@@ -0,0 +1,120 @@
package search
import (
"context"
"encoding/json"
"fmt"
"testing"
seaMdl "go-common/app/interface/main/tv/model/search"
"go-common/library/ecode"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchyearTrans(t *testing.T) {
var (
year = "2018-2019"
)
convey.Convey("yearTrans", t, func(ctx convey.C) {
stime, etime, err := yearTrans(year)
ctx.Convey("Then err should be nil.stime,etime should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(etime, convey.ShouldNotBeNil)
ctx.So(stime, convey.ShouldNotBeNil)
fmt.Println(stime, etime)
})
})
}
func TestSearchPgcIdx(t *testing.T) {
var (
c = context.Background()
pnDoc = seaMdl.ReqEsPn{
Ps: 50,
Pn: 1,
Order: 6,
Sort: 0,
}
reqDoc = &seaMdl.ReqPgcIdx{
SeasonType: 3,
ProducerID: 31,
Year: "",
StyleID: -1,
PubDate: "",
SeasonMonth: 0,
SeasonStatus: []int{},
Copyright: []int{},
IsFinish: "",
Area: []int{},
SeasonVersion: 0,
}
reqJp = &seaMdl.ReqPgcIdx{
SeasonType: 1,
ProducerID: -1,
Year: "",
StyleID: 136,
PubDate: "2018-2018",
SeasonMonth: 10,
SeasonStatus: []int{2, 6}, // need to pay
Copyright: []int{0, 1, 2, 4},
IsFinish: "0",
Area: []int{},
SeasonVersion: 1,
}
)
convey.Convey("PgcIdxJp", t, func(ctx convey.C) {
ctx.Convey("PgcIdxJp should not be nil", func(ctx convey.C) {
reqDoc.ReqEsPn = pnDoc
dataDoc, err2 := d.PgcIdx(c, reqDoc)
ctx.So(err2, convey.ShouldBeNil)
ctx.So(dataDoc, convey.ShouldNotBeNil)
str, _ := json.Marshal(dataDoc)
fmt.Println(string(str))
})
ctx.Convey("PgcIdxDoc should not be nil", func(ctx convey.C) {
pnDoc.Sort = 1
reqJp.ReqEsPn = pnDoc
dataJp, err := d.PgcIdx(c, reqJp)
ctx.So(err, convey.ShouldBeNil)
ctx.So(dataJp, convey.ShouldNotBeNil)
fmt.Println(dataJp)
str, _ := json.Marshal(dataJp)
fmt.Println(string(str))
})
})
}
func TestSeachUgcIdx(t *testing.T) {
var (
c = context.Background()
pnDoc = seaMdl.ReqEsPn{
Ps: 50,
Pn: 1,
Order: 6,
Sort: 0,
}
reqNone = &seaMdl.SrvUgcIdx{}
reqTIDs = &seaMdl.SrvUgcIdx{
TIDs: []int32{75},
PubTime: &seaMdl.UgcTime{
STime: "2017-01-01 00:00:00",
ETime: "2018-12-31 23:59:59",
},
}
)
convey.Convey("UgcIdx", t, func(ctx convey.C) {
ctx.Convey("If TIDs empty, return request error ", func(ctx convey.C) {
reqTIDs.ReqEsPn = pnDoc
_, err := d.UgcIdx(c, reqNone)
ctx.So(err, convey.ShouldEqual, ecode.RequestErr)
})
ctx.Convey("Pick Result with type_ids AND pub_time", func(ctx convey.C) {
dataDoc, err := d.UgcIdx(c, reqTIDs)
ctx.So(err, convey.ShouldBeNil)
ctx.So(dataDoc, convey.ShouldNotBeNil)
str, _ := json.Marshal(dataDoc)
fmt.Println(string(str))
})
})
}

View File

@@ -0,0 +1,76 @@
package search
import (
"context"
"fmt"
"net/url"
searchMdl "go-common/app/interface/main/tv/model/search"
"go-common/library/ecode"
"go-common/library/log"
"github.com/pkg/errors"
)
// SearchAll gets the search all_tv result
func (d *Dao) SearchAll(ctx context.Context, req *searchMdl.ReqSearch) (result searchMdl.RespAll, common *searchMdl.ResultResponse, err error) {
params := commonParam(req)
if err = d.client.Get(ctx, d.resultURL, "", params, &result); err != nil {
log.Error("[result] SearchAll URL(%s) error[%v]", d.resultURL, err)
return
}
if result.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(result.Code), "Search API Error: "+result.Msg)
log.Error("[result] SearchAll URL(%s) error[%v]", d.resultURL, err)
}
common = result.ResultResponse
return
}
// SearchUgc gets the search tv_ugc result
func (d *Dao) SearchUgc(ctx context.Context, req *searchMdl.ReqSearch) (result searchMdl.RespUgc, common *searchMdl.ResultResponse, err error) {
// common params
params := commonParam(req)
params.Set("category", fmt.Sprintf("%d", req.Category))
if err = d.client.Get(ctx, d.resultURL, "", params, &result); err != nil {
log.Error("[result] SearchUgc URL(%s) error[%v]", d.resultURL, err)
return
}
if result.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(result.Code), "Search API Error: "+result.Msg)
log.Error("[result] SearchUgc Code(%d) URL(%s) error[%v]", result.Code, d.resultURL, err)
}
common = result.ResultResponse
return
}
// SearchPgc gets the search tv_pgc result
func (d *Dao) SearchPgc(ctx context.Context, req *searchMdl.ReqSearch) (result searchMdl.RespPgc, common *searchMdl.ResultResponse, err error) {
params := commonParam(req)
if err = d.client.Get(ctx, d.resultURL, "", params, &result); err != nil {
log.Error("[result] SearchPgc URL(%s) error[%v]", d.resultURL, err)
return
}
if result.Code != ecode.OK.Code() {
log.Error("ClientGet Code Result Not OK [%v]", result)
err = errors.Wrap(ecode.Int(result.Code), "Search API Error: "+result.Msg)
}
common = result.ResultResponse
return
}
func commonParam(req *searchMdl.ReqSearch) (params url.Values) {
params = url.Values{}
params.Set("search_type", req.SearchType)
params.Set("order", req.Order)
params.Set("build", req.Build)
params.Set("mobi_app", req.MobiAPP)
params.Set("platform", req.Platform)
params.Set("device", req.Device)
params.Set("keyword", req.Keyword)
params.Set("page", fmt.Sprintf("%d", req.Page))
if req.Pagesize != 0 {
params.Set("pagesize", fmt.Sprintf("%d", req.Pagesize))
}
return
}

View File

@@ -0,0 +1,88 @@
package search
import (
"context"
searchMdl "go-common/app/interface/main/tv/model/search"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchSearchAll(t *testing.T) {
var (
ctx = context.Background()
req = &searchMdl.ReqSearch{
SearchType: "all_tv",
Keyword: "番剧",
Page: 1,
MobiAPP: "android_tv_yst",
Platform: "android",
Build: "1011",
}
)
convey.Convey("SearchAll", t, func(cx convey.C) {
result, common, err := d.SearchAll(ctx, req)
cx.Convey("Then err should be nil.result,all should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(common, convey.ShouldNotBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
}
func TestSearchSearchUgc(t *testing.T) {
var (
ctx = context.Background()
req = &searchMdl.ReqSearch{
SearchType: "tv_ugc",
Category: 160,
Keyword: "测试",
Page: 1,
MobiAPP: "android_tv_yst",
Platform: "android",
Build: "1011",
}
)
convey.Convey("SearchUgc", t, func(cx convey.C) {
result, common, err := d.SearchUgc(ctx, req)
cx.Convey("Then err should be nil.result,ugc should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(common, convey.ShouldNotBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
}
func TestSearchSearchPgc(t *testing.T) {
var (
ctx = context.Background()
req = &searchMdl.ReqSearch{
SearchType: "tv_pgc",
Keyword: "番剧",
Page: 1,
MobiAPP: "android_tv_yst",
Platform: "android",
Build: "1011",
}
)
convey.Convey("SearchPgc", t, func(cx convey.C) {
result, common, err := d.SearchPgc(ctx, req)
cx.Convey("Then err should be nil.result,pgc should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(common, convey.ShouldNotBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
}
func TestSearchcommonParam(t *testing.T) {
var (
req = &searchMdl.ReqSearch{}
)
convey.Convey("commonParam", t, func(cx convey.C) {
params := commonParam(req)
cx.Convey("Then params should not be nil.", func(ctx convey.C) {
ctx.So(params, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,40 @@
package search
import (
"context"
"fmt"
"net/url"
searchMdl "go-common/app/interface/main/tv/model/search"
"go-common/library/ecode"
"go-common/library/log"
"github.com/pkg/errors"
)
// SearchSug gets the search sug detail data from Search API
func (d *Dao) SearchSug(ctx context.Context, req *searchMdl.ReqSug) (result searchMdl.SugResponse, err error) {
var (
config = d.conf.Search
params = url.Values{}
)
// common params
params.Set("main_ver", config.MainVer)
params.Set("sug_num", fmt.Sprintf("%d", config.SugNum))
params.Set("suggest_type", config.SugType)
params.Set("highlight", config.Highlight)
params.Set("build", req.Build)
params.Set("mobi_app", req.MobiApp)
params.Set("platform", req.Platform)
params.Set("term", req.Term) // search term
if err = d.client.Get(ctx, config.URL, "", params, &result); err != nil {
log.Error("ClientGet URL %s error[%v]", config.URL, err)
return
}
if result.Code != ecode.OK.Code() {
log.Error("ClientGet Code Result Not OK [%v]", result)
err = errors.Wrap(ecode.ServerErr, "Search API Error")
return
}
return
}

View File

@@ -0,0 +1,29 @@
package search
import (
"context"
"testing"
searchMdl "go-common/app/interface/main/tv/model/search"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchSearchSug(t *testing.T) {
var (
ctx = context.Background()
req = &searchMdl.ReqSug{
MobiApp: "android_tv_yst",
Build: "1011",
Platform: "android",
Term: "test",
}
)
convey.Convey("SearchSug", t, func(c convey.C) {
result, err := d.SearchSug(ctx, req)
c.Convey("Then err should be nil.result should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(result, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,213 @@
package search
import (
"context"
"go-common/library/sync/errgroup"
"go-common/library/xstr"
"net/http"
"net/url"
"strconv"
mdlSearch "go-common/app/interface/main/tv/model/search"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// UserSearch search user .
func (d *Dao) UserSearch(ctx context.Context, arg *mdlSearch.UserSearch) (user []*mdlSearch.User, err error) {
var (
params = url.Values{}
req *http.Request
ip = metadata.String(ctx, metadata.RemoteIP)
)
params.Set("platform", arg.Platform)
params.Set("mobi_app", arg.MobiAPP)
params.Set("build", arg.Build)
params.Set("keyword", arg.Keyword)
params.Set("page", strconv.Itoa(arg.Page))
params.Set("pagesize", strconv.Itoa(arg.Pagesize))
params.Set("userid", strconv.FormatInt(arg.MID, 10))
params.Set("order", arg.Order)
params.Set("main_ver", "v3") // 支持赛事搜索
params.Set("search_type", "bili_user")
params.Set("user_type", strconv.Itoa(arg.UserType)) // 用户类型
params.Set("highlight", strconv.Itoa(arg.Highlight))
params.Set("order_sort", strconv.Itoa(arg.OrderSort))
params.Set("from_source", arg.FromSource)
params.Set("bili_user_vl", strconv.Itoa(d.cfgWild.BiliUserVl))
// new request
if req, err = d.client.NewRequest("GET", d.userSearch, ip, params); err != nil {
log.Error("[wild.UserSearch] d.client.NewRequest url(%s) error(%v)", d.userSearch, err)
return
}
req.Header.Set("Buvid", arg.Buvid)
var res struct {
Code int `json:"code"`
SeID string `json:"seid"`
Pages int `json:"numPages"`
List []*mdlSearch.User `json:"result"`
}
if err = d.client.Do(ctx, req, &res); err != nil {
log.Error("[wild.UserSearch] d.client.Do url(%s) error(%v)", d.userSearch, err)
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
log.Error("[wild.UserSearch] url(%s) error(%v)", d.userSearch, err)
return
}
user = res.List
return
}
// SearchAllWild wild search all .
func (d *Dao) SearchAllWild(ctx context.Context, arg *mdlSearch.UserSearch) (user *mdlSearch.Search, err error) {
var (
req *http.Request
ip = metadata.String(ctx, metadata.RemoteIP)
)
params := url.Values{}
user = &mdlSearch.Search{}
params.Set("build", arg.Build)
params.Set("keyword", arg.Keyword)
params.Set("main_ver", "v3")
params.Set("mobi_app", arg.MobiAPP)
params.Set("device", arg.Device)
params.Set("userid", strconv.FormatInt(arg.MID, 10))
params.Set("tids", strconv.Itoa(arg.RID))
params.Set("highlight", strconv.Itoa(arg.Highlight))
params.Set("page", strconv.Itoa(arg.Page))
params.Set("pagesize", strconv.Itoa(arg.Pagesize))
params.Set("bili_user_num", strconv.Itoa(d.cfgWild.BiliUserNum))
params.Set("bili_user_vl", strconv.Itoa(d.cfgWild.BiliUserVl))
params.Set("user_num", strconv.Itoa(d.cfgWild.UserNum))
params.Set("user_video_limit", strconv.Itoa(d.cfgWild.UserVideoLimit))
params.Set("platform", arg.Platform)
// params.Set("duration", strconv.Itoa(arg.Duration)) // 视频时长筛选默认是0
params.Set("order", arg.Order)
params.Set("search_type", "all")
params.Set("from_source", arg.FromSource)
params.Set("media_bangumi_num", strconv.Itoa(arg.SeasonNum))
params.Set("movie_num", strconv.Itoa(arg.MovieNum))
params.Set("is_new_pgc", "1") // 新番剧
params.Set("media_ft_num", strconv.Itoa(arg.MovieNum))
// params.Set("flow_need", "1") // 混排
// params.Set("query_rec_need", "1") // 搜索结果推荐
// new request
if req, err = d.client.NewRequest("GET", d.userSearch, ip, params); err != nil {
log.Error("d.client.NewRequest URI(%s) error(%v)", d.userSearch, err)
return
}
req.Header.Set("Buvid", arg.Buvid)
if err = d.client.Do(ctx, req, user); err != nil {
log.Error("[wild.SearchAllWild] d.client.Do() url(%s) error(%v)", d.userSearch, err)
}
return
}
// card bangumi card .
func (d *Dao) cardInfo(c context.Context, mid int64, sids []int64) (s map[string]*mdlSearch.Card, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("season_ids", xstr.JoinInts(sids))
var res struct {
Code int `json:"code"`
Result map[string]*mdlSearch.Card `json:"result"`
}
if err = d.client.Get(c, d.card, ip, params, &res); err != nil {
log.Error("d.client.NewRequest url(%s) error(%v)", d.resultURL, err)
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
log.Error("[wild] cardInfo error(%v)", err)
return
}
s = res.Result
return
}
// PgcSearch .
func (d *Dao) PgcSearch(c context.Context, arg *mdlSearch.UserSearch) (st *mdlSearch.TypeSearch, err error) {
var (
req *http.Request
ip = metadata.String(c, metadata.RemoteIP)
seasonIDs []int64
bangumis map[string]*mdlSearch.Card
items1, items2 []*mdlSearch.Item
)
params := url.Values{}
params.Set("build", arg.Build)
params.Set("keyword", arg.Keyword)
params.Set("main_ver", "v3")
params.Set("mobi_app", arg.MobiAPP)
params.Set("device", arg.Device)
params.Set("userid", strconv.FormatInt(arg.MID, 10))
params.Set("highlight", strconv.Itoa(arg.Highlight))
params.Set("page", strconv.Itoa(arg.Page))
params.Set("pagesize", strconv.Itoa(arg.Pagesize))
params.Set("platform", arg.Platform)
params.Set("order", arg.Order)
params.Set("search_type", "all")
params.Set("from_source", arg.FromSource)
params.Set("media_bangumi_num", strconv.Itoa(arg.SeasonNum))
params.Set("media_ft_num", strconv.Itoa(arg.MovieNum))
params.Set("is_new_pgc", "1")
if req, err = d.client.NewRequest("GET", d.userSearch, ip, params); err != nil {
log.Error("d.client.NewRequest url(%s) error(%v)", d.userSearch, err)
return
}
req.Header.Set("Buvid", arg.Buvid)
res := &mdlSearch.Search{}
if err = d.client.Do(c, req, res); err != nil {
log.Error("[wild.PgcSearch] d.client.Do url(%s) error(%v)", d.userSearch, err)
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
log.Error("[wild.PgcSearch] code(%d) error(%v)", res.Code, err)
return
}
for _, v := range res.Result.MediaBangumi {
seasonIDs = append(seasonIDs, v.SeasonID)
}
for _, v := range res.Result.MediaFt {
seasonIDs = append(seasonIDs, v.SeasonID)
}
if len(seasonIDs) > 0 {
if bangumis, err = d.cardInfo(c, arg.MID, seasonIDs); err != nil {
log.Error("[wild.PgcSearch] MovieByType2 %+v", err)
return
}
}
if len(bangumis) > 0 {
group := new(errgroup.Group)
group.Go(func() error {
items1 = make([]*mdlSearch.Item, 0, len(res.Result.MediaBangumi))
for _, v := range res.Result.MediaBangumi {
si := &mdlSearch.Item{}
si.FromMedia(v, "", mdlSearch.GotoMovie, bangumis)
items1 = append(items1, si)
}
return nil
})
group.Go(func() error {
items2 = make([]*mdlSearch.Item, 0, len(res.Result.MediaFt))
for _, v := range res.Result.MediaFt {
si := &mdlSearch.Item{}
si.FromMedia(v, "", mdlSearch.GotoMovie, bangumis)
items2 = append(items2, si)
}
return nil
})
if err = group.Wait(); err != nil {
log.Error("[wild.PgcSearch] group.Wait() is error(%v)", err)
return
}
}
items1 = append(items1, items2...)
st = &mdlSearch.TypeSearch{TrackID: res.Trackid, Pages: res.Page, Total: res.Total, Items: items1}
return
}

View File

@@ -0,0 +1,76 @@
package search
import (
"context"
"testing"
mdlSearch "go-common/app/interface/main/tv/model/search"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchUserSearch(t *testing.T) {
convey.Convey("UserSearch", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &mdlSearch.UserSearch{
Keyword: "lex",
Build: "111",
SearchType: "bili_user",
Page: 1,
Pagesize: 20,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
user, err := d.UserSearch(c, arg)
ctx.Convey("Then err should be nil.user should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(user, convey.ShouldNotBeNil)
})
})
})
}
func TestSearchSearchAllWild(t *testing.T) {
convey.Convey("SearchAllWild", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &mdlSearch.UserSearch{
Keyword: "工作细胞",
Build: "111",
SearchType: "all",
Page: 1,
Pagesize: 20,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
user, err := d.SearchAllWild(c, arg)
ctx.Convey("Then err should be nil.user should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(user, convey.ShouldNotBeNil)
})
})
})
}
func TestSearchPgcSearch(t *testing.T) {
convey.Convey("PgcSearch", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &mdlSearch.UserSearch{
Keyword: "白兔糖",
Build: "111",
SearchType: "all",
Page: 1,
Pagesize: 20,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
st, err := d.PgcSearch(c, arg)
ctx.Convey("Then err should be nil.st should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(st, convey.ShouldNotBeNil)
})
})
})
}