Permalink
Browse files

Youtube will break long messages across multiple messages. Removed de…

…ad code. Youtube will correctly stop polling on ended events and on high errors.
  • Loading branch information...
1 parent d3a1988 commit fc79838f589ed5d4c4d34e4dd23186c112986730 @iopred committed Dec 11, 2016
Showing with 35 additions and 38 deletions.
  1. +6 −1 statsplugin/statsplugin.go
  2. +29 −37 youtube.go
@@ -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"), " "))
}
}
View
@@ -11,7 +11,6 @@ import (
"net/http"
"sort"
"strings"
- "sync"
"time"
"github.com/atotto/clipboard"
@@ -36,6 +35,10 @@ const (
LiveChatBanSnippetTypePermanent = "permanent"
)
+const (
+ LiveChatEndedEvent string = "chatEndedEvent"
+)
+
// LiveChatMessage is a Message wrapper around youtube.LiveChatMessage.
type LiveChatMessage youtube.LiveChatMessage
@@ -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
@@ -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
@@ -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{},
}
@@ -150,6 +146,8 @@ func (yt *YouTube) pollMessages(broadcast *youtube.LiveBroadcast) {
}
yt.joined[broadcast.Snippet.LiveChatId] = true
+ errors := 0
+
yt.channelCount++
pageToken := ""
for {
@@ -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
}
}
}
@@ -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.
@@ -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
}

0 comments on commit fc79838

Please sign in to comment.