From f29f982f7f820fed1a496f280c7ae6c8f25adfa1 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 12 Sep 2018 12:03:44 +0800 Subject: [PATCH] twitch streaming check ok --- model/facebook_page.go | 15 ++++++----- model/twitch_channel.go | 53 +++++++++++++++++++++++++++++-------- module/background/twitch.go | 44 ++++++++++++++++++++++++++---- 3 files changed, 90 insertions(+), 22 deletions(-) diff --git a/model/facebook_page.go b/model/facebook_page.go index 9db35d0..030f10c 100644 --- a/model/facebook_page.go +++ b/model/facebook_page.go @@ -2,18 +2,19 @@ package model import "time" -type FBGroups struct { +// FBGroup - +type FBGroup struct { *LineGroup Tmpl string `db:"tmpl"` } // FacebookPage - struct type FacebookPage struct { - ID string `db:"id" cc:"id"` - LastPost string `db:"lastpost" cc:"lastpost"` - Ctime time.Time `db:"ctime" cc:"ctime"` - Mtime time.Time `db:"mtime" cc:"ctime"` - Groups []*FBGroups `db:"-"` + ID string `db:"id" cc:"id"` + LastPost string `db:"lastpost" cc:"lastpost"` + Ctime time.Time `db:"ctime" cc:"ctime"` + Mtime time.Time `db:"mtime" cc:"ctime"` + 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 diff --git a/model/twitch_channel.go b/model/twitch_channel.go index 0771def..0587437 100644 --- a/model/twitch_channel.go +++ b/model/twitch_channel.go @@ -2,19 +2,50 @@ package model import "time" -// TwitchChannel - struct -type TwitchChannel struct { - ID string `db:"id" cc:"id"` - Name string `db:"name" cc:"name"` - LastStream string `db:"laststream" cc:"laststream"` - Join bool `db:"join" cc:"join"` - OpayID string `db:"opayid" cc:"opayid"` - Ctime time.Time `db:"ctime" cc:"ctime"` - Mtime time.Time `db:"mtime" cc:"ctime"` +// TwitchGroup - +type TwitchGroup struct { + *LineGroup + Tmpl string `db:"tmpl"` } -// GetAllChannel - -func GetAllChannel() (channels []*TwitchChannel, err error) { +// TwitchChannel - struct +type TwitchChannel struct { + ID string `db:"id" cc:"id"` + Name string `db:"name" cc:"name"` + LastStream string `db:"laststream" cc:"laststream"` + Join bool `db:"join" cc:"join"` + OpayID string `db:"opayid" cc:"opayid"` + Ctime time.Time `db:"ctime" cc:"ctime"` + Mtime time.Time `db:"mtime" cc:"ctime"` + Groups []*TwitchGroup `db:"-"` +} + +// 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 +} diff --git a/module/background/twitch.go b/module/background/twitch.go index 7a65838..60c5b12 100644 --- a/module/background/twitch.go +++ b/module/background/twitch.go @@ -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) + } + } }