Merge pull request #79 from velour/fix_twitch

twitch: update to current API
This commit is contained in:
Chris Sexton 2017-09-28 22:39:29 -04:00 committed by GitHub
commit 5ded7a8f7b
4 changed files with 120 additions and 57 deletions

View File

@ -48,8 +48,10 @@ type Config struct {
Channels []string Channels []string
} }
Twitch struct { Twitch struct {
Freq int Freq int
Users map[string][]string //channel -> usernames Users map[string][]string //channel -> usernames
ClientID string
Authorization string
} }
EnforceNicks bool EnforceNicks bool
WelcomeMsgs []string WelcomeMsgs []string

View File

@ -23,6 +23,7 @@ import (
"github.com/velour/catbase/plugins/rss" "github.com/velour/catbase/plugins/rss"
"github.com/velour/catbase/plugins/stats" "github.com/velour/catbase/plugins/stats"
"github.com/velour/catbase/plugins/talker" "github.com/velour/catbase/plugins/talker"
"github.com/velour/catbase/plugins/twitch"
"github.com/velour/catbase/plugins/your" "github.com/velour/catbase/plugins/your"
"github.com/velour/catbase/plugins/zork" "github.com/velour/catbase/plugins/zork"
"github.com/velour/catbase/slack" "github.com/velour/catbase/slack"
@ -65,6 +66,7 @@ func main() {
b.AddHandler("rss", rss.New(b)) b.AddHandler("rss", rss.New(b))
b.AddHandler("reaction", reaction.New(b)) b.AddHandler("reaction", reaction.New(b))
b.AddHandler("emojifyme", emojifyme.New(b)) b.AddHandler("emojifyme", emojifyme.New(b))
b.AddHandler("twitch", twitch.New(b))
// catches anything left, will always return true // catches anything left, will always return true
b.AddHandler("factoid", fact.New(b)) b.AddHandler("factoid", fact.New(b))

View File

@ -2,9 +2,12 @@ package twitch
import ( import (
"encoding/json" "encoding/json"
"fmt"
"html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url"
"strings" "strings"
"time" "time"
@ -24,10 +27,22 @@ type Twitcher struct {
game string game string
} }
type Stream struct { type stream struct {
Stream struct { Data []struct {
Game string `json:game` ID string `json:"id"`
} `json:stream,omitempty` 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"`
} }
func New(bot bot.Bot) *TwitchPlugin { func New(bot bot.Bot) *TwitchPlugin {
@ -60,7 +75,40 @@ func (p *TwitchPlugin) BotMessage(message msg.Message) bool {
} }
func (p *TwitchPlugin) RegisterWeb() *string { func (p *TwitchPlugin) RegisterWeb() *string {
return nil http.HandleFunc("/isstreaming/", p.serveStreaming)
tmp := "/isstreaming"
return &tmp
}
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."
if twitcher.game != "" {
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)
}
} }
func (p *TwitchPlugin) Message(message msg.Message) bool { func (p *TwitchPlugin) Message(message msg.Message) bool {
@ -99,10 +147,6 @@ func (p *TwitchPlugin) twitchLoop(channel string) {
for { for {
time.Sleep(time.Duration(frequency) * time.Second) time.Sleep(time.Duration(frequency) * time.Second)
// auth := p.getTwitchStreams(channel)
// if !auth {
// p.Bot.SendMessage(channel, "OAuth for catbase twitch account has expired. Renew it!")
// }
for _, twitcherName := range p.config.Twitch.Users[channel] { for _, twitcherName := range p.config.Twitch.Users[channel] {
p.checkTwitch(channel, p.twitchList[twitcherName], false) p.checkTwitch(channel, p.twitchList[twitcherName], false)
@ -110,37 +154,66 @@ func (p *TwitchPlugin) twitchLoop(channel string) {
} }
} }
func getRequest(url string) ([]byte, bool) { func getRequest(url, clientID, authorization string) ([]byte, bool) {
resp, err := http.Get(url) var body []byte
var resp *http.Response
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
log.Println(err) goto errCase
return []byte{}, false
} }
body, err := ioutil.ReadAll(resp.Body) req.Header.Add("Client-ID", clientID)
req.Header.Add("Authorization", authorization)
resp, err = client.Do(req)
if err != nil { if err != nil {
log.Println(err) goto errCase
return []byte{}, false }
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
goto errCase
} }
return body, true return body, true
errCase:
log.Println(err)
return []byte{}, false
} }
func (p *TwitchPlugin) checkTwitch(channel string, twitcher *Twitcher, alwaysPrintStatus bool) { func (p *TwitchPlugin) checkTwitch(channel string, twitcher *Twitcher, alwaysPrintStatus bool) {
baseUrl := "https://api.twitch.tv/kraken/streams/" baseURL, err := url.Parse("https://api.twitch.tv/helix/streams")
if err != nil {
log.Println("Error parsing twitch stream URL")
return
}
body, ok := getRequest(baseUrl + twitcher.name) query := baseURL.Query()
query.Add("user_login", twitcher.name)
baseURL.RawQuery = query.Encode()
cid := p.config.Twitch.ClientID
auth := p.config.Twitch.Authorization
body, ok := getRequest(baseURL.String(), cid, auth)
if !ok { if !ok {
return return
} }
var stream Stream var s stream
err := json.Unmarshal(body, &stream) err = json.Unmarshal(body, &s)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
game := stream.Stream.Game games := s.Data
game := ""
if len(games) > 0 {
game = games[0].Title
}
if alwaysPrintStatus { if alwaysPrintStatus {
if game == "" { if game == "" {
p.Bot.SendMessage(channel, twitcher.name+" is not streaming.") p.Bot.SendMessage(channel, twitcher.name+" is not streaming.")
@ -159,37 +232,3 @@ func (p *TwitchPlugin) checkTwitch(channel string, twitcher *Twitcher, alwaysPri
twitcher.game = game twitcher.game = game
} }
} }
// func (p *TwitchPlugin) getTwitchStreams(channel string) bool {
// token := p.Bot.config.Twitch.Token
// if token == "" || token == "<Your Token>" {
// log.Println("No Twitch token, cannot enable plugin.")
// return false
// }
// access_token := "?access_token=" + token + "&scope=user_subscriptions"
// baseUrl := "https://api.twitch.tv/kraken/streams/followed"
// body, ok := getRequest(baseUrl + access_token)
// if !ok {
// return false
// }
// var subscriptions Subscriptions
// err := json.Unmarshal(body, &subscriptions)
// if err != nil {
// log.Println(err)
// return false
// }
// for _, subscription := range subscriptions.Follows {
// twitcherName := subscription.Channel.Name
// if _, ok := p.twitchList[twitcherName]; !ok {
// p.twitchList[twitcherName] = &Twitcher {
// name : twitcherName,
// game : "",
// }
// }
// }
// return true
// }

View File

@ -0,0 +1,20 @@
package twitch
var page = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Is {{.Name}} streaming?</title>
</head>
<body style="text-align: center; padding-top: 200px;">
<a style="font-weight: bold; font-size: 120pt;
font-family: Arial, sans-serif; text-decoration: none; color: black;"
title="{{.Status}}">{{.Status}}</a>
</body>
</html>
`