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_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"dao.go",
"music.go",
"search.go",
],
importpath = "go-common/app/interface/main/creative/dao/music",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/dao/tool:go_default_library",
"//app/interface/main/creative/model/music:go_default_library",
"//app/interface/main/creative/model/search:go_default_library",
"//library/database/elastic:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/xstr: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"],
)
go_test(
name = "go_default_test",
srcs = [
"api_test.go",
"dao_test.go",
"music_test.go",
"search_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/search:go_default_library",
"//library/database/elastic:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,62 @@
package music
import (
"context"
"net/http"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/dao/tool"
"go-common/app/interface/main/creative/model/music"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_audioListURI = "/x/internal/v1/audio/songs/batch"
)
// Audio fn
func (d *Dao) Audio(c context.Context, ids []int64, level int, ip string) (au map[int64]*music.Audio, err error) {
params := url.Values{}
params.Set("ids", xstr.JoinInts(ids))
params.Set("level", strconv.Itoa(level)) //0、只返回基本信息 1、会增加up主名称、播放数、评论数
params.Set("appkey", conf.Conf.App.Key)
params.Set("appsecret", conf.Conf.App.Secret)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
var (
res struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data map[int64]*music.Audio `json:"data"`
}
query, _ = tool.Sign(params)
url string
)
url = d.audioListURL + "?" + query
log.Info("music query url(%s)", url)
// new requests
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v)|ip(%s)", url, err, ip)
err = ecode.CreativeElecErr
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do(%s)|error(%v)|ip(%s)", url, err, ip)
err = ecode.CreativeMusicErr
return
}
if res.Code != 0 {
log.Error("music url(%s)|res(%v)|ip(%s)|code(%d)", url, res, ip, res.Code)
err = ecode.CreativeMusicErr
return
}
au = res.Data
return
}

View File

