From 69bcf858d377550ef7eeded3f67bbff79cf6971f Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 27 Sep 2018 21:43:02 +0800 Subject: [PATCH 1/8] add lottery --- model/lottery.go | 25 +++++++++++++++++++++++++ module/message-command/line-group.go | 26 ++++++++++++++++++++++++++ router/rimg/rimg.go | 25 ++++++++++++++++++++++--- schema | 2 +- 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 model/lottery.go diff --git a/model/lottery.go b/model/lottery.go new file mode 100644 index 0000000..c09e211 --- /dev/null +++ b/model/lottery.go @@ -0,0 +1,25 @@ +package model + +import ( + "database/sql" + "time" +) + +// Lottery - +type Lottery struct { + ID string `db:"id" cc:"id"` + Type string `db:"type" cc:"type"` + Message string `db:"message" cc:"message"` + Ctime time.Time `db:"ctime" cc:"ctime"` + Mtime time.Time `db:"mtime" cc:"mtime"` +} + +// GetRandomLotteryByType - +func GetRandomLotteryByType(t string) (p *Lottery, err error) { + p = &Lottery{} + err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order random() limit 1`, t) + if err == sql.ErrNoRows { + return nil, nil + } + return +} diff --git a/module/message-command/line-group.go b/module/message-command/line-group.go index 12e695d..6bd500d 100644 --- a/module/message-command/line-group.go +++ b/module/message-command/line-group.go @@ -6,6 +6,7 @@ import ( "strings" "git.trj.tw/golang/mtfosbot/module/apis/twitch" + "git.trj.tw/golang/mtfosbot/module/config" "git.trj.tw/golang/mtfosbot/model" googleapi "git.trj.tw/golang/mtfosbot/module/apis/google" @@ -26,6 +27,8 @@ func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) { return delTwitchChannel(sub, txt, s) case "image": return fmt.Sprintf("$image$%s", sub) + case "lottery": + return lottery(sub, txt, s) case "hello": return "World!!" } @@ -298,3 +301,26 @@ func addYoutubeChannel(sub, txt string, s *lineobj.SourceObject) (res string) { return "Success" } + +func lottery(sub, txt string, s *lineobj.SourceObject) (res string) { + if len(sub) == 0 { + return "" + } + data, err := model.GetRandomLotteryByType(sub) + if err != nil || data == nil { + return + } + conf := config.GetConf() + u := conf.URL + if last := len(u); last > 0 && u[last-1] == '/' { + u = u[:last] + } + oriURL := "/image/origin" + thumbURL := "/image/thumbnail" + if len(data.Message) == 0 { + return + } + o := u + oriURL + "/" + data.Message + "?d=" + sub + t := u + thumbURL + "/" + data.Message + "?d=" + sub + return fmt.Sprintf("$image$%s;%s", o, t) +} diff --git a/router/rimg/rimg.go b/router/rimg/rimg.go index cf4437d..5ed9e14 100644 --- a/router/rimg/rimg.go +++ b/router/rimg/rimg.go @@ -45,6 +45,16 @@ func GetOriginImage(c *context.Context) { return } + subd := c.DefaultQuery("d", "") + if len(subd) > 0 { + imgP = path.Join(imgP, subd) + exists = utils.CheckExists(imgP, true) + if !exists { + c.NotFound("image path not found") + return + } + } + newP := path.Join(imgP, fname) exists = utils.CheckExists(newP, false) if !exists { @@ -126,11 +136,20 @@ func GetThumbnailImage(c *context.Context) { return } - thumbP := path.Join(imgP, "thumbnail", fname) + thumbDir := "thumbnail" + subd := c.DefaultQuery("d", "") + if exists := utils.CheckExists(path.Join(imgP, thumbDir, subd), true); !exists { + if err := os.MkdirAll(path.Join(imgP, thumbDir, subd), 0775); err != nil { + c.ServerError(nil) + return + } + } + + thumbP := path.Join(imgP, thumbDir, subd, fname) genNew := false if !utils.CheckExists(thumbP, false) { genNew = true - thumbP = path.Join(imgP, fname) + thumbP = path.Join(imgP, subd, fname) exists = utils.CheckExists(thumbP, false) if !exists { c.NotFound("image file not found") @@ -174,7 +193,7 @@ func GetThumbnailImage(c *context.Context) { breader := bytes.NewReader(buf.Bytes()) if genNew { - savep := path.Join(conf.ImageRoot, "thumbnail", fname) + savep := path.Join(conf.ImageRoot, "thumbnail", subd, fname) err := saveNewThumbnail(breader, savep) if err != nil { c.ServerError(nil) diff --git a/schema b/schema index 33f8a1d..09deeff 160000 --- a/schema +++ b/schema @@ -1 +1 @@ -Subproject commit 33f8a1dc6ea0de9323e9a75f5366fa68d8325774 +Subproject commit 09deeff71694c8baa68e77dd8285e3a245d2d284 From 6436081c0cae466d51399a3f5f839cea9d6e8f0b Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 27 Sep 2018 21:46:34 +0800 Subject: [PATCH 2/8] update schema --- schema | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema b/schema index 09deeff..a6d5769 160000 --- a/schema +++ b/schema @@ -1 +1 @@ -Subproject commit 09deeff71694c8baa68e77dd8285e3a245d2d284 +Subproject commit a6d5769247f440df3c78995afc00d6b2b87385aa From da13bc9cd2851bd4dacb438b6e1741f20d471e61 Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 27 Sep 2018 21:48:34 +0800 Subject: [PATCH 3/8] fix schema --- schema | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema b/schema index a6d5769..265d85f 160000 --- a/schema +++ b/schema @@ -1 +1 @@ -Subproject commit a6d5769247f440df3c78995afc00d6b2b87385aa +Subproject commit 265d85f0d3249abb14ee8c925800d2371dd27265 From aaa463ac6e864abf5205eaa6228c4e93885f6042 Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 27 Sep 2018 21:53:57 +0800 Subject: [PATCH 4/8] add log --- module/message-command/line-group.go | 1 + 1 file changed, 1 insertion(+) diff --git a/module/message-command/line-group.go b/module/message-command/line-group.go index 6bd500d..6096ef0 100644 --- a/module/message-command/line-group.go +++ b/module/message-command/line-group.go @@ -14,6 +14,7 @@ import ( ) func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) { + fmt.Println("Try Run CMD :: ", cmd, sub) switch cmd { case "addgroup": return addLineGroup(sub, txt, s) From c233fa0cfbaa8cc92c6143cf405c635f16fe2e2a Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 27 Sep 2018 21:56:32 +0800 Subject: [PATCH 5/8] add more log --- module/message-command/line-group.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/module/message-command/line-group.go b/module/message-command/line-group.go index 6096ef0..76af684 100644 --- a/module/message-command/line-group.go +++ b/module/message-command/line-group.go @@ -304,10 +304,12 @@ func addYoutubeChannel(sub, txt string, s *lineobj.SourceObject) (res string) { } func lottery(sub, txt string, s *lineobj.SourceObject) (res string) { + fmt.Println("lottery input ", sub, txt) if len(sub) == 0 { return "" } data, err := model.GetRandomLotteryByType(sub) + fmt.Println("get data ::: ", data, err) if err != nil || data == nil { return } @@ -323,5 +325,6 @@ func lottery(sub, txt string, s *lineobj.SourceObject) (res string) { } o := u + oriURL + "/" + data.Message + "?d=" + sub t := u + thumbURL + "/" + data.Message + "?d=" + sub + fmt.Printf("$image$%s;%s\n", o, t) return fmt.Sprintf("$image$%s;%s", o, t) } From c6e0ada3f75cd4179a482ccf0d4e34038365a896 Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 27 Sep 2018 21:58:59 +0800 Subject: [PATCH 6/8] fix query error --- model/lottery.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/lottery.go b/model/lottery.go index c09e211..8cc459e 100644 --- a/model/lottery.go +++ b/model/lottery.go @@ -17,7 +17,7 @@ type Lottery struct { // GetRandomLotteryByType - func GetRandomLotteryByType(t string) (p *Lottery, err error) { p = &Lottery{} - err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order random() limit 1`, t) + err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order by random() limit 1`, t) if err == sql.ErrNoRows { return nil, nil } From e1d09bc77b7daa421ec3f366afca01a25a49b378 Mon Sep 17 00:00:00 2001 From: jay Date: Thu, 27 Sep 2018 22:07:55 +0800 Subject: [PATCH 7/8] remove debug log --- module/message-command/line-group.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/module/message-command/line-group.go b/module/message-command/line-group.go index 76af684..6bd500d 100644 --- a/module/message-command/line-group.go +++ b/module/message-command/line-group.go @@ -14,7 +14,6 @@ import ( ) func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) { - fmt.Println("Try Run CMD :: ", cmd, sub) switch cmd { case "addgroup": return addLineGroup(sub, txt, s) @@ -304,12 +303,10 @@ func addYoutubeChannel(sub, txt string, s *lineobj.SourceObject) (res string) { } func lottery(sub, txt string, s *lineobj.SourceObject) (res string) { - fmt.Println("lottery input ", sub, txt) if len(sub) == 0 { return "" } data, err := model.GetRandomLotteryByType(sub) - fmt.Println("get data ::: ", data, err) if err != nil || data == nil { return } @@ -325,6 +322,5 @@ func lottery(sub, txt string, s *lineobj.SourceObject) (res string) { } o := u + oriURL + "/" + data.Message + "?d=" + sub t := u + thumbURL + "/" + data.Message + "?d=" + sub - fmt.Printf("$image$%s;%s\n", o, t) return fmt.Sprintf("$image$%s;%s", o, t) } From aa1cbb9f041075f2ffbeda5349c41fcfd12a9956 Mon Sep 17 00:00:00 2001 From: jay Date: Fri, 28 Sep 2018 22:09:08 +0800 Subject: [PATCH 8/8] modify random query, update schema --- model/lottery.go | 4 +++- schema | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/model/lottery.go b/model/lottery.go index 8cc459e..557377d 100644 --- a/model/lottery.go +++ b/model/lottery.go @@ -2,6 +2,7 @@ package model import ( "database/sql" + "math/rand" "time" ) @@ -17,7 +18,8 @@ type Lottery struct { // GetRandomLotteryByType - func GetRandomLotteryByType(t string) (p *Lottery, err error) { p = &Lottery{} - err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order by random() limit 1`, t) + offset := rand.Intn(10) + err = x.Get(p, `select * from "public"."lottery" where "type" = $1 order by random() offset $2 limit 1`, t, offset) if err == sql.ErrNoRows { return nil, nil } diff --git a/schema b/schema index 265d85f..03ae87d 160000 --- a/schema +++ b/schema @@ -1 +1 @@ -Subproject commit 265d85f0d3249abb14ee8c925800d2371dd27265 +Subproject commit 03ae87dcfe0b20adca876c6ab40e83cedbe2af16