twitch: add HTTP output

This commit is contained in:
cws 2017-09-27 16:29:04 -04:00
parent 403c6dae0a
commit 20c038d57c
2 changed files with 56 additions and 1 deletions

View File

@ -2,6 +2,8 @@ package twitch
import ( import (
"encoding/json" "encoding/json"
"fmt"
"html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -73,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 {

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>
`