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,53 @@
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"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-intl/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"grpc.go",
],
importpath = "go-common/app/interface/main/app-intl/dao/bangumi",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-intl/conf:go_default_library",
"//app/interface/main/app-intl/model/bangumi:go_default_library",
"//app/service/openplatform/pgc-season/api/grpc/season/v1:go_default_library",
"//library/ecode:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata: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,145 @@
package bangumi
import (
"context"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/app-intl/conf"
"go-common/app/interface/main/app-intl/model/bangumi"
seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
"go-common/library/ecode"
httpx "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_pgc = "/pgc/internal/season/appview"
_movie = "/internal_api/movie_aid_info"
_seasonidAidURL = "/api/inner/archive/seasonid2aid"
_card = "/pgc/internal/season/search/card"
)
// Dao is bangumi dao
type Dao struct {
client *httpx.Client
pgc string
movie string
seasonidAidURL string
card string
// grpc
rpcClient seasongrpc.SeasonClient
}
// New bangumi dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
client: httpx.NewClient(c.HTTPBangumi),
pgc: c.Host.APICo + _pgc,
movie: c.Host.Bangumi + _movie,
seasonidAidURL: c.Host.Bangumi + _seasonidAidURL,
card: c.Host.APICo + _card,
}
var err error
if d.rpcClient, err = seasongrpc.NewClient(nil); err != nil {
panic(errors.WithMessage(err, "panic by seasongrpc"))
}
return
}
// PGC bangumi Season .
func (d *Dao) PGC(c context.Context, aid, mid int64, build int, mobiApp, device string) (s *bangumi.Season, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("build", strconv.Itoa(build))
params.Set("device", device)
params.Set("mobi_app", mobiApp)
params.Set("platform", "Golang")
var res struct {
Code int `json:"code"`
Result *bangumi.Season `json:"result"`
}
if err = d.client.Get(c, d.pgc, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.pgc+"?"+params.Encode())
return
}
s = res.Result
return
}
// Movie bangumi Movie
func (d *Dao) Movie(c context.Context, aid, mid int64, build int, mobiApp, device string) (m *bangumi.Movie, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("id", strconv.FormatInt(aid, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("build", strconv.Itoa(build))
params.Set("device", device)
params.Set("mobi_app", mobiApp)
params.Set("platform", "Golang")
var res struct {
Code int `json:"code"`
Result *bangumi.Movie `json:"result"`
}
if err = d.client.Get(c, d.movie, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.movie+"?"+params.Encode())
return
}
m = res.Result
return
}
// SeasonidAid moive_id by aid
func (d *Dao) SeasonidAid(c context.Context, moiveID int64, now time.Time) (data map[int64]int64, err error) {
params := url.Values{}
params.Set("build", "app-api")
params.Set("platform", "Golang")
params.Set("season_id", strconv.FormatInt(moiveID, 10))
params.Set("season_type", "2")
var res struct {
Code int `json:"code"`
Result map[int64]int64 `json:"result"`
}
if err = d.client.Get(c, d.seasonidAidURL, "", params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.seasonidAidURL+"?"+params.Encode())
return
}
data = res.Result
return
}
// Card bangumi card.
func (d *Dao) Card(c context.Context, mid int64, sids []int64) (s map[string]*bangumi.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]*bangumi.Card `json:"result"`
}
if err = d.client.Get(c, d.card, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.card+"?"+params.Encode())
return
}
s = res.Result
return
}

View File

@@ -0,0 +1,72 @@
package bangumi
import (
"context"
"flag"
"os"
"testing"
"time"
"go-common/app/interface/main/app-intl/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-intl")
flag.Set("conf_token", "02007e8d0f77d31baee89acb5ce6d3ac")
flag.Set("tree_id", "64518")
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/app-intl-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestMovie(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.Movie(context.Background(), 1, 1, 1, "iphone", "phone")
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestPGC(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.PGC(context.Background(), 1, 1, 1, "iphone", "phone")
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}
func TestSeasonidAid(t *testing.T) {
Convey(t.Name(), t, func() {
_, err := d.SeasonidAid(context.Background(), 1, time.Now())
if err != nil {
t.Log(err)
}
err = nil
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,21 @@
package bangumi
import (
"context"
seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
"github.com/pkg/errors"
)
// CardsInfoReply pgc cards info
func (d *Dao) CardsInfoReply(c context.Context, seasonIds []int32) (res map[int32]*seasongrpc.CardInfoProto, err error) {
arg := &seasongrpc.SeasonInfoReq{SeasonIds: seasonIds}
info, err := d.rpcClient.Cards(c, arg)
if err != nil {
err = errors.Wrapf(err, "%v", arg)
return
}
res = info.Cards
return
}