Initial commit

This commit is contained in:
Donny committed 2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 files changed
+4055000

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
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",
"media_test.go",
"style_cache_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/tv/conf:go_default_library",
"//app/interface/main/tv/model:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"media.go",
"style_cache.go",
],
importpath = "go-common/app/interface/main/tv/dao/pgc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/tv/conf:go_default_library",
"//app/interface/main/tv/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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"],
)
+24
View File
@@ -0,0 +1,24 @@
package pgc
import (
"go-common/app/interface/main/tv/conf"
"go-common/library/cache/memcache"
bm "go-common/library/net/http/blademaster"
)
// Dao is account dao.
type Dao struct {
conf *conf.Config
client *bm.Client
mc *memcache.Pool
}
// New account dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
conf: c,
client: bm.NewClient(c.HTTPClient),
mc: memcache.NewPool(c.Memcache.Config),
}
return
}
+45
View File
@@ -0,0 +1,45 @@
package pgc
import (
"context"
"flag"
"os"
"strings"
"go-common/app/interface/main/tv/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
ctx = context.Background()
)
func init() {
// dir, _ := filepath.Abs("../../cmd/tv-interface.toml")
// flag.Set("conf", dir)
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)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
d.client.SetTransport(gock.DefaultTransport)
return r
}
+38
View File
@@ -0,0 +1,38 @@
package pgc
import (
"context"
"go-common/app/interface/main/tv/model"
"go-common/library/log"
)
// Media gets the media detail data from PGC API
func (d *Dao) Media(ctx context.Context, tvParam *model.MediaParam) (detail *model.SeasonDetail, err error) {
var result model.MediaResp
if err = d.client.Get(ctx, d.conf.Host.APIMedia, "", tvParam.GenerateUrl(), &result); err != nil {
log.Error("ClientGet Sid %d, error[%v]", tvParam.SeasonID, err)
return
}
if err = result.CodeErr(); err != nil {
log.Error("PGC API MediaResp: [CODE:(%d),MESSAGE:(%s)]", result.Code, result.Message)
return
}
detail = result.Result
return
}
// MediaV2 gets the media detail data from PGC API V2
func (d *Dao) MediaV2(ctx context.Context, tvParam *model.MediaParam) (detail *model.SnDetailV2, err error) {
var result model.MediaRespV2
if err = d.client.Get(ctx, d.conf.Host.APIMediaV2, "", tvParam.GenerateUrl(), &result); err != nil {
log.Error("ClientGet Sid %d, error[%v]", tvParam.SeasonID, err)
return
}
if err = result.CodeErr(); err != nil {
log.Error("PGC API MediaResp: [CODE:(%d),MESSAGE:(%s)]", result.Code, result.Message)
return
}
detail = result.Result
return
}
@@ -0,0 +1,60 @@
package pgc
import (
"context"
"testing"
"go-common/app/interface/main/tv/model"
"go-common/library/ecode"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoMedia(t *testing.T) {
var (
ctx = context.Background()
tvParam = &model.MediaParam{
SeasonID: 296,
TrackPath: "1",
MobiAPP: "android_tv_yst",
Platform: "android",
Build: 101300,
}
normalStr = `{"code":0,"message":"success","result":{"cover":"http://i0.hdslb.com/bfs/bangumi/b2662854abfffc742a505853274425ac33bced24.jpg","dialog":{"btn_right":{"title":"成为大会员","type":"vip"},"desc":"","title":"开通大会员抢先看"},"episodes":[{"aid":10100146,"badge":"会员","badge_type":0,"cid":10114326,"cover":"http://i0.hdslb.com/bfs/archive/0e40e69bdc340e1c773f8813652935145ffd756c.jpg","duration":0,"ep_id":115281,"episode_status":13,"from":"bangumi","index":"大会员专享的","index_title":"大会员专享的","mid":452156,"page":1,"pub_real_time":"2018-08-24 20:27:00","section_id":23781,"section_type":0,"share_url":"https://m.bilibili.com/bangumi/play/ep115281","vid":""},{"aid":10110399,"badge":"会员","badge_type":0,"cid":10131912,"cover":"http://i0.hdslb.com/bfs/archive/0e40e69bdc340e1c773f8813652935145ffd756c.jpg","duration":0,"ep_id":117023,"episode_status":13,"from":"bangumi","index":"付费2","index_title":"付费2作为覆盖点","mid":27515256,"page":1,"pub_real_time":"2018-09-21 08:00:00","section_id":23781,"section_type":0,"share_url":"https://m.bilibili.com/bangumi/play/ep117023","vid":""},{"aid":10110399,"badge":"会员","badge_type":0,"cid":10131912,"cover":"http://i0.hdslb.com/bfs/archive/0e40e69bdc340e1c773f8813652935145ffd756c.jpg","duration":0,"ep_id":117024,"episode_status":13,"from":"bangumi","index":"付费3","index_title":"付费3作为测试覆盖点","mid":27515256,"page":1,"pub_real_time":"2018-09-20 08:00:00","section_id":23781,"section_type":0,"share_url":"https://m.bilibili.com/bangumi/play/ep117024","vid":""},{"aid":10110399,"badge":"会员","badge_type":0,"cid":10131912,"cover":"http://i0.hdslb.com/bfs/archive/0e40e69bdc340e1c773f8813652935145ffd756c.jpg","duration":0,"ep_id":117025,"episode_status":13,"from":"bangumi","index":"付费ep4","index_title":"付费ep4作为测试覆盖点","mid":27515256,"page":1,"pub_real_time":"2018-09-21 12:00:00","section_id":23781,"section_type":0,"share_url":"https://m.bilibili.com/bangumi/play/ep117025","vid":""}],"evaluate":"自动化:大会员专享纯付费ep,自动化测试使用番,请勿修改","is_new_danmaku":1,"link":"http://www.bilibili.com/bangumi/media/md2130684526/","media_id":2130684526,"mid":-1,"mode":2,"music_menus":[{"cover_url":"http://uat-i0.hdslb.com/bfs/static/a8f59108b204df22093dbc15d416a80bd8ea7249.jpg","intro":"","is_off":0,"is_pay":1,"menu_id":59054,"play_num":0,"title":"ff云の泣 - 青玉案(唱:云の泣 词:小鱼萝莉)"}],"newest_ep":{"desc":"连载中, 每周三更新","id":117025,"index":"付费ep4","is_new":0,"pub_real_time":"1537502400000"},"paster":{"aid":0,"allow_jump":0,"cid":0,"duration":0,"type":0,"url":""},"payment":{"pay_tip":{"primary":{"sub_title":"","title":"开通大会员抢先看","type":1,"url":""}},"pay_type":{"allow_discount":0,"allow_pack":0,"allow_ticket":0,"allow_time_limit":0,"allow_vip_discount":0},"price":"0.0","promotion":"","tip":"大会员专享观看特权哦~","vip_first_promotion":"","vip_first_switch":"1","vip_promotion":"开通大会员抢先看"},"player_icon":{"ctime":1540525201,"hash1":"4f688a632ce9dfe167ac3021ab9fb9f3","hash2":"ec06112f8b96d9c8d592593249d47c34","url1":"http://uat-i0.hdslb.com/bfs/archive/29cb6cf28b86c069ce7ef9aa051f84a1cb3d5148.json","url2":"http://uat-i0.hdslb.com/bfs/archive/737f375f10216213e203f807097bce03c211669d.json"},"publish":{"is_finish":0,"is_started":1,"pub_time":"2018-09-10 10:00:00","pub_time_show":"09月10日10:00","weekday":3},"record":"","rights":{"allow_bp":0,"allow_download":0,"allow_review":1,"area_limit":0,"ban_area_show":1,"copyright":"bilibili","is_preview":0},"season_id":20001,"season_status":13,"season_title":"全ep大会员专享","season_type":4,"seasons":[{"is_new":0,"season_id":33729,"season_title":"player专用的","title":"自动化:专门测试player的付费预览类,不要有任何修改,任何时候!"},{"is_new":0,"season_id":33382,"season_title":"免费仅大陆可看加iphone限制","title":"自动化:免费仅大陆可看加iphone限制,自动化测试使用番,请勿修改"},{"is_new":0,"season_id":33405,"season_title":"大会员&付费混杂ep","title":"自动化:大会员&付费混杂ep,自动化测试使用番,请勿修改"},{"is_new":0,"season_id":20001,"season_title":"全ep大会员专享","title":"自动化:大会员专享纯付费ep,自动化测试使用番,请勿修改"},{"is_new":0,"season_id":20009,"season_title":"付费&会员限时承包免费","title":"自动化:付费&会员,承包免费,请勿修改"},{"is_new":0,"season_id":33413,"season_title":"付费抢先限时","title":"专门测试player的国创-付费抢先限时"},{"is_new":0,"season_id":33427,"season_title":"霹雳付费","title":"自动化:霹雳付费,纯付费ep,自动化测试使用番,请勿修改"},{"is_new":0,"season_id":33406,"season_title":"免费但限制ipad平台","title":"自动化:免费仅亚洲非日本加ipad限制,自动化测试使用番,请勿修改"},{"is_new":0,"season_id":33801,"seLine truncated
httpErrStr = `{"code":-400}`
)
convey.Convey("Media", t, func(cx convey.C) {
cx.Convey("Then err should be nil.detail should not be nil.", func(cx convey.C) {
httpMock("GET", d.conf.Host.APIMedia).Reply(200).JSON(normalStr)
detail, err := d.Media(ctx, tvParam)
cx.So(err, convey.ShouldBeNil)
cx.So(detail, convey.ShouldNotBeNil)
})
cx.Convey("Http Err, err should not be nil", func(cx convey.C) {
httpMock("GET", d.conf.Host.APIMedia).Reply(200).JSON(httpErrStr)
_, err := d.Media(ctx, tvParam)
cx.So(err, convey.ShouldNotBeNil)
cx.So(err, convey.ShouldEqual, ecode.Int(-400))
httpMock("GET", d.conf.Host.APIMedia).Reply(-500).JSON(httpErrStr)
_, err = d.Media(ctx, tvParam)
cx.So(err, convey.ShouldNotBeNil)
})
})
convey.Convey("MediaV2", t, func(cx convey.C) {
cx.Convey("Then err should be nil.detail should not be nil.", func(cx convey.C) {
httpMock("GET", d.conf.Host.APIMediaV2).Reply(200).JSON(normalStr)
detail, err := d.MediaV2(ctx, tvParam)
cx.So(err, convey.ShouldBeNil)
cx.So(detail, convey.ShouldNotBeNil)
})
cx.Convey("Http Err, err should not be nil", func(cx convey.C) {
httpMock("GET", d.conf.Host.APIMediaV2).Reply(200).JSON(httpErrStr)
_, err := d.MediaV2(ctx, tvParam)
cx.So(err, convey.ShouldNotBeNil)
cx.So(err, convey.ShouldEqual, ecode.Int(-400))
httpMock("GET", d.conf.Host.APIMediaV2).Reply(-500).JSON(httpErrStr)
_, err = d.MediaV2(ctx, tvParam)
cx.So(err, convey.ShouldNotBeNil)
})
})
}
@@ -0,0 +1,36 @@
package pgc
import (
"context"
"go-common/app/interface/main/tv/model"
"go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_style = "style_label"
)
// GetLabelCache .
func (d *Dao) GetLabelCache(ctx context.Context) (res map[int64][]*model.ParamStyle, err error) {
var (
conn = d.mc.Get(ctx)
key = _style
rp *memcache.Item
)
res = make(map[int64][]*model.ParamStyle)
defer conn.Close()
if rp, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("mc.Get(%s) error(%v)", key, err)
}
return
}
if err = conn.Scan(rp, &res); err != nil {
log.Error("conn.Scan error(%v)", err)
}
return
}
@@ -0,0 +1,19 @@
package pgc
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetLabelCache(t *testing.T) {
convey.Convey("GetLabelCache", t, func(cx convey.C) {
cx.Convey("When everything goes positive", func(cx convey.C) {
res, err := d.GetLabelCache(ctx)
cx.Convey("Then err should be nil.res should not be nil.", func(cx convey.C) {
cx.So(err, convey.ShouldBeNil)
cx.So(res, convey.ShouldNotBeNil)
})
})
})
}