Merge branch 'master' of https://git.trj.tw/golang/mtfosbot
This commit is contained in:
commit
7f9ade4332
27
model/lottery.go
Normal file
27
model/lottery.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"math/rand"
|
||||||
|
"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{}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
||||||
|
"git.trj.tw/golang/mtfosbot/module/config"
|
||||||
|
|
||||||
"git.trj.tw/golang/mtfosbot/model"
|
"git.trj.tw/golang/mtfosbot/model"
|
||||||
googleapi "git.trj.tw/golang/mtfosbot/module/apis/google"
|
googleapi "git.trj.tw/golang/mtfosbot/module/apis/google"
|
||||||
@ -30,6 +31,8 @@ func selectAct(cmd, sub, txt string, s *lineobj.SourceObject) (res string) {
|
|||||||
return addYoutubeChannel(sub, txt, s)
|
return addYoutubeChannel(sub, txt, s)
|
||||||
case "delyoutube":
|
case "delyoutube":
|
||||||
return delYoutubeChannel(sub, txt, s)
|
return delYoutubeChannel(sub, txt, s)
|
||||||
|
case "lottery":
|
||||||
|
return lottery(sub, txt, s)
|
||||||
case "hello":
|
case "hello":
|
||||||
return "World!!"
|
return "World!!"
|
||||||
}
|
}
|
||||||
@ -330,3 +333,26 @@ func delYoutubeChannel(sub, txt string, s *lineobj.SourceObject) (res string) {
|
|||||||
}
|
}
|
||||||
return "Success"
|
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)
|
||||||
|
}
|
||||||
|
@ -45,6 +45,16 @@ func GetOriginImage(c *context.Context) {
|
|||||||
return
|
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)
|
newP := path.Join(imgP, fname)
|
||||||
exists = utils.CheckExists(newP, false)
|
exists = utils.CheckExists(newP, false)
|
||||||
if !exists {
|
if !exists {
|
||||||
@ -126,11 +136,20 @@ func GetThumbnailImage(c *context.Context) {
|
|||||||
return
|
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
|
genNew := false
|
||||||
if !utils.CheckExists(thumbP, false) {
|
if !utils.CheckExists(thumbP, false) {
|
||||||
genNew = true
|
genNew = true
|
||||||
thumbP = path.Join(imgP, fname)
|
thumbP = path.Join(imgP, subd, fname)
|
||||||
exists = utils.CheckExists(thumbP, false)
|
exists = utils.CheckExists(thumbP, false)
|
||||||
if !exists {
|
if !exists {
|
||||||
c.NotFound("image file not found")
|
c.NotFound("image file not found")
|
||||||
@ -174,7 +193,7 @@ func GetThumbnailImage(c *context.Context) {
|
|||||||
breader := bytes.NewReader(buf.Bytes())
|
breader := bytes.NewReader(buf.Bytes())
|
||||||
|
|
||||||
if genNew {
|
if genNew {
|
||||||
savep := path.Join(conf.ImageRoot, "thumbnail", fname)
|
savep := path.Join(conf.ImageRoot, "thumbnail", subd, fname)
|
||||||
err := saveNewThumbnail(breader, savep)
|
err := saveNewThumbnail(breader, savep)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError(nil)
|
c.ServerError(nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user