twitch streaming check ok

This commit is contained in:
Jay 2018-09-12 12:03:44 +08:00
parent f70d11517c
commit f29f982f7f
3 changed files with 90 additions and 22 deletions

View File

@ -2,7 +2,8 @@ package model
import "time"
type FBGroups struct {
// FBGroup -
type FBGroup struct {
*LineGroup
Tmpl string `db:"tmpl"`
}
@ -13,7 +14,7 @@ type FacebookPage struct {
LastPost string `db:"lastpost" cc:"lastpost"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
Groups []*FBGroups `db:"-"`
Groups []*FBGroup `db:"-"`
}
// GetAllFacebookPage -
@ -25,6 +26,7 @@ func GetAllFacebookPage() (pages []*FacebookPage, err error) {
return
}
// UpdatePost -
func (p *FacebookPage) UpdatePost(postID string) (err error) {
query := `update "public"."facebook_page" set "lastpost" = $1 where id = $2`
_, err = x.Exec(query, postID, p.ID)
@ -35,6 +37,7 @@ func (p *FacebookPage) UpdatePost(postID string) (err error) {
return
}
// GetGroups -
func (p *FacebookPage) GetGroups() (err error) {
query := `select g.*, rt.tmpl as tmpl from "public"."facebook_page" p
left join "public"."line_fb_rt" rt

View File

@ -2,6 +2,12 @@ package model
import "time"
// TwitchGroup -
type TwitchGroup struct {
*LineGroup
Tmpl string `db:"tmpl"`
}
// TwitchChannel - struct
type TwitchChannel struct {
ID string `db:"id" cc:"id"`
@ -11,10 +17,35 @@ type TwitchChannel struct {
OpayID string `db:"opayid" cc:"opayid"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
Groups []*TwitchGroup `db:"-"`
}
// GetAllChannel -
func GetAllChannel() (channels []*TwitchChannel, err error) {
// GetAllTwitchChannel -
func GetAllTwitchChannel() (channels []*TwitchChannel, err error) {
err = x.Select(&channels, `select * from "public"."twitch_channel"`)
return
}
// UpdateStream -
func (p *TwitchChannel) UpdateStream(streamID string) (err error) {
query := `update "public"."twitch_channel" set "laststream" = $1 where "id" = $2`
_, err = x.Exec(query, streamID, p.ID)
if err != nil {
return
}
p.LastStream = streamID
return
}
// GetGroups -
func (p *TwitchChannel) GetGroups() (err error) {
query := `select g.*, rt.tmpl as tmpl from "public"."twitch_channel" tw
left join "public"."line_twitch_rt" rt
on rt.twitch = tw.id
left join "public"."line_group" g
on g.id = rt.line
where
tw.id = $1`
err = x.Select(&p.Groups, query, p.ID)
return
}

View File

@ -2,16 +2,16 @@ package background
import (
"fmt"
"git.trj.tw/golang/mtfosbot/module/utils"
"strings"
"git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/apis/line"
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
)
func getStreamStatus() {
fmt.Println("run twitch check")
channels, err := model.GetAllChannel()
channels, err := model.GetAllTwitchChannel()
if err != nil {
return
}
@ -22,11 +22,45 @@ func getStreamStatus() {
info := twitch.GetUserStreamStatus(ids)
fmt.Printf("info len: %d\n", len(info))
if len(info) == 0 {
return
}
for _, v := range info {
fmt.Println(utils.ToMap(v))
for _, ch := range channels {
if v.UserID == ch.ID {
go checkStream(ch, v)
}
}
}
}
func checkStream(ch *model.TwitchChannel) {
func checkStream(ch *model.TwitchChannel, info *twitch.StreamInfo) {
if info.ID == ch.LastStream {
return
}
err := ch.GetGroups()
if err != nil {
return
}
err = ch.UpdateStream(info.ID)
if err != nil {
return
}
link := fmt.Sprintf("https://twitch.tv/%s", ch.Name)
for _, v := range ch.Groups {
if v.Notify {
tmpl := v.Tmpl
if len(tmpl) > 0 {
tmpl = strings.Replace(tmpl, "{txt}", info.Title, -1)
tmpl = strings.Replace(tmpl, "{link}", link, -1)
} else {
tmpl = fmt.Sprintf("%s\n%s", info.Title, link)
}
msg := line.TextMessage{
Text: tmpl,
}
line.PushMessage(v.ID, msg)
}
}
}