From 67eeab4ba1990b4c1b5913542f4bb0aaf771c3e9 Mon Sep 17 00:00:00 2001 From: jay Date: Mon, 17 Sep 2018 21:46:59 +0800 Subject: [PATCH] add opay background check --- model/opay_donate_list.go | 27 +++++++- module/background/opay.go | 105 +++++++++++++++++++++++++++++++- module/twitch-irc/twitch-irc.go | 26 +++++++- 3 files changed, 154 insertions(+), 4 deletions(-) diff --git a/model/opay_donate_list.go b/model/opay_donate_list.go index bf181b1..242a07a 100644 --- a/model/opay_donate_list.go +++ b/model/opay_donate_list.go @@ -1,6 +1,10 @@ package model -import "time" +import ( + "time" + + "github.com/jmoiron/sqlx" +) // OpayDonateList - struct type OpayDonateList struct { @@ -8,6 +12,27 @@ type OpayDonateList struct { DonateID string `db:"donate_id" cc:"donate_id"` Price int `db:"price" cc:"price"` Text string `db:"text" cc:"text"` + Name string `db:"name" cc:"name"` Ctime time.Time `db:"ctime" cc:"ctime"` Mtime time.Time `db:"mtime" cc:"ctime"` } + +// GetDonateListWithIDs - +func GetDonateListWithIDs(ids []string) (ls []*OpayDonateList, err error) { + if len(ids) == 0 { + return + } + s, i, err := sqlx.In(`select * from "public"."opay_donate_list" where "donate_id" in (?)`, ids) + if err != nil { + return + } + s = x.Rebind(s) + err = x.Select(&ls, s, i...) + return +} + +// InsertData - +func (p *OpayDonateList) InsertData() (err error) { + _, err = x.NamedExec(`insert into "public"."opay_donate_list" ("opayid", "donate_id", "price", "text", "name") values (:opayid, :donate_id, :price, :text, :name)`, p) + return +} diff --git a/module/background/opay.go b/module/background/opay.go index ff77987..f31745a 100644 --- a/module/background/opay.go +++ b/module/background/opay.go @@ -1,6 +1,14 @@ package background import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + + "git.trj.tw/golang/mtfosbot/module/twitch-irc" + "git.trj.tw/golang/mtfosbot/model" ) @@ -10,12 +18,107 @@ func checkOpay() { return } for _, v := range channels { - if len(v.OpayID) > 0 && v.Join { + if len(v.OpayID) > 0 && len(v.OpayID) == 32 { go getOpayData(v) } } } +type opayResp struct { + LstDonate []donateList `json:"lstDonate"` + Settings opaySetting `json:"settings"` +} + +type donateList struct { + DonateID string `json:"donateid"` + Name string `json:"name"` + Amount int `json:"amount"` + MSG string `json:"msg"` +} + +type opaySetting struct { + BGColor string `json:"BgColor"` + FontAnimate string `json:"FontAnimate"` + MSGTemplate string `json:"MsgTemplate"` +} + func getOpayData(ch *model.TwitchChannel) { + u := fmt.Sprintf("https://payment.opay.tw/Broadcaster/CheckDonate/%s", ch.OpayID) + + req, err := http.NewRequest("POST", u, strings.NewReader("{}")) + if err != nil { + return + } + req.Header.Add("Content-Type", "application/json") + req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + fmt.Println("Opay http response code ::: ", resp.StatusCode) + return + } + + if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") { + fmt.Println("Opay resp not json") + return + } + + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + return + } + oResp := opayResp{} + err = json.Unmarshal(bodyBytes, &oResp) + if err != nil { + return + } + + fmt.Println(oResp.LstDonate) + + if len(oResp.LstDonate) == 0 { + return + } + + var ids []string + for _, v := range oResp.LstDonate { + ids = append(ids, v.DonateID) + } + + donateList, err := model.GetDonateListWithIDs(ids) + if err != nil { + return + } + + if len(donateList) > 0 { + for i := 0; i < len(oResp.LstDonate); i++ { + for _, v := range donateList { + if v.DonateID == oResp.LstDonate[i].DonateID { + oResp.LstDonate[i].DonateID = "" + } + } + } + } + + for _, v := range oResp.LstDonate { + if len(v.DonateID) > 0 { + donateData := &model.OpayDonateList{ + OpayID: ch.OpayID, + DonateID: v.DonateID, + Price: v.Amount, + Text: v.MSG, + Name: v.Name, + } + err = donateData.InsertData() + if err == nil && ch.Join { + msg := fmt.Sprintf("/me 感謝 %s 贊助了 %d 元, %s", v.Name, v.Amount, v.MSG) + twitchirc.SendMessage(ch.Name, msg) + } + } + } } diff --git a/module/twitch-irc/twitch-irc.go b/module/twitch-irc/twitch-irc.go index bd74992..9d91afa 100644 --- a/module/twitch-irc/twitch-irc.go +++ b/module/twitch-irc/twitch-irc.go @@ -5,6 +5,8 @@ import ( "fmt" "time" + "git.trj.tw/golang/mtfosbot/model" + "gopkg.in/irc.v2" "git.trj.tw/golang/mtfosbot/module/config" @@ -28,6 +30,7 @@ func InitIRC() { channels = make([]string, 0) queue = NewQueue() runQueue() + ReJoin() config := irc.ClientConfig{ Nick: "mtfos", @@ -57,12 +60,26 @@ func SendMessage(ch, msg string) { Command: "PRIVMSG", Params: []string{ fmt.Sprintf("#%s", ch), - fmt.Sprintf(":%s", msg), + fmt.Sprintf("%s", msg), }, } queue.Add(m) } +// ReJoin - +func ReJoin() { + ch, err := model.GetAllTwitchChannel() + if err != nil { + return + } + LeaveAllChannel() + for _, v := range ch { + if v.Join { + JoinChannel(v.Name) + } + } +} + // JoinChannel - func JoinChannel(ch string) { if len(ch) == 0 { @@ -103,6 +120,9 @@ func LeaveChannel(ch string) { // LeaveAllChannel - func LeaveAllChannel() { + if len(channels) == 0 { + return + } for _, v := range channels { m := &MsgObj{ Command: "PART", @@ -118,7 +138,7 @@ func runQueue() { go func() { cnt := 0 for { - if !queue.IsEmpty() { + if !queue.IsEmpty() && client != nil { m := queue.Get() msg := &irc.Message{} msg.Command = m.Command @@ -142,6 +162,8 @@ func runQueue() { cnt++ if cnt > 1800 { // call rejoin + ReJoin() + cnt = 0 } time.Sleep(time.Second * 1) }