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,66 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"audio_playlist_test.go",
"audio_test.go",
"bangumi_test.go",
"credit_test.go",
"dao_test.go",
"drawyoo_test.go",
"dynamic_test.go",
"live_test.go",
"topic_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/reply/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"audio.go",
"audio_playlist.go",
"bangumi.go",
"credit.go",
"dao.go",
"drawyoo.go",
"dynamic.go",
"live.go",
"topic.go",
],
importpath = "go-common/app/job/main/reply/dao/notice",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/reply/conf: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"],
)

View File

@@ -0,0 +1,42 @@
package notice
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
)
const (
_urlAudio = "https://m.bilibili.com/audio/au%d"
)
type audio struct {
Title string `json:"title"`
}
// Audio is Audio
func (d *Dao) Audio(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
uri := d.urlAudio
params.Set("ids", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data map[int64]*audio `json:"data"`
}
if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
return
}
if res.Data == nil {
err = fmt.Errorf("url:%s code:%d", uri, res.Code)
return
}
if r := res.Data[oid]; r != nil {
title = r.Title
}
link = fmt.Sprintf(_urlAudio, oid)
return
}

View File

@@ -0,0 +1,36 @@
package notice
import (
"context"
"fmt"
"net/url"
"go-common/library/log"
)
const (
_urlAudioPlayList = "https://m.bilibili.com/audio/am%d"
)
// AudioPlayList is show AudioPlay list
func (d *Dao) AudioPlayList(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
uri := fmt.Sprintf(d.urlAudioPlaylist, oid)
var res struct {
Code int `json:"code"`
Data *struct {
Title string `json:"title"`
} `json:"data"`
}
if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
return
}
if res.Data == nil {
err = fmt.Errorf("url:%s code:%d", uri, res.Code)
return
}
title = res.Data.Title
link = fmt.Sprintf(_urlAudioPlayList, oid)
return
}

View File

