2016-08-09 00:44:28 +00:00
|
|
|
package twitch
|
|
|
|
|
|
|
|
import (
|
2019-02-09 13:32:27 +00:00
|
|
|
"bytes"
|
2016-08-09 00:44:28 +00:00
|
|
|
"encoding/json"
|
2017-09-27 20:29:04 +00:00
|
|
|
"fmt"
|
2016-08-09 00:44:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2017-09-27 18:39:36 +00:00
|
|
|
"net/url"
|
2016-08-09 00:44:28 +00:00
|
|
|
"strings"
|
2019-02-09 13:32:27 +00:00
|
|
|
"text/template"
|
2016-08-09 00:44:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
2019-02-09 13:32:27 +00:00
|
|
|
const (
|
|
|
|
isStreamingTplFallback = "{{.Name}} is streaming {{.Game}} at {{.URL}}"
|
|
|
|
notStreamingTplFallback = "{{.Name}} is not streaming"
|
|
|
|
stoppedStreamingTplFallback = "{{.Name}} just stopped streaming"
|
|
|
|
)
|
|
|
|
|
2016-08-09 00:44:28 +00:00
|
|
|
type TwitchPlugin struct {
|
|
|
|
Bot bot.Bot
|
|
|
|
config *config.Config
|
|
|
|
twitchList map[string]*Twitcher
|
|
|
|
}
|
|
|
|
|
|
|
|
type Twitcher struct {
|
2019-02-06 02:13:35 +00:00
|
|
|
name string
|
|
|
|
gameID string
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 16:53:37 +00:00
|
|
|
func (t Twitcher) URL() string {
|
|
|
|
u, _ := url.Parse("https://twitch.tv/")
|
|
|
|
u2, _ := url.Parse(t.name)
|
|
|
|
return u.ResolveReference(u2).String()
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:39:36 +00:00
|
|
|
type stream struct {
|
|
|
|
Data []struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
GameID string `json:"game_id"`
|
|
|
|
CommunityIds []string `json:"community_ids"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
ViewerCount int `json:"viewer_count"`
|
|
|
|
StartedAt time.Time `json:"started_at"`
|
|
|
|
Language string `json:"language"`
|
|
|
|
ThumbnailURL string `json:"thumbnail_url"`
|
|
|
|
} `json:"data"`
|
|
|
|
Pagination struct {
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
} `json:"pagination"`
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *TwitchPlugin {
|
2016-08-09 00:44:28 +00:00
|
|
|
p := &TwitchPlugin{
|
2019-02-05 19:41:38 +00:00
|
|
|
Bot: b,
|
|
|
|
config: b.Config(),
|
2016-08-09 00:44:28 +00:00
|
|
|
twitchList: map[string]*Twitcher{},
|
|
|
|
}
|
|
|
|
|
2019-01-22 00:16:57 +00:00
|
|
|
for _, ch := range p.config.GetArray("Twitch.Channels", []string{}) {
|
|
|
|
for _, twitcherName := range p.config.GetArray("Twitch."+ch+".Users", []string{}) {
|
2016-08-09 00:44:28 +00:00
|
|
|
if _, ok := p.twitchList[twitcherName]; !ok {
|
|
|
|
p.twitchList[twitcherName] = &Twitcher{
|
2019-02-06 02:13:35 +00:00
|
|
|
name: twitcherName,
|
|
|
|
gameID: "",
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 20:21:26 +00:00
|
|
|
go p.twitchLoop(ch)
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(p, bot.Message, p.message)
|
2019-02-07 16:30:42 +00:00
|
|
|
p.registerWeb()
|
|
|
|
|
2016-08-09 00:44:28 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-02-07 16:30:42 +00:00
|
|
|
func (p *TwitchPlugin) registerWeb() {
|
2017-09-27 20:29:04 +00:00
|
|
|
http.HandleFunc("/isstreaming/", p.serveStreaming)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pathParts := strings.Split(r.URL.Path, "/")
|
|
|
|
if len(pathParts) != 3 {
|
|
|
|
fmt.Fprint(w, "User not found.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
twitcher := p.twitchList[pathParts[2]]
|
|
|
|
if twitcher == nil {
|
|
|
|
|
|
|
|
fmt.Fprint(w, "User not found.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
status := "NO."
|
2019-02-06 02:13:35 +00:00
|
|
|
if twitcher.gameID != "" {
|
2017-09-27 20:29:04 +00:00
|
|
|
status = "YES."
|
|
|
|
}
|
|
|
|
context := map[string]interface{}{"Name": twitcher.name, "Status": status}
|
|
|
|
|
|
|
|
t, err := template.New("streaming").Parse(page)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Could not parse template!", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, context)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Could not execute template!", err)
|
|
|
|
}
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func (p *TwitchPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2019-02-09 13:32:27 +00:00
|
|
|
body := strings.ToLower(message.Body)
|
|
|
|
if body == "twitch status" {
|
2016-08-09 00:44:28 +00:00
|
|
|
channel := message.Channel
|
2019-01-22 00:16:57 +00:00
|
|
|
if users := p.config.GetArray("Twitch."+channel+".Users", []string{}); len(users) > 0 {
|
2019-01-20 20:21:26 +00:00
|
|
|
for _, twitcherName := range users {
|
|
|
|
if _, ok := p.twitchList[twitcherName]; ok {
|
2016-08-09 00:44:28 +00:00
|
|
|
p.checkTwitch(channel, p.twitchList[twitcherName], true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-02-09 13:32:27 +00:00
|
|
|
} else if body == "reset twitch" {
|
|
|
|
p.config.Set("twitch.istpl", isStreamingTplFallback)
|
|
|
|
p.config.Set("twitch.nottpl", notStreamingTplFallback)
|
|
|
|
p.config.Set("twitch.stoppedtpl", stoppedStreamingTplFallback)
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func (p *TwitchPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2019-02-09 13:32:27 +00:00
|
|
|
msg := "You can set the templates for streams with\n"
|
|
|
|
msg += fmt.Sprintf("twitch.istpl (default: %s)\n", isStreamingTplFallback)
|
|
|
|
msg += fmt.Sprintf("twitch.nottpl (default: %s)\n", notStreamingTplFallback)
|
|
|
|
msg += fmt.Sprintf("twitch.stoppedtpl (default: %s)\n", stoppedStreamingTplFallback)
|
|
|
|
msg += "And you can ask who is streaming with `!twitch status`"
|
2019-02-05 19:41:38 +00:00
|
|
|
p.Bot.Send(bot.Message, message.Channel, msg)
|
|
|
|
return true
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TwitchPlugin) twitchLoop(channel string) {
|
2019-01-22 00:16:57 +00:00
|
|
|
frequency := p.config.GetInt("Twitch.Freq", 60)
|
2019-02-06 03:52:49 +00:00
|
|
|
if p.config.Get("twitch.clientid", "") == "" || p.config.Get("twitch.authorization", "") == "" {
|
|
|
|
log.Println("Disabling twitch autochecking.")
|
|
|
|
return
|
|
|
|
}
|
2016-08-09 00:44:28 +00:00
|
|
|
|
|
|
|
log.Println("Checking every ", frequency, " seconds")
|
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(frequency) * time.Second)
|
|
|
|
|
2019-01-22 00:16:57 +00:00
|
|
|
for _, twitcherName := range p.config.GetArray("Twitch."+channel+".Users", []string{}) {
|
2016-08-09 00:44:28 +00:00
|
|
|
p.checkTwitch(channel, p.twitchList[twitcherName], false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:39:36 +00:00
|
|
|
func getRequest(url, clientID, authorization string) ([]byte, bool) {
|
|
|
|
var body []byte
|
|
|
|
var resp *http.Response
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2016-08-09 00:44:28 +00:00
|
|
|
if err != nil {
|
2017-09-27 18:39:36 +00:00
|
|
|
goto errCase
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 18:39:36 +00:00
|
|
|
req.Header.Add("Client-ID", clientID)
|
|
|
|
req.Header.Add("Authorization", authorization)
|
|
|
|
|
|
|
|
resp, err = client.Do(req)
|
2016-08-09 00:44:28 +00:00
|
|
|
if err != nil {
|
2017-09-27 18:39:36 +00:00
|
|
|
goto errCase
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
goto errCase
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
return body, true
|
2017-09-27 18:39:36 +00:00
|
|
|
|
|
|
|
errCase:
|
|
|
|
log.Println(err)
|
|
|
|
return []byte{}, false
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TwitchPlugin) checkTwitch(channel string, twitcher *Twitcher, alwaysPrintStatus bool) {
|
2017-09-27 18:39:36 +00:00
|
|
|
baseURL, err := url.Parse("https://api.twitch.tv/helix/streams")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error parsing twitch stream URL")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query := baseURL.Query()
|
|
|
|
query.Add("user_login", twitcher.name)
|
|
|
|
|
|
|
|
baseURL.RawQuery = query.Encode()
|
2016-08-09 00:44:28 +00:00
|
|
|
|
2019-01-22 00:16:57 +00:00
|
|
|
cid := p.config.Get("Twitch.ClientID", "")
|
|
|
|
auth := p.config.Get("Twitch.Authorization", "")
|
|
|
|
if cid == auth && cid == "" {
|
|
|
|
log.Println("Twitch plugin not enabled.")
|
|
|
|
return
|
|
|
|
}
|
2017-09-27 18:39:36 +00:00
|
|
|
|
|
|
|
body, ok := getRequest(baseURL.String(), cid, auth)
|
2016-08-09 00:44:28 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:39:36 +00:00
|
|
|
var s stream
|
|
|
|
err = json.Unmarshal(body, &s)
|
2016-08-09 00:44:28 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:39:36 +00:00
|
|
|
games := s.Data
|
2019-02-06 02:13:35 +00:00
|
|
|
gameID, title := "", ""
|
2017-09-27 18:39:36 +00:00
|
|
|
if len(games) > 0 {
|
2019-02-06 02:13:35 +00:00
|
|
|
gameID = games[0].GameID
|
|
|
|
title = games[0].Title
|
2017-09-27 18:39:36 +00:00
|
|
|
}
|
2019-02-09 13:32:27 +00:00
|
|
|
|
|
|
|
notStreamingTpl := p.config.Get("Twitch.NotTpl", notStreamingTplFallback)
|
|
|
|
isStreamingTpl := p.config.Get("Twitch.IsTpl", isStreamingTplFallback)
|
|
|
|
stoppedStreamingTpl := p.config.Get("Twitch.StoppedTpl", stoppedStreamingTplFallback)
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
|
|
|
info := struct {
|
|
|
|
Name string
|
|
|
|
Game string
|
|
|
|
URL string
|
|
|
|
}{
|
|
|
|
twitcher.name,
|
|
|
|
title,
|
|
|
|
twitcher.URL(),
|
|
|
|
}
|
|
|
|
|
2016-08-09 00:44:28 +00:00
|
|
|
if alwaysPrintStatus {
|
2019-02-06 02:13:35 +00:00
|
|
|
if gameID == "" {
|
2019-02-09 13:32:27 +00:00
|
|
|
t, err := template.New("notStreaming").Parse(notStreamingTpl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
p.Bot.Send(bot.Message, channel, err)
|
|
|
|
t = template.Must(template.New("notStreaming").Parse(notStreamingTplFallback))
|
|
|
|
}
|
|
|
|
t.Execute(&buf, info)
|
|
|
|
p.Bot.Send(bot.Message, channel, buf.String())
|
2016-08-09 00:44:28 +00:00
|
|
|
} else {
|
2019-02-09 13:32:27 +00:00
|
|
|
t, err := template.New("isStreaming").Parse(isStreamingTpl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
p.Bot.Send(bot.Message, channel, err)
|
|
|
|
t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback))
|
|
|
|
}
|
|
|
|
t.Execute(&buf, info)
|
|
|
|
p.Bot.Send(bot.Message, channel, buf.String())
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
2019-02-06 02:13:35 +00:00
|
|
|
} else if gameID == "" {
|
|
|
|
if twitcher.gameID != "" {
|
2019-02-09 13:32:27 +00:00
|
|
|
t, err := template.New("stoppedStreaming").Parse(stoppedStreamingTpl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
p.Bot.Send(bot.Message, channel, err)
|
|
|
|
t = template.Must(template.New("stoppedStreaming").Parse(stoppedStreamingTplFallback))
|
|
|
|
}
|
|
|
|
t.Execute(&buf, info)
|
|
|
|
p.Bot.Send(bot.Message, channel, buf.String())
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
2019-02-06 02:13:35 +00:00
|
|
|
twitcher.gameID = ""
|
2016-08-09 00:44:28 +00:00
|
|
|
} else {
|
2019-02-06 02:13:35 +00:00
|
|
|
if twitcher.gameID != gameID {
|
2019-02-09 13:32:27 +00:00
|
|
|
t, err := template.New("isStreaming").Parse(isStreamingTpl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
p.Bot.Send(bot.Message, channel, err)
|
|
|
|
t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback))
|
|
|
|
}
|
|
|
|
t.Execute(&buf, info)
|
|
|
|
p.Bot.Send(bot.Message, channel, buf.String())
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
2019-02-06 02:13:35 +00:00
|
|
|
twitcher.gameID = gameID
|
2016-08-09 00:44:28 +00:00
|
|
|
}
|
|
|
|
}
|