Skip to content

Commit

Permalink
Youtube will break long messages across multiple messages. Removed de…
Browse files Browse the repository at this point in the history
…ad code. Youtube will correctly stop polling on ended events and on high errors.
  • Loading branch information
iopred committed Dec 11, 2016
1 parent d3a1988 commit fc79838
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
7 changes: 6 additions & 1 deletion statsplugin/statsplugin.go
Expand Up @@ -94,14 +94,19 @@ func StatsCommand(bot *bruxism.Bot, service bruxism.Service, message bruxism.Mes
w.Flush()
out := buf.String()

end := ""
if IsSeptapus {
out += "\nSeptapus community: <https://discord.gg/HWN9pwj>\nPatreon: <https://www.patreon.com/iopred>\nBuilt with love by iopred."
end += "Septapus community: <https://discord.gg/HWN9pwj>\nPatreon: <https://www.patreon.com/iopred>\nBuilt with love by iopred."
}

if service.SupportsMultiline() {
if end != "" {
out += "\n" + end
}
service.SendMessage(message.Channel(), out)
} else {
service.SendMessage(message.Channel(), strings.Join(strings.Split(out, "\n"), " "))
service.SendMessage(message.Channel(), strings.Join(strings.Split(end, "\n"), " "))
}
}

Expand Down
66 changes: 29 additions & 37 deletions youtube.go
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"sort"
"strings"
"sync"
"time"

"github.com/atotto/clipboard"
Expand All @@ -36,6 +35,10 @@ const (
LiveChatBanSnippetTypePermanent = "permanent"
)

const (
LiveChatEndedEvent string = "chatEndedEvent"
)

// LiveChatMessage is a Message wrapper around youtube.LiveChatMessage.
type LiveChatMessage youtube.LiveChatMessage

Expand Down Expand Up @@ -87,11 +90,6 @@ func (m *LiveChatMessage) Type() MessageType {
return MessageTypeCreate
}

type fanFunding struct {
sync.Mutex
Messages map[string]*youtube.LiveChatMessage
}

// YouTube is a Service provider for YouTube.
type YouTube struct {
url bool
Expand All @@ -105,7 +103,6 @@ type YouTube struct {
messageChan chan Message
InsertChan chan interface{}
DeleteChan chan interface{}
fanFunding fanFunding
me *youtube.Channel
channelCount int
joined map[string]bool
Expand All @@ -122,7 +119,6 @@ func NewYouTube(url bool, auth, configFilename, tokenFilename string) *YouTube {
messageChan: make(chan Message, 200),
InsertChan: make(chan interface{}, 200),
DeleteChan: make(chan interface{}, 200),
fanFunding: fanFunding{Messages: make(map[string]*youtube.LiveChatMessage)},
joined: make(map[string]bool),
videoToChannel: map[string]string{},
}
Expand Down Expand Up @@ -150,6 +146,8 @@ func (yt *YouTube) pollMessages(broadcast *youtube.LiveBroadcast) {
}
yt.joined[broadcast.Snippet.LiveChatId] = true

errors := 0

yt.channelCount++
pageToken := ""
for {
Expand All @@ -161,17 +159,21 @@ func (yt *YouTube) pollMessages(broadcast *youtube.LiveBroadcast) {
liveChatMessageListResponse, err := list.Do()

if err != nil {
log.Println(err)
errors++
if errors > 10 {
return
}
} else {
errors = 0
// Ignore the first results, we only want new chats.
if pageToken != "" {
for _, message := range liveChatMessageListResponse.Items {
liveChatMessage := LiveChatMessage(*message)
yt.messageChan <- &liveChatMessage

switch message.Snippet.Type {
case LiveChatMessageSnippetTypeFanFunding:
yt.addFanFundingMessage(message)
case LiveChatEndedEvent:
return
}
}
}
Expand All @@ -197,25 +199,6 @@ func (yt *YouTube) writeMessagesToFile(messages []*youtube.LiveChatMessage, file
}
}

func (yt *YouTube) addFanFundingMessage(message *youtube.LiveChatMessage) {
yt.fanFunding.Lock()
defer yt.fanFunding.Unlock()

if yt.fanFunding.Messages[message.Id] == nil {
yt.fanFunding.Messages[message.Id] = message
yt.writeMessagesToFile([]*youtube.LiveChatMessage{message}, "youtubelatest.txt")
}

largest := message
for _, check := range yt.fanFunding.Messages {
if check.Snippet.FanFundingEventDetails.AmountMicros > largest.Snippet.FanFundingEventDetails.AmountMicros {
largest = check
}
}

yt.writeMessagesToFile([]*youtube.LiveChatMessage{largest}, "youtubelargest.txt")
}

func (yt *YouTube) generateOauthURLAndExit() {
// Redirect user to Google's consent page to ask for permission
// for the scopes specified above.
Expand Down Expand Up @@ -381,14 +364,23 @@ var messageReplacer = strings.NewReplacer("<", "(", ">", ")")

// SendMessage sends a message.
func (yt *YouTube) SendMessage(channel, message string) error {
yt.InsertChan <- &youtube.LiveChatMessage{
Snippet: &youtube.LiveChatMessageSnippet{
LiveChatId: channel,
Type: LiveChatMessageSnippetTypeText,
TextMessageDetails: &youtube.LiveChatTextMessageDetails{
MessageText: messageReplacer.Replace(message),
// Send messages of 200 characters.
for i := 0; i < len(message); i += 200 {
m := i + 200
if m > len(message) {
m = len(message)
}

me := message[i:m]
yt.InsertChan <- &youtube.LiveChatMessage{
Snippet: &youtube.LiveChatMessageSnippet{
LiveChatId: channel,
Type: LiveChatMessageSnippetTypeText,
TextMessageDetails: &youtube.LiveChatTextMessageDetails{
MessageText: messageReplacer.Replace(me),
},
},
},
}
}
return nil
}
Expand Down

0 comments on commit fc79838

Please sign in to comment.