@@ -0,0 +1,29 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNoticeAudioPlayList(t *testing.T) {
convey.Convey("AudioPlayList", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.AudioPlayList(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,29 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNoticeAudio(t *testing.T) {
convey.Convey("Audio", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.Audio(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,41 @@
package notice
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
)
type bangumi struct {
EpisodeID int64 `json:"episode_id,string"`
SeasonID int64 `json:"season_id"`
Title string `json:"title"`
IndexTitle string `json:"index_title"`
}
// Bangumi return link.
func (d *Dao) Bangumi(c context.Context, oid int64) (title, link string, epid int64, err error) {
params := url.Values{}
params.Set("aids", strconv.FormatInt(oid, 10))
params.Set("platform", "reply")
params.Set("build", "0")
var res struct {
Code int `json:"code"`
Result map[int64]*bangumi `json:"result"`
}
if err = d.httpClient.Get(c, d.urlBangumi, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlBangumi, params.Encode(), err)
return
}
if res.Code != 0 || res.Result == nil {
err = fmt.Errorf("url:%s?%s code:%d", d.urlBangumi, params.Encode(), res.Code)
return
}
if r := res.Result[oid]; r != nil {
epid = r.EpisodeID
}
return
}

View File

@@ -0,0 +1,30 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNoticeBangumi(t *testing.T) {
convey.Convey("Bangumi", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, epid, err := d.Bangumi(c, oid)
ctx.Convey("Then err should be nil.title,link,epid should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(epid, convey.ShouldNotBeNil)
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,88 @@
package notice
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
)
const (
_urlBan = "https://www.bilibili.com/blackroom/ban/%d"
_urlNotice = "https://www.bilibili.com/blackroom/notice/%d"
_urlCreditLink = "https://www.bilibili.com/judgement/case/%d"
)
type notice struct {
Title string `json:"title"`
}
type ban struct {
Title string `json:"punishTitle"`
}
type credit struct {
Title string `json:"punishTitle"`
}
// Credit return link.
func (d *Dao) Credit(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("ids", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data map[int64]*credit `json:"data"`
}
if err = d.httpClient.Get(c, d.urlCredit, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlCredit, params.Encode(), err)
return
}
if res.Code != 0 || res.Data == nil {
err = fmt.Errorf("url:%s?%s code:%d", d.urlCredit, params.Encode(), res.Code)
return
}
if r := res.Data[oid]; r != nil {
title = r.Title
}
link = fmt.Sprintf(_urlCreditLink, oid)
return
}
// Notice get blackromm notice info.
func (d *Dao) Notice(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("ids", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data map[int64]*notice `json:"data"`
}
if err = d.httpClient.Get(c, d.urlNotice, "", params, &res); err != nil {
log.Error("httpNotice(%s) error(%v)", d.urlNotice, err)
return
}
if r := res.Data[oid]; r != nil {
title = r.Title
}
link = fmt.Sprintf(_urlNotice, oid)
return
}
// Ban get blackroom ban info.
func (d *Dao) Ban(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("ids", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data map[int64]*ban `json:"data"`
}
if err = d.httpClient.Get(c, d.urlBan, "", params, &res); err != nil {
log.Error("httpNotice(%s) error(%v)", d.urlBan, err)
return
}
if r := res.Data[oid]; r != nil {
title = r.Title
}
link = fmt.Sprintf(_urlBan, oid)
return
}

View File

@@ -0,0 +1,71 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestCreditBan(t *testing.T) {
convey.Convey("Audio", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.Ban(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}
func TestNoticeCredit(t *testing.T) {
convey.Convey("Audio", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.Credit(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}
func TestNoticeCreditNotice(t *testing.T) {
convey.Convey("Audio", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.Notice(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,53 @@
package notice
import (
"go-common/app/job/main/reply/conf"
bm "go-common/library/net/http/blademaster"
)
// Dao activity dao.
type Dao struct {
c *conf.Config
urlLiveSmallVideo string
urlLiveActivity string
urlLiveNotice string
urlLivePicture string
urlCredit string
urlTopic string
urlActivity string
urlActivitySub string
urlDrwayoo string
urlDynamic string
urlNotice string
urlBan string
urlBangumi string
urlAudio string
urlAudioPlaylist string
httpClient *bm.Client
drawyooHTTPClient *bm.Client
}
// New new a dao and return.
func New(c *conf.Config) *Dao {
return &Dao{
c: c,
// http
urlLiveSmallVideo: c.Host.LiveVC + "/clip/v1/video/detail",
urlLiveActivity: c.Host.LiveAct + "/comment/v1/relation/get_by_id",
urlLiveNotice: c.Host.LiveVC + "/news/v1/notice/info",
urlLivePicture: c.Host.LiveVC + "/link_draw/v1/doc/detail",
urlCredit: c.Host.API + "/x/internal/credit/blocked/cases",
urlTopic: c.Host.Activity + "/activity/page/one/%d",
urlActivity: c.Host.Activity + "/activity/page/one/%d",
urlActivitySub: c.Host.Activity + "/activity/subject/url",
urlDrwayoo: c.Host.DrawYoo + "/api/pushS",
urlDynamic: c.Host.LiveVC + "/dynamic_repost/v0/dynamic_repost/ftch_rp_cont?dynamic_ids[]=%d",
urlNotice: c.Host.API + "/x/internal/credit/publish/infos",
urlBan: c.Host.API + "/x/internal/credit/blocked/infos",
urlBangumi: c.Host.Bangumi + "/api/inner/aid_episodes_v2",
urlAudio: c.Host.API + "/x/internal/v1/audio/songs/batch",
urlAudioPlaylist: c.Host.API + "/x/internal/v1/audio/menus/%d",
httpClient: bm.NewClient(c.HTTPClient),
drawyooHTTPClient: bm.NewClient(c.DrawyooHTTPClient),
}
}

View File

@@ -0,0 +1,34 @@
package notice
import (
"flag"
"go-common/app/job/main/reply/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.reply-job")
flag.Set("conf_token", "5deea0665f8a7670b22a719337a39c7d")
flag.Set("tree_id", "2123")
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/reply-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,35 @@
package notice
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
)
// Drawyoo return link.
func (d *Dao) Drawyoo(c context.Context, hid int64) (title, link string, err error) {
params := url.Values{}
params.Set("hid", strconv.FormatInt(hid, 10))
params.Set("act", "getHidInfo")
var res struct {
State int `json:"state"`
Data []*struct {
Title string `json:"title"`
Link string `json:"link"`
} `json:"data"`
}
if err = d.drawyooHTTPClient.Post(c, d.urlDrwayoo, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlDrwayoo, params.Encode(), err)
return
}
if len(res.Data) == 0 {
err = fmt.Errorf("url:%s code:%d", d.urlDrwayoo, res.State)
return
}
title = res.Data[0].Title
link = res.Data[0].Link
return
}

View File

@@ -0,0 +1,29 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNoticeDrawyoo(t *testing.T) {
convey.Convey("Drawyoo", t, func(ctx convey.C) {
var (
c = context.Background()
hid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.Drawyoo(c, hid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,43 @@
package notice
import (
"context"
"fmt"
"net/url"
"go-common/library/log"
)
const (
_dynamicLink = "https://t.bilibili.com/%d"
)
// Dynamic return link and content.
func (d *Dao) Dynamic(c context.Context, oid int64) (content, link string, err error) {
params := url.Values{}
uri := fmt.Sprintf(d.urlDynamic, oid)
var res struct {
Code int `json:"code"`
Data *struct {
Pairs []struct {
DynamicID int64 `json:"dynamic_id"`
Content string `json:"rp_cont"`
Type int32 `json:"type"`
} `json:"pairs"`
TotalCount int64 `json:"total_count"`
} `json:"data,omitempty"`
Message string `json:"message"`
}
if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
return
}
if res.Code != 0 || res.Data == nil || len(res.Data.Pairs) == 0 {
err = fmt.Errorf("get dynamic failed!url:%s?%s code:%d message:%s pairs:%v", uri, params.Encode(), res.Code, res.Message, res.Data.Pairs)
return
}
content = res.Data.Pairs[0].Content
link = fmt.Sprintf(_dynamicLink, oid)
return
}

View File

@@ -0,0 +1,29 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNoticeDynamic(t *testing.T) {
convey.Convey("Dynamic", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
content, link, err := d.Dynamic(c, oid)
ctx.Convey("Then err should be nil.content,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(content, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,117 @@
package notice
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
)
const (
_liveSmallVideoLink = "http://vc.bilibili.com/video/%d"
_liveNoticeLink = "http://link.bilibili.com/p/eden/news#/newsdetail?id=%d"
_livePictureLink = "http://h.bilibili.com/ywh/%d"
)
// LiveSmallVideo return link.
func (d *Dao) LiveSmallVideo(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("video_id", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data *struct {
Item *struct {
Description string `json:"description"`
} `json:"item"`
} `json:"data"`
}
if err = d.httpClient.Get(c, d.urlLiveSmallVideo, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLiveSmallVideo, params.Encode(), err)
return
}
if res.Code != 0 || res.Data == nil || res.Data.Item == nil {
err = fmt.Errorf("url:%s?%s code:%d", d.urlLiveSmallVideo, params.Encode(), res.Code)
return
}
title = res.Data.Item.Description
link = fmt.Sprintf(_liveSmallVideoLink, oid)
return
}
// LiveActivity return link.
func (d *Dao) LiveActivity(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("id", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data *struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"data"`
}
if err = d.httpClient.Get(c, d.urlLiveActivity, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLiveActivity, params.Encode(), err)
return
}
if res.Code != 0 || res.Data == nil {
err = fmt.Errorf("url:%s?%s code:%d", d.urlLiveActivity, params.Encode(), res.Code)
return
}
title = res.Data.Name
link = res.Data.URL
return
}
// LiveNotice return link.
func (d *Dao) LiveNotice(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("id", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data *struct {
Title string `json:"title"`
} `json:"data"`
}
if err = d.httpClient.Get(c, d.urlLiveNotice, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLiveNotice, params.Encode(), err)
return
}
if res.Code != 0 || res.Data == nil {
err = fmt.Errorf("url:%s?%s code:%d", d.urlLiveNotice, params.Encode(), res.Code)
return
}
title = res.Data.Title
link = fmt.Sprintf(_liveNoticeLink, oid)
return
}
// LivePicture return link.
func (d *Dao) LivePicture(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("doc_id", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data *struct {
Item *struct {
Title string `json:"title"`
Desc string `json:"description"`
} `json:"item"`
} `json:"data"`
}
if err = d.httpClient.Get(c, d.urlLivePicture, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlLivePicture, params.Encode(), err)
return
}
if res.Code != 0 || res.Data == nil || res.Data.Item == nil {
err = fmt.Errorf("url:%s?%s code:%d", d.urlLivePicture, params.Encode(), res.Code)
return
}
title = res.Data.Item.Title
if title == "" {
title = res.Data.Item.Desc
}
link = fmt.Sprintf(_livePictureLink, oid)
return
}

View File

@@ -0,0 +1,92 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNoticeLiveSmallVideo(t *testing.T) {
convey.Convey("LiveSmallVideo", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.LiveSmallVideo(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}
func TestNoticeLiveActivity(t *testing.T) {
convey.Convey("LiveActivity", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.LiveActivity(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}
func TestNoticeLiveNotice(t *testing.T) {
convey.Convey("LiveNotice", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.LiveNotice(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}
func TestNoticeLivePicture(t *testing.T) {
convey.Convey("LivePicture", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.LivePicture(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,90 @@
package notice
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
)
// Topic return topic link.
func (d *Dao) Topic(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
uri := fmt.Sprintf(d.urlTopic, oid)
var res struct {
Code int `json:"code"`
Data *struct {
Title string `json:"name"`
PCLink string `json:"pc_url"`
H5Link string `json:"h5_url"`
} `json:"data"`
}
if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
return
}
if res.Data == nil {
err = fmt.Errorf("url:%s code:%d", uri, res.Code)
return
}
title = res.Data.Title
link = res.Data.PCLink
if link == "" {
link = res.Data.H5Link
}
return
}
// Activity return topic link.
func (d *Dao) Activity(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
uri := fmt.Sprintf(d.urlActivity, oid)
var res struct {
Code int `json:"code"`
Data *struct {
Title string `json:"name"`
PCLink string `json:"pc_url"`
H5Link string `json:"h5_url"`
} `json:"data"`
}
if err = d.httpClient.Get(c, uri, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", uri, params.Encode(), err)
return
}
if res.Data == nil {
err = fmt.Errorf("url:%s code:%d", uri, res.Code)
return
}
title = res.Data.Title
link = res.Data.PCLink
if link == "" {
link = res.Data.H5Link
}
return
}
// ActivitySub return topic link.
func (d *Dao) ActivitySub(c context.Context, oid int64) (title, link string, err error) {
params := url.Values{}
params.Set("oid", strconv.FormatInt(oid, 10))
var res struct {
Code int `json:"code"`
Data *struct {
Title string `json:"name"`
Link string `json:"act_url"`
} `json:"data"`
}
if err = d.httpClient.Get(c, d.urlActivitySub, "", params, &res); err != nil {
log.Error("d.httpClient.Get(%s?%s) error(%v)", d.urlActivitySub, params.Encode(), err)
return
}
if res.Data == nil {
err = fmt.Errorf("url:%s code:%d", d.urlActivitySub, res.Code)
return
}
title = res.Data.Title
link = res.Data.Link
return
}

View File

@@ -0,0 +1,71 @@
package notice
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNoticeTopic(t *testing.T) {
convey.Convey("Topic", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.Topic(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}
func TestNoticeActivity(t *testing.T) {
convey.Convey("Activity", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.Activity(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}
func TestNoticeActivitySub(t *testing.T) {
convey.Convey("ActivitySub", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
title, link, err := d.ActivitySub(c, oid)
ctx.Convey("Then err should be nil.title,link should not be nil.", func(ctx convey.C) {
if err != nil {
ctx.So(err, convey.ShouldNotBeNil)
} else {
ctx.So(err, convey.ShouldBeNil)
}
ctx.So(link, convey.ShouldNotBeNil)
ctx.So(title, convey.ShouldNotBeNil)
})
})
})
}