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,58 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"call.go",
"dao.go",
"lic.go",
"xml.go",
],
importpath = "go-common/app/job/main/tv/dao/lic",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/tv/conf:go_default_library",
"//app/job/main/tv/model/pgc:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"lic_test.go",
"xml_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/tv/conf:go_default_library",
"//app/job/main/tv/model/pgc:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,63 @@
package lic
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"net/http"
"time"
model "go-common/app/job/main/tv/model/pgc"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
_normalCode = "0000"
)
func (d *Dao) callLic(c context.Context, url string, xmlBody string) (result *model.Document, err error) {
var resp []byte
result = &model.Document{}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(xmlBody)))
if err != nil {
log.Error("http.NewRequest err - %v", err)
return
}
req.Header.Add("Content-Type", "text/xml; charset=utf-8")
if resp, err = d.client.Raw(c, req); err != nil {
log.Error("ClientGet error[%v]", err)
return
}
if err = xml.Unmarshal(resp, result); err != nil {
log.Error("XML Unmarshal %s, Error %v", string(resp), err)
return
}
if result == nil || result.Response == nil {
err = errors.Wrap(err, "Response Empty Error")
return
}
if result.Response.ResponseCode != _normalCode {
err = fmt.Errorf("Response Code Error %s", result.Response.ResponseCode)
return
}
if result.Response.ErrorList != nil && result.Response.ErrorList.Error != nil {
log.Warn("Response Error %v", result.Response.ErrorList.Error)
}
return
}
// CallRetry retries the xml call
func (d *Dao) CallRetry(c context.Context, url string, xmlBody string) (result *model.Document, err error) {
log.Info("callLic URL: %s, Body %s", url, xmlBody)
for i := 0; i < 3; i++ {
result, err = d.callLic(c, url, xmlBody)
if err == nil {
break
}
time.Sleep(10 * time.Second) // 5 seconds gap for each retrial
}
return
}

View File

@@ -0,0 +1,21 @@
package lic
import (
"go-common/app/job/main/tv/conf"
httpx "go-common/library/net/http/blademaster"
)
// Dao dao.
type Dao struct {
conf *conf.Config
client *httpx.Client
}
// New create a instance of Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
conf: c,
client: httpx.NewClient(conf.Conf.HTTPClient),
}
return
}

View File

@@ -0,0 +1,56 @@
package lic
import (
"context"
"flag"
"path/filepath"
"testing"
"go-common/app/job/main/tv/conf"
"fmt"
. "github.com/smartystreets/goconvey/convey"
)
var (
ctx = context.Background()
d *Dao
xmlBody = `inputTime=20180606&sign=timer-import_BILIBILI&tId=UHZFmufgweRpWhqAzToFYMWtYuZhMKCU&xmlData=<?xmlversion="1.0"encoding="UTF-8"?><Serviceid="dataSync"><Head><TradeId>UHZFmufgweRpWhqAzToFYMWtYuZhMKCU</TradeId><Date>2018-06-06</Date><Count>1</Count></Head><Body><programSetList><programSet><programSetId>ugc10100044</programSetId><programSetName>drm</programSetName><programSetClass></programSetClass><programSetType></programSetType><programSetPoster>http://i1.hdslb.com/bfs/archive/diuren.png</programSetPoster><publishDate>2018-01-30</publishDate><copyright>bilibili</copyright><programCount>1</programCount><cREndDate>1970-01-01</cREndDate><definitionType>SD</definitionType><cpCode>BILIBILI</cpCode><payStatus>0</payStatus><primitiveName></primitiveName><alias></alias><zone></zone><leadingRole></leadingRole><programSetDesc>drm</programSetDesc><Staff></Staff><programList><program><programId>ugc10114149</programId><programName>1</programName><programPoster></programPoster><programLength>1448</programLength><publishDate>1970-01-01</publishDate><ifPreview>0</ifPreview><number>1</number><definitionType>SD</definitionType><playCount>0</playCount><drm>0</drm><programMediaList><programMedia><mediaId>ugc10114149</mediaId><playUrl>http://upos-hz-tvshenhe.acgvideo.com/upgcxcode/87/75/41057587/41057587-1-6.mp4</playUrl><definition>SD</definition><htmlUrl>http://upos-hz-tvshenhe.acgvideo.com/upgcxcode/87/75/41057587/41057587-1-6.mp4</htmlUrl></programMedia></programMediaList></program></programList></programSet></programSetList></Body></Service>`
)
func WithDao(f func(d *Dao)) func() {
return func() {
dir, _ := filepath.Abs("../../cmd/tv-job-test.toml")
flag.Set("conf", dir)
conf.Init()
if d == nil {
d = New(conf.Conf)
}
f(d)
}
}
func TestDelEpLic(t *testing.T) {
Convey("TestDao_CallRetry", t, WithDao(func(d *Dao) {
res := DelEpLic("ugc", "timer-import_BILIBILI", []int{10109083, 10109084})
So(len(res), ShouldBeGreaterThan, 0)
fmt.Println(res)
}))
}
func TestDao_CallRetry(t *testing.T) {
Convey("TestDao_CallRetry", t, WithDao(func(d *Dao) {
res, err := d.CallRetry(ctx, d.conf.Sync.API.AddURL, xmlBody)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
}))
}
func TestDao_CallLic(t *testing.T) {
Convey("TestDao_CallLic", t, WithDao(func(d *Dao) {
result, err := d.callLic(ctx, d.conf.Sync.API.AddURL, xmlBody)
So(err, ShouldBeNil)
So(result, ShouldNotBeNil)
}))
}