@@ -0,0 +1,24 @@
package music
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestMusicAudio(t *testing.T) {
var (
c = context.TODO()
ids = []int64{1, 2, 3}
level = int(0)
ip = "127.0.0.1"
)
convey.Convey("Audio", t, func(ctx convey.C) {
au, err := d.Audio(c, ids, level, ip)
ctx.Convey("Then err should be nil.au should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(au, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,46 @@
package music
import (
"context"
"go-common/app/interface/main/creative/conf"
"go-common/library/database/elastic"
"go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
)
// Dao is archive dao.
type Dao struct {
// config
c *conf.Config
db *sql.DB
client *bm.Client
audioListURL string
es *elastic.Elastic
}
// New init api url
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Archive),
// client
client: bm.NewClient(c.HTTPClient.Slow),
audioListURL: c.Host.API + _audioListURI,
es: elastic.NewElastic(&elastic.Config{
Host: c.Host.MainSearch,
HTTPClient: c.HTTPClient.Slow,
}),
}
return
}
// Ping fn
func (d *Dao) Ping(c context.Context) (err error) {
return d.db.Ping(c)
}
// Close fn
func (d *Dao) Close() (err error) {
return d.db.Close()
}

View File

@@ -0,0 +1,35 @@
package music
import (
"flag"
"go-common/app/interface/main/creative/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
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")
} else {
flag.Set("conf", "../../cmd/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,107 @@
package music
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/creative/model/music"
"go-common/library/log"
"go-common/library/xstr"
"sort"
"strings"
)
const (
_CategorysSQL = "SELECT id,pid,name,`index`,camera_index FROM music_category WHERE id IN (%s) and state = 0 order by `index` asc "
_MCategorysSQL = "SELECT id,sid,tid,`index`,ctime FROM music_with_category where state = 0 order by tid asc, `index` asc "
_MusicsSQL = "SELECT cooperate,id,sid,name,frontname,musicians,mid,cover,playurl,state,duration,filesize,ctime,mtime,pubtime,tags,timeline FROM music WHERE sid IN (%s) and state = 0 "
)
// Categorys fn
func (d *Dao) Categorys(c context.Context, ids []int64) (res []*music.Category, resMap map[int]*music.Category, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_CategorysSQL, xstr.JoinInts(ids)))
if err != nil {
log.Error("mysqlDB.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*music.Category, 0)
resMap = make(map[int]*music.Category, len(ids))
for rows.Next() {
v := &music.Category{}
if err = rows.Scan(&v.ID, &v.PID, &v.Name, &v.Index, &v.CameraIndex); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
res = append(res, v)
resMap[v.ID] = v
}
return
}
// MCategorys fn
func (d *Dao) MCategorys(c context.Context) (res []*music.Mcategory, err error) {
rows, err := d.db.Query(c, _MCategorysSQL)
if err != nil {
log.Error("mysqlDB.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*music.Mcategory, 0)
for rows.Next() {
v := &music.Mcategory{}
if err = rows.Scan(&v.ID, &v.SID, &v.Tid, &v.Index, &v.CTime); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
res = append(res, v)
}
return
}
// Music fn
func (d *Dao) Music(c context.Context, sids []int64) (res map[int64]*music.Music, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_MusicsSQL, xstr.JoinInts(sids)))
if err != nil {
log.Error("mysqlDB.Query error(%v)", err)
return
}
defer rows.Close()
res = make(map[int64]*music.Music, len(sids))
for rows.Next() {
v := &music.Music{}
var fName string
if err = rows.Scan(&v.Cooperate, &v.ID, &v.SID, &v.Name, &fName, &v.Musicians, &v.UpMID, &v.Cover, &v.Playurl, &v.State, &v.Duration, &v.FileSize, &v.CTime, &v.MTime, &v.Pubtime, &v.TagsStr, &v.Timeline); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
if len(fName) > 0 {
v.Name = fName
}
v.CooperateURL = d.c.H5Page.Cooperate
v.Tl = make([]*music.TimePoint, 0)
if len(v.Timeline) > 0 {
if err = json.Unmarshal([]byte(v.Timeline), &v.Tl); err != nil {
log.Error("json.Unmarshal Timeline failed error(%v)", err)
continue
}
sort.Slice(v.Tl, func(i, j int) bool {
return v.Tl[i].Point < v.Tl[j].Point
})
if len(v.Tl) > 0 {
for _, point := range v.Tl {
if point.Recommend == 1 {
v.RecommendPoint = point.Point
break
}
}
}
}
v.Tags = make([]string, 0)
if len(v.TagsStr) > 0 {
v.Tags = append(v.Tags, strings.Split(v.TagsStr, ",")...)
}
res[v.SID] = v
}
return
}

View File

@@ -0,0 +1,50 @@
package music
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestMusicCategorys(t *testing.T) {
var (
c = context.TODO()
ids = []int64{10, 11, 12}
)
convey.Convey("Categorys", t, func(ctx convey.C) {
res, resMap, err := d.Categorys(c, ids)
ctx.Convey("Then err should be nil.res,resMap should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(resMap, convey.ShouldNotBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestMusicMCategorys(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("MCategorys", t, func(ctx convey.C) {
res, err := d.MCategorys(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestMusicMusic(t *testing.T) {
var (
c = context.TODO()
sids = []int64{1, 2, 3, 4, 5, 6}
)
convey.Convey("Music", t, func(ctx convey.C) {
res, err := d.Music(c, sids)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,78 @@
package music
import (
"context"
"go-common/app/interface/main/creative/dao/tool"
"go-common/app/interface/main/creative/model/search"
"go-common/library/database/elastic"
"go-common/library/ecode"
"go-common/library/log"
)
// SearchBgmSIDs fn
func (d *Dao) SearchBgmSIDs(c context.Context, keyword string, pn, ps int) (ret []int64, page *search.Pager, err error) {
retSIDs := make([]int64, 0)
sres := &search.BgmResult{}
r := d.es.NewRequest("archive_music").Fields(
"sid",
)
r.Index("archive_music").Pn(1).Ps(5000).OrderScoreFirst(true)
if len(keyword) > 0 { // "frontname", "name", "uname"
r.WhereLike([]string{"music_frontname", "music_name", "uname"}, []string{keyword}, true, elastic.LikeLevelLow)
}
r.WhereEq("music_category_state", 0).WhereEq("music_state", 0).WhereEq("state", 0)
r.Order("music_ctime", "desc") // 默认按入库时间倒序排
log.Info("SearchBgmSIDs params(%s)", r.Params())
if err = r.Scan(c, sres); err != nil {
log.Error("SearchBgmSIDs r.Scan error(%v)", err)
err = ecode.CreativeSearchErr
return
}
if len(sres.Result) > 0 {
for _, v := range sres.Result {
retSIDs = append(retSIDs, v.SID)
}
}
retSIDs = tool.DeDuplicationSlice(retSIDs)
total := len(retSIDs)
start := (pn - 1) * ps
end := pn * ps
page = &search.Pager{
Num: pn,
Size: ps,
Total: total,
}
if total <= start {
ret = make([]int64, 0)
} else if total <= end {
ret = retSIDs[start:total]
} else {
ret = retSIDs[start:end]
}
return
}
// ExtAidsWithSameBgm fn 获取这个sid最多的使用的aids列表,
func (d *Dao) ExtAidsWithSameBgm(c context.Context, sid int64, pn int) (retAIDs []int64, total int, err error) {
retAIDs = make([]int64, 0)
sres := &search.BgmExtResult{}
r := d.es.NewRequest("archive_material").Fields("aid")
r.Index("archive_material").Pn(1).Ps(pn).OrderScoreFirst(true)
r.WhereEq("type", 3).WhereIn("data", sid).GroupBy(elastic.EnhancedModeDistinct, "aid", nil)
r.Order("click", "desc")
log.Info("ExtAidsWithSameBgm params(%s)", r.Params())
if err = r.Scan(c, sres); err != nil {
log.Error("ExtAidsWithSameBgm r.Scan error(%v)", err)
err = ecode.CreativeSearchErr
return
}
if len(sres.Result) > 0 {
for _, v := range sres.Result {
retAIDs = append(retAIDs, v.AID)
}
}
if sres.Page != nil {
total = sres.Page.Total
}
return
}

View File

@@ -0,0 +1,60 @@
package music
import (
"context"
"go-common/app/interface/main/creative/model/search"
"go-common/library/database/elastic"
"go-common/library/ecode"
"reflect"
"testing"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestMusicSearchBgmSIDs(t *testing.T) {
var (
c = context.TODO()
ret []int64
kw = "kw"
pn = 1
ps = 10
page *search.Pager
err error
esReq *elastic.Request
)
convey.Convey("SearchBgmSIDs", t, func(ctx convey.C) {
monkey.PatchInstanceMethod(reflect.TypeOf(esReq), "Scan", func(_ *elastic.Request, _ context.Context, _ interface{}) (err error) {
return ecode.CreativeSearchErr
})
ret, page, err = d.SearchBgmSIDs(c, kw, pn, ps)
ctx.Convey("SearchBgmSIDs", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeSearchErr)
ctx.So(ret, convey.ShouldBeNil)
ctx.So(page, convey.ShouldBeNil)
})
})
}
func TestExtAidsWithSameBgm(t *testing.T) {
var (
c = context.TODO()
sid = int64(87600)
total int
aids []int64
err error
esReq *elastic.Request
)
convey.Convey("ExtAidsWithSameBgm", t, func(ctx convey.C) {
monkey.PatchInstanceMethod(reflect.TypeOf(esReq), "Scan", func(_ *elastic.Request, _ context.Context, _ interface{}) (err error) {
return ecode.CreativeSearchErr
})
aids, total, err = d.ExtAidsWithSameBgm(c, sid, 1)
ctx.Convey("ExtAidsWithSameBgm err should be nil.res,resMap should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeSearchErr)
ctx.So(aids, convey.ShouldHaveLength, 0)
ctx.So(total, convey.ShouldNotBeNil)
})
})
}