twitch streaming check ok
This commit is contained in:
parent
f70d11517c
commit
f29f982f7f
@ -2,7 +2,8 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type FBGroups struct {
|
// FBGroup -
|
||||||
|
type FBGroup struct {
|
||||||
*LineGroup
|
*LineGroup
|
||||||
Tmpl string `db:"tmpl"`
|
Tmpl string `db:"tmpl"`
|
||||||
}
|
}
|
||||||
@ -13,7 +14,7 @@ type FacebookPage struct {
|
|||||||
LastPost string `db:"lastpost" cc:"lastpost"`
|
LastPost string `db:"lastpost" cc:"lastpost"`
|
||||||
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"`
|
||||||
Groups []*FBGroups `db:"-"`
|
Groups []*FBGroup `db:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllFacebookPage -
|
// GetAllFacebookPage -
|
||||||
@ -25,6 +26,7 @@ func GetAllFacebookPage() (pages []*FacebookPage, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdatePost -
|
||||||
func (p *FacebookPage) UpdatePost(postID string) (err error) {
|
func (p *FacebookPage) UpdatePost(postID string) (err error) {
|
||||||
query := `update "public"."facebook_page" set "lastpost" = $1 where id = $2`
|
query := `update "public"."facebook_page" set "lastpost" = $1 where id = $2`
|
||||||
_, err = x.Exec(query, postID, p.ID)
|
_, err = x.Exec(query, postID, p.ID)
|
||||||
@ -35,6 +37,7 @@ func (p *FacebookPage) UpdatePost(postID string) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetGroups -
|
||||||
func (p *FacebookPage) GetGroups() (err error) {
|
func (p *FacebookPage) GetGroups() (err error) {
|
||||||
query := `select g.*, rt.tmpl as tmpl from "public"."facebook_page" p
|
query := `select g.*, rt.tmpl as tmpl from "public"."facebook_page" p
|
||||||
left join "public"."line_fb_rt" rt
|
left join "public"."line_fb_rt" rt
|
||||||
|
@ -2,6 +2,12 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
// TwitchGroup -
|
||||||
|
type TwitchGroup struct {
|
||||||
|
*LineGroup
|
||||||
|
Tmpl string `db:"tmpl"`
|
||||||
|
}
|
||||||
|
|
||||||
// TwitchChannel - struct
|
// TwitchChannel - struct
|
||||||
type TwitchChannel struct {
|
type TwitchChannel struct {
|
||||||
ID string `db:"id" cc:"id"`
|
ID string `db:"id" cc:"id"`
|
||||||
@ -11,10 +17,35 @@ type TwitchChannel struct {
|
|||||||
OpayID string `db:"opayid" cc:"opayid"`
|
OpayID string `db:"opayid" cc:"opayid"`
|
||||||
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"`
|
||||||
|
Groups []*TwitchGroup `db:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllChannel -
|
// GetAllTwitchChannel -
|
||||||
func GetAllChannel() (channels []*TwitchChannel, err error) {
|
func GetAllTwitchChannel() (channels []*TwitchChannel, err error) {
|
||||||
err = x.Select(&channels, `select * from "public"."twitch_channel"`)
|
err = x.Select(&channels, `select * from "public"."twitch_channel"`)
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
@ -2,16 +2,16 @@ package background
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"git.trj.tw/golang/mtfosbot/module/utils"
|
|
||||||
|
|
||||||
"git.trj.tw/golang/mtfosbot/model"
|
"git.trj.tw/golang/mtfosbot/model"
|
||||||
|
"git.trj.tw/golang/mtfosbot/module/apis/line"
|
||||||
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getStreamStatus() {
|
func getStreamStatus() {
|
||||||
fmt.Println("run twitch check")
|
fmt.Println("run twitch check")
|
||||||
channels, err := model.GetAllChannel()
|
channels, err := model.GetAllTwitchChannel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -22,11 +22,45 @@ func getStreamStatus() {
|
|||||||
|
|
||||||
info := twitch.GetUserStreamStatus(ids)
|
info := twitch.GetUserStreamStatus(ids)
|
||||||
fmt.Printf("info len: %d\n", len(info))
|
fmt.Printf("info len: %d\n", len(info))
|
||||||
|
if len(info) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, v := range info {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user