View File

@@ -0,0 +1,101 @@
package lic
import (
"encoding/xml"
"fmt"
model "go-common/app/job/main/tv/model/pgc"
"math/rand"
"net/url"
"time"
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
_serviceID = "dataSync"
)
// BuildLic builds the skeleton of a license
func BuildLic(sign string, ps []*model.PS, count int) *model.License {
var (
tid = RandStringBytesRmndr(32)
now = time.Now()
)
return &model.License{
TId: tid,
InputTime: now.Format("20060102"),
Sign: sign,
XMLData: &model.XMLData{
Service: &model.Service{
ID: _serviceID,
Head: &model.Head{
TradeID: tid,
Date: now.Format("2006-01-02"),
Count: count,
},
Body: &model.Body{
ProgramSetList: &model.PSList{
ProgramSet: ps,
},
},
},
},
}
}
// RandStringBytesRmndr generates an random string
func RandStringBytesRmndr(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
// DelLic creates the license message with only the Season ID, for deletion
func DelLic(sign string, prefix string, sid int64) *model.License {
var (
ps []*model.PS
programS = &model.PS{
ProgramSetID: fmt.Sprintf("%s%d", prefix, sid),
}
)
ps = append(ps, programS)
return BuildLic(sign, ps, 1)
}
// DelEpLic creates the license message with only the Ep IDs, for deletion
func DelEpLic(prefix string, sign string, delEps []int) string {
// message skeleton
var tid = RandStringBytesRmndr(32)
type Service struct {
ID string `xml:"id,attr"`
Head *model.Head
Body *model.DelBody `xml:"Body"`
}
Msg := &Service{
ID: _serviceID,
Head: &model.Head{
TradeID: tid,
Date: time.Now().Format("2006-01-02"),
Count: len(delEps),
},
Body: &model.DelBody{
ProgramList: &model.ProgramList{},
},
}
for _, v := range delEps {
pm := &model.Program{
ProgramID: fmt.Sprintf("%s%d", prefix, v),
}
Msg.Body.ProgramList.Program = append(Msg.Body.ProgramList.Program, pm)
}
// combine the xml message
xmlRes, _ := xml.MarshalIndent(Msg, " ", " ")
params := url.Values{}
params.Set("tId", tid)
params.Set("inputTime", time.Now().Format("20060102"))
params.Set("sign", sign)
body := params.Encode()
body = body + "&xmlData=<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + string(xmlRes)
return body
}

View File

@@ -0,0 +1,62 @@
package lic
import (
model "go-common/app/job/main/tv/model/pgc"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestLicBuildLic(t *testing.T) {
var (
sign = ""
ps = []*model.PS{}
count = int(0)
)
convey.Convey("BuildLic", t, func(ctx convey.C) {
p1 := BuildLic(sign, ps, count)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestLicRandStringBytesRmndr(t *testing.T) {
var (
n = int(0)
)
convey.Convey("RandStringBytesRmndr", t, func(ctx convey.C) {
p1 := RandStringBytesRmndr(n)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestLicDelLic(t *testing.T) {
var (
sign = ""
prefix = ""
sid = int64(0)
)
convey.Convey("DelLic", t, func(ctx convey.C) {
p1 := DelLic(sign, prefix, sid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestLicDelEpLic(t *testing.T) {
var (
prefix = ""
sign = ""
delEps = []int{}
)
convey.Convey("DelEpLic", t, func(ctx convey.C) {
p1 := DelEpLic(prefix, sign, delEps)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,20 @@
package lic
import (
"encoding/xml"
"net/url"
model "go-common/app/job/main/tv/model/pgc"
)
// PrepareXML combine the xml data to sync
func PrepareXML(v *model.License) (body string) {
xmlRes, _ := xml.MarshalIndent(v.XMLData.Service, " ", " ")
params := url.Values{}
params.Set("tId", v.TId)
params.Set("inputTime", v.InputTime)
params.Set("sign", v.Sign)
body = params.Encode()
body = body + "&xmlData=<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + string(xmlRes)
return body
}

View File

@@ -0,0 +1,22 @@
package lic
import (
"encoding/json"
"testing"
model "go-common/app/job/main/tv/model/pgc"
"github.com/smartystreets/goconvey/convey"
)
func TestLicPrepareXML(t *testing.T) {
licStr := `{"TId":"cd-IqonHtgOndMAywoPLezvXwYmUxcHJ","InputTime":"20180904","Sign":"timer-import_BILIBILI","XMLData":{"Service":{"ID":"dataSync","Head":{"TradeID":"cd-IqonHtgOndMAywoPLezvXwYmUxcHJ","Date":"2018-09-04","Count":0},"Body":{"ProgramSetList":{"ProgramSet":[{"ProgramSetID":"xds296","ProgramSetName":"我家浴缸的二三事-Test","ProgramSetClass":"基腐,日常,泡面,治愈","ProgramSetType":"电影","ProgramSetPoster":"http://i0.hdslb.com/bfs/bangumi/23fbb5ece1d3adb8700988c02e8e97f30bbfbf33.jpg","Portrait":"","Producer":"","PublishDate":"2014-10-06","Copyright":"bilibili","ProgramCount":13,"CREndData":"1970-01-01","DefinitionType":"SD","CpCode":"BILIBILI","PayStatus":0,"PrimitiveName":"オレん家のフロ事情","Alias":"我家浴室的二三事,我家浴室的现况,オレん家のフロ事情","Zone":"日本","LeadingRole":"若狭:梅原裕一郎\n龙己岛崎信长\n鹰巢铃木达央\n真木津田健次郎\n三国花江夏树\n霞木户衣吹\n阿比留川原庆久","ProgramSetDesc":"一个人无忧无虑独自生活的男子高中生龙己,某日救下了一位倒在河边的美青年,没想到这位青年竟然是人鱼!于是这位人鱼先生似乎很中意龙己的浴缸,变住了下来!基友和卖萌人鱼同居的故事开始上演!","Staff":"原作:いときち\n监督青井小夜\n演出青井小夜\n脚本绫奈由仁子\n系列构成绫奈由仁子\n角色设计羽田浩二\n色彩设计小鹿绘里\n摄影监督藤坂めぐみ\n美术监督永吉幸树\n音响监督小泉纪介\n编辑斋藤朱里\n动画制作旭PRODUCTION","ProgramList":{"Program":null}}]}}}}}`
license := &model.License{}
json.Unmarshal([]byte(licStr), &license)
convey.Convey("PrepareXML", t, func(ctx convey.C) {
body := PrepareXML(license)
ctx.Convey("Then body should not be nil.", func(ctx convey.C) {
ctx.So(body, convey.ShouldNotBeNil)
})
})
}