commiting so zorch can do my work

This commit is contained in:
joeyak 2019-06-01 19:41:28 -04:00
parent d153c2b4f0
commit 7962fad02e
10 changed files with 252 additions and 180 deletions

View File

@ -439,7 +439,7 @@ var commands = &CommandControl{
HelpText: "Reload the emotes on the server.", HelpText: "Reload the emotes on the server.",
Function: func(cl *Client, args []string) (string, error) { Function: func(cl *Client, args []string) (string, error) {
cl.SendServerMessage("Reloading emotes") cl.SendServerMessage("Reloading emotes")
num, err := common.LoadEmotes() err := loadEmotes()
if err != nil { if err != nil {
common.LogErrorf("Unbale to reload emotes: %s\n", err) common.LogErrorf("Unbale to reload emotes: %s\n", err)
return "", err return "", err
@ -447,6 +447,8 @@ var commands = &CommandControl{
cl.belongsTo.AddChatMsg(common.NewChatHiddenMessage(common.CdEmote, common.Emotes)) cl.belongsTo.AddChatMsg(common.NewChatHiddenMessage(common.CdEmote, common.Emotes))
cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes") cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes")
num := len(Emotes)
common.LogInfof("Loaded %d emotes\n", num) common.LogInfof("Loaded %d emotes\n", num)
return fmt.Sprintf("Emotes loaded: %d", num), nil return fmt.Sprintf("Emotes loaded: %d", num), nil
}, },
@ -525,7 +527,7 @@ var commands = &CommandControl{
go func() { go func() {
// Pretty sure this breaks on partial downloads (eg, one good channel and one non-existent) // Pretty sure this breaks on partial downloads (eg, one good channel and one non-existent)
_, err := GetEmotes(args) err := getEmotes(args)
if err != nil { if err != nil {
cl.SendChatData(common.NewChatMessage("", "", cl.SendChatData(common.NewChatMessage("", "",
err.Error(), err.Error(),
@ -533,8 +535,13 @@ var commands = &CommandControl{
return return
} }
// If the emotes were able to be downloaded, add the channels to settings
settingsMtx.Lock()
settings.ApprovedEmotes = append(settings.ApprovedEmotes, args...)
settingsMtx.Unlock()
// reload emotes now that new ones were added // reload emotes now that new ones were added
_, err = common.LoadEmotes() err = loadEmotes()
if err != nil { if err != nil {
cl.SendChatData(common.NewChatMessage("", "", cl.SendChatData(common.NewChatMessage("", "",
err.Error(), err.Error(),

View File

@ -36,11 +36,11 @@ func newChatRoom() (*ChatRoom, error) {
clients: []*Client{}, clients: []*Client{},
} }
num, err := common.LoadEmotes() err := loadEmotes()
if err != nil { if err != nil {
return nil, fmt.Errorf("error loading emotes: %s", err) return nil, fmt.Errorf("error loading emotes: %s", err)
} }
common.LogInfof("Loaded %d emotes\n", num) common.LogInfof("Loaded %d emotes\n", len(Emotes))
//the "heartbeat" for broadcasting messages //the "heartbeat" for broadcasting messages
go cr.Broadcast() go cr.Broadcast()

View File

@ -2,14 +2,23 @@ package common
import ( import (
"fmt" "fmt"
"path/filepath" "path"
"strings" "strings"
) )
var Emotes map[string]string var Emotes map[string]EmotePath
type EmotePath struct {
Dir string
File string
}
func (e EmotePath) path() string {
return path.Join(e.Dir, e.File)
}
func EmoteToHtml(file, title string) string { func EmoteToHtml(file, title string) string {
return fmt.Sprintf(`<img src="/emotes/%s" height="28px" title="%s" />`, file, title) return fmt.Sprintf(`<img src="%s" height="28px" title="%s" />`, file, title)
} }
func ParseEmotesArray(words []string) []string { func ParseEmotesArray(words []string) []string {
@ -21,7 +30,7 @@ func ParseEmotesArray(words []string) []string {
found := false found := false
for key, val := range Emotes { for key, val := range Emotes {
if key == wordTrimmed { if key == wordTrimmed {
newWords = append(newWords, EmoteToHtml(val, key)) newWords = append(newWords, EmoteToHtml(val.File, key))
found = true found = true
} }
} }
@ -36,31 +45,3 @@ func ParseEmotes(msg string) string {
words := ParseEmotesArray(strings.Split(msg, " ")) words := ParseEmotesArray(strings.Split(msg, " "))
return strings.Join(words, " ") return strings.Join(words, " ")
} }
func LoadEmotes() (int, error) {
newEmotes := map[string]string{}
emotePNGs, err := filepath.Glob("./static/emotes/*.png")
if err != nil {
return 0, fmt.Errorf("unable to glob emote directory: %s\n", err)
}
emoteGIFs, err := filepath.Glob("./static/emotes/*.gif")
if err != nil {
return 0, fmt.Errorf("unable to glob emote directory: %s\n", err)
}
globbed_files := []string(emotePNGs)
globbed_files = append(globbed_files, emoteGIFs...)
LogInfoln("Loading emotes...")
emInfo := []string{}
for _, file := range globbed_files {
file = filepath.Base(file)
key := file[0 : len(file)-4]
newEmotes[key] = file
emInfo = append(emInfo, key)
}
Emotes = newEmotes
LogInfoln(strings.Join(emInfo, " "))
return len(Emotes), nil
}

View File

@ -1,7 +1,6 @@
package common package common
import ( import (
"os"
"testing" "testing"
) )
@ -26,12 +25,21 @@ var data_good = map[string]string{
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
Emotes = map[string]string{ Emotes = map[string]EmotePath{
"one": "one.png", "one": EmotePath{
"two": "two.png", Dir: "",
"three": "three.gif", File: "one.png",
},
"two": EmotePath{
Dir: "",
File: "two.png",
},
"three": EmotePath{
Dir: "",
File: "three.gif",
},
} }
os.Exit(m.Run()) // os.Exit(m.Run())
} }
func TestEmotes_ParseEmotes(t *testing.T) { func TestEmotes_ParseEmotes(t *testing.T) {

318
emotes.go
View File

@ -4,164 +4,224 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"os" "os"
"path"
"strings" "strings"
"github.com/zorchenhimer/MovieNight/common" "github.com/pkg/errors"
) )
type twitchChannel struct { const emoteDir = "./static/emotes/"
ChannelName string `json:"channel_name"`
DisplayName string `json:"display_name"` var Emotes map[string]Emote
ChannelId string `json:"channel_id"`
BroadcasterType string `json:"broadcaster_type"` type Emote struct {
Plans map[string]string `json:"plans"` Dir string
Emotes []struct { File string
Code string `json:"code"`
Set int `json:"emoticon_set"`
Id int `json:"id"`
} `json:"emotes"`
BaseSetId string `json:"base_set_id"`
GeneratedAt string `json:"generated_at"`
} }
// Used in settings func (e Emote) path() string {
type EmoteSet struct { return path.Join(e.Dir, e.File)
Channel string // channel name
Prefix string // emote prefix
Found bool `json:"-"`
} }
const subscriberJson string = `subscribers.json` type TwitchUser struct {
ID string
Login string
}
// Download a single channel's emote set type EmoteInfo struct {
func (tc *twitchChannel) downloadEmotes() (*EmoteSet, error) { ID int
es := &EmoteSet{Channel: strings.ToLower(tc.ChannelName)} Code string
for _, emote := range tc.Emotes { }
url := fmt.Sprintf(`https://static-cdn.jtvnw.net/emoticons/v1/%d/1.0`, emote.Id)
png := `static/emotes/` + emote.Code + `.png`
if len(es.Prefix) == 0 { // func loadEmotes() error {
// For each letter // newEmotes := map[string]string{}
for i := 0; i < len(emote.Code); i++ {
// Find the first capital // emotePNGs, err := filepath.Glob("./static/emotes/*.png")
b := emote.Code[i] // if err != nil {
if b >= 'A' && b <= 'Z' { // return 0, fmt.Errorf("unable to glob emote directory: %s\n", err)
es.Prefix = emote.Code[0 : i-1] // }
common.LogDebugf("Found prefix for channel %q: %q (%q)\n", es.Channel, es.Prefix, emote)
break // emoteGIFs, err := filepath.Glob("./static/emotes/*.gif")
// if err != nil {
// return 0, fmt.Errorf("unable to glob emote directory: %s\n", err)
// }
// globbed_files := []string(emotePNGs)
// globbed_files = append(globbed_files, emoteGIFs...)
// LogInfoln("Loading emotes...")
// emInfo := []string{}
// for _, file := range globbed_files {
// file = filepath.Base(file)
// key := file[0 : len(file)-4]
// newEmotes[key] = file
// emInfo = append(emInfo, key)
// }
// Emotes = newEmotes
// LogInfoln(strings.Join(emInfo, " "))
// return len(Emotes), nil
// }
func loadEmotes() error {
fmt.Println(processEmoteDir(emoteDir))
return nil
}
func processEmoteDir(path string) ([]Emote, error) {
dir, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "could not open emoteDir:")
}
files, err := dir.Readdir(0)
if err != nil {
return nil, errors.Wrap(err, "could not get files:")
}
var emotes []Emote
for _, file := range files {
emotes = append(emotes, Emote{Dir: path, File: file.Name()})
}
subdir, err := dir.Readdirnames(0)
if err != nil {
return nil, errors.Wrap(err, "could not get sub directories:")
}
for _, d := range subdir {
subEmotes, err := processEmoteDir(d)
if err != nil {
return nil, errors.Wrapf(err, "could not process sub directory \"%s\":", d)
}
emotes = append(emotes, subEmotes...)
}
return emotes, nil
}
func getEmotes(names []string) error {
users := getUserIDs(names)
users = append(users, TwitchUser{ID: "0", Login: "global"})
for _, user := range users {
emotes, cheers, err := getChannelEmotes(user.ID)
if err != nil {
return errors.Wrapf(err, "could not get emote data for \"%s\"", user.ID)
}
emoteUserDir := path.Join(emoteDir, "twitch", user.Login)
if _, err := os.Stat(emoteUserDir); os.IsNotExist(err) {
os.MkdirAll(emoteUserDir, os.ModePerm)
}
for _, emote := range emotes {
if !strings.ContainsAny(emote.Code, `:;\[]|?&`) {
filePath := path.Join(emoteUserDir, emote.Code+".png")
file, err := os.Create(filePath)
if err != nil {
return errors.Wrapf(err, "could not create emote file in path \"%s\":", filePath)
}
err = downloadEmote(emote.ID, file)
if err != nil {
return errors.Wrapf(err, "could not download emote %s:", emote.Code)
} }
} }
} }
resp, err := http.Get(url) for amount, sizes := range cheers {
if err != nil { name := fmt.Sprintf("%sCheer%s.gif", user.Login, amount)
return nil, err filePath := path.Join(emoteUserDir, name)
} file, err := os.Create(filePath)
if err != nil {
return errors.Wrapf(err, "could not create emote file in path \"%s\":", filePath)
}
f, err := os.Create(png) err = downloadCheerEmote(sizes["4"], file)
if err != nil { if err != nil {
return nil, err return errors.Wrapf(err, "could not download emote %s:", name)
}
_, err = io.Copy(f, resp.Body)
if err != nil {
return nil, err
}
}
return es, nil
}
func GetEmotes(names []string) ([]*EmoteSet, error) {
// Do this up-front
for i := 0; i < len(names); i++ {
names[i] = strings.ToLower(names[i])
}
channels, err := findChannels(names)
if err != nil {
return nil, fmt.Errorf("Error reading %q: %v", subscriberJson, err)
}
emoteSets := []*EmoteSet{}
for _, c := range channels {
es, err := c.downloadEmotes()
if err != nil {
return nil, fmt.Errorf("Error downloading emotes: %v", err)
}
emoteSets = append(emoteSets, es)
}
for _, es := range emoteSets {
found := false
for _, name := range names {
if es.Channel == name {
found = true
break
} }
} }
if !found {
es.Found = false
}
} }
return nil
return emoteSets, nil
} }
func findChannels(names []string) ([]twitchChannel, error) { func getUserIDs(names []string) []TwitchUser {
file, err := os.Open(subscriberJson) logins := strings.Join(names, "&login=")
request, err := http.NewRequest("GET", fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", logins), nil)
if err != nil { if err != nil {
return nil, err log.Fatalln("Error generating new request:", err)
} }
defer file.Close() request.Header.Set("Client-ID", settings.TwitchClientID)
data := []twitchChannel{} client := http.Client{}
dec := json.NewDecoder(file) resp, err := client.Do(request)
// Open bracket
_, err = dec.Token()
if err != nil { if err != nil {
return nil, err log.Fatalln("Error sending request:", err)
} }
done := false decoder := json.NewDecoder(resp.Body)
for dec.More() && !done { type userResponse struct {
// opening bracket of channel Data []TwitchUser
_, err = dec.Token() }
if err != nil { var data userResponse
return nil, err
}
// Decode the channel stuff err = decoder.Decode(&data)
var c twitchChannel if err != nil {
err = dec.Decode(&c) log.Fatalln("Error decoding data:", err)
if err != nil {
return nil, err
}
// Is this a channel we are looking for?
found := false
for _, search := range names {
if strings.ToLower(c.ChannelName) == search {
found = true
break
}
}
// Yes it is. Add it to the data
if found {
data = append(data, c)
}
// Check for completion. Don't bother parsing the rest of
// the json file if we've already found everything that we're
// looking for.
if len(data) == len(names) {
done = true
}
} }
return data, nil return data.Data
}
func getChannelEmotes(ID string) ([]EmoteInfo, map[string]map[string]string, error) {
resp, err := http.Get("https://api.twitchemotes.com/api/v4/channels/" + ID)
if err != nil {
return nil, nil, errors.Wrap(err, "could not get emotes")
}
decoder := json.NewDecoder(resp.Body)
type EmoteResponse struct {
Emotes []EmoteInfo
Cheermotes map[string]map[string]string
}
var data EmoteResponse
err = decoder.Decode(&data)
if err != nil {
return nil, nil, errors.Wrap(err, "could not decode emotes")
}
return data.Emotes, data.Cheermotes, nil
}
func downloadEmote(ID int, file *os.File) error {
resp, err := http.Get(fmt.Sprintf("https://static-cdn.jtvnw.net/emoticons/v1/%d/3.0", ID))
if err != nil {
return errors.Errorf("could not download emote file %s: %v", file.Name(), err)
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return errors.Errorf("could not save emote: %v", err)
}
return nil
}
func downloadCheerEmote(url string, file *os.File) error {
resp, err := http.Get(url)
if err != nil {
return errors.Errorf("could not download cheer file %s: %v", file.Name(), err)
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return errors.Errorf("could not save cheer: %v", err)
}
return nil
} }

2
go.mod
View File

@ -14,7 +14,7 @@ require (
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 // indirect github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 // indirect
github.com/nareix/joy4 v0.0.0-20181022032202-3ddbc8f9d431 github.com/nareix/joy4 v0.0.0-20181022032202-3ddbc8f9d431
github.com/ory/dockertest v3.3.4+incompatible // indirect github.com/ory/dockertest v3.3.4+incompatible // indirect
github.com/pkg/errors v0.8.1 // indirect github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.1 // indirect github.com/sirupsen/logrus v1.4.1 // indirect
github.com/stretchr/objx v0.2.0 // indirect github.com/stretchr/objx v0.2.0 // indirect
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect

1
go.sum
View File

@ -65,6 +65,7 @@ github.com/ory/dockertest v3.3.2+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnh
github.com/ory/dockertest v3.3.4+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs= github.com/ory/dockertest v3.3.4+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -79,7 +80,7 @@ func wsImages(w http.ResponseWriter, r *http.Request) {
func wsEmotes(w http.ResponseWriter, r *http.Request) { func wsEmotes(w http.ResponseWriter, r *http.Request) {
emotefile := filepath.Base(r.URL.Path) emotefile := filepath.Base(r.URL.Path)
http.ServeFile(w, r, "./static/emotes/"+emotefile) http.ServeFile(w, r, path.Join(emoteDir, emotefile))
} }
// Handling the websocket // Handling the websocket

21
main.go
View File

@ -14,9 +14,10 @@ import (
) )
var ( var (
addr string pullEmotes bool
sKey string addr string
stats = newStreamStats() sKey string
stats = newStreamStats()
) )
func setupSettings() error { func setupSettings() error {
@ -42,6 +43,7 @@ func setupSettings() error {
func main() { func main() {
flag.StringVar(&addr, "l", "", "host:port of the MovieNight") flag.StringVar(&addr, "l", "", "host:port of the MovieNight")
flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream") flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream")
flag.BoolVar(&pullEmotes, "e", false, "Pull emotes")
flag.Parse() flag.Parse()
format.RegisterAll() format.RegisterAll()
@ -51,6 +53,16 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if pullEmotes {
common.LogInfoln("Pulling emotes")
err := getEmotes(settings.ApprovedEmotes)
if err != nil {
common.LogErrorf("Error downloading emotes: %+v\n", err)
common.LogErrorf("Error downloading emotes: %v\n", err)
os.Exit(1)
}
}
if err := common.InitTemplates(); err != nil { if err := common.InitTemplates(); err != nil {
common.LogErrorln(err) common.LogErrorln(err)
os.Exit(1) os.Exit(1)
@ -61,7 +73,8 @@ func main() {
// Load emotes before starting server. // Load emotes before starting server.
var err error var err error
if chat, err = newChatRoom(); err != nil { chat, err = newChatRoom()
if err != nil {
common.LogErrorln(err) common.LogErrorln(err)
os.Exit(1) os.Exit(1)
} }

View File

@ -30,8 +30,9 @@ type Settings struct {
AdminPassword string AdminPassword string
StreamKey string StreamKey string
ListenAddress string ListenAddress string
ApprovedEmotes []EmoteSet // list of channels that have been approved for emote use. Global emotes are always "approved". ApprovedEmotes []string // list of channels that have been approved for emote use. Global emotes are always "approved".
SessionKey string // key for session data TwitchClientID string // client id from twitch developers portal
SessionKey string // key for session data
Bans []BanInfo Bans []BanInfo
LogLevel common.LogLevel LogLevel common.LogLevel
LogFile string LogFile string