add opay background check

This commit is contained in:
Jay 2018-09-17 21:46:59 +08:00
parent cfc1e43192
commit 67eeab4ba1
3 changed files with 154 additions and 4 deletions

View File

@ -1,6 +1,10 @@
package model package model
import "time" import (
"time"
"github.com/jmoiron/sqlx"
)
// OpayDonateList - struct // OpayDonateList - struct
type OpayDonateList struct { type OpayDonateList struct {
@ -8,6 +12,27 @@ type OpayDonateList struct {
DonateID string `db:"donate_id" cc:"donate_id"` DonateID string `db:"donate_id" cc:"donate_id"`
Price int `db:"price" cc:"price"` Price int `db:"price" cc:"price"`
Text string `db:"text" cc:"text"` Text string `db:"text" cc:"text"`
Name string `db:"name" cc:"name"`
Ctime time.Time `db:"ctime" cc:"ctime"` Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" 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
}

View File

@ -1,6 +1,14 @@
package background package background
import ( import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"git.trj.tw/golang/mtfosbot/module/twitch-irc"
"git.trj.tw/golang/mtfosbot/model" "git.trj.tw/golang/mtfosbot/model"
) )
@ -10,12 +18,107 @@ func checkOpay() {
return return
} }
for _, v := range channels { for _, v := range channels {
if len(v.OpayID) > 0 && v.Join { if len(v.OpayID) > 0 && len(v.OpayID) == 32 {
go getOpayData(v) 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) { 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)
}
}
}
} }

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"time" "time"
"git.trj.tw/golang/mtfosbot/model"
"gopkg.in/irc.v2" "gopkg.in/irc.v2"
"git.trj.tw/golang/mtfosbot/module/config" "git.trj.tw/golang/mtfosbot/module/config"
@ -28,6 +30,7 @@ func InitIRC() {
channels = make([]string, 0) channels = make([]string, 0)
queue = NewQueue() queue = NewQueue()
runQueue() runQueue()
ReJoin()
config := irc.ClientConfig{ config := irc.ClientConfig{
Nick: "mtfos", Nick: "mtfos",
@ -57,12 +60,26 @@ func SendMessage(ch, msg string) {
Command: "PRIVMSG", Command: "PRIVMSG",
Params: []string{ Params: []string{
fmt.Sprintf("#%s", ch), fmt.Sprintf("#%s", ch),
fmt.Sprintf(":%s", msg), fmt.Sprintf("%s", msg),
}, },
} }
queue.Add(m) 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 - // JoinChannel -
func JoinChannel(ch string) { func JoinChannel(ch string) {
if len(ch) == 0 { if len(ch) == 0 {
@ -103,6 +120,9 @@ func LeaveChannel(ch string) {
// LeaveAllChannel - // LeaveAllChannel -
func LeaveAllChannel() { func LeaveAllChannel() {
if len(channels) == 0 {
return
}
for _, v := range channels { for _, v := range channels {
m := &MsgObj{ m := &MsgObj{
Command: "PART", Command: "PART",
@ -118,7 +138,7 @@ func runQueue() {
go func() { go func() {
cnt := 0 cnt := 0
for { for {
if !queue.IsEmpty() { if !queue.IsEmpty() && client != nil {
m := queue.Get() m := queue.Get()
msg := &irc.Message{} msg := &irc.Message{}
msg.Command = m.Command msg.Command = m.Command
@ -142,6 +162,8 @@ func runQueue() {
cnt++ cnt++
if cnt > 1800 { if cnt > 1800 {
// call rejoin // call rejoin
ReJoin()
cnt = 0
} }
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
} }