mirror of https://github.com/velour/catbase.git
twitch: update to current API
* Added OAuth abilities * Added full API spec for endpoint response * Use net/url to build request
This commit is contained in:
parent
0220b8f266
commit
403c6dae0a
|
@ -50,6 +50,8 @@ type Config struct {
|
||||||
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
|
||||||
|
|
2
main.go
2
main.go
|
@ -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))
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -24,10 +25,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 {
|
||||||
|
@ -99,10 +112,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 +119,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 +197,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
|
|
||||||
// }
|
|
||||||
|
|
Loading…
Reference in New Issue