twitch streaming check ok
This commit is contained in:
parent
f70d11517c
commit
f29f982f7f
@ -2,18 +2,19 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type FBGroups struct {
|
// FBGroup -
|
||||||
|
type FBGroup struct {
|
||||||
*LineGroup
|
*LineGroup
|
||||||
Tmpl string `db:"tmpl"`
|
Tmpl string `db:"tmpl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// FacebookPage - struct
|
// FacebookPage - struct
|
||||||
type FacebookPage struct {
|
type FacebookPage struct {
|
||||||
ID string `db:"id" cc:"id"`
|
ID string `db:"id" cc:"id"`
|
||||||
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,19 +2,50 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// TwitchChannel - struct
|
// TwitchGroup -
|
||||||
type TwitchChannel struct {
|
type TwitchGroup struct {
|
||||||
ID string `db:"id" cc:"id"`
|
*LineGroup
|
||||||
Name string `db:"name" cc:"name"`
|
Tmpl string `db:"tmpl"`
|
||||||
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"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllChannel -
|
// TwitchChannel - struct
|
||||||
func GetAllChannel() (channels []*TwitchChannel, err error) {
|
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"`)
|
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