Permalink
Browse files

Fix ytinviteplugin.

  • Loading branch information...
1 parent 1086303 commit 3d71e982633176acbacef5b954979d25dbe3da67 @iopred committed Dec 11, 2016
Showing with 46 additions and 28 deletions.
  1. +0 −1 cmd/bruxism/main.go
  2. +2 −1 youtube.go
  3. +7 −3 youtubejoinplugin/youtubejoinplugin.go
  4. +37 −23 ytlivechannel.go
View
@@ -206,7 +206,6 @@ func main() {
// This will cause the youtubejoin plugin to join any live streams on those channels.
if youtubeChannelIDs != "" {
channelIDs := strings.Split(youtubeChannelIDs, ",")
-
for _, channelID := range channelIDs {
ytip.Monitor(channelID)
}
View
@@ -124,6 +124,7 @@ func NewYouTube(url bool, auth, configFilename, tokenFilename string) *YouTube {
DeleteChan: make(chan interface{}, 200),
fanFunding: fanFunding{Messages: make(map[string]*youtube.LiveChatMessage)},
joined: make(map[string]bool),
+ videoToChannel: map[string]string{},
}
}
@@ -455,7 +456,6 @@ func (yt *YouTube) Join(videoID string) error {
if yt.joined[videoID] {
return ErrAlreadyJoined
}
- yt.joined[videoID] = true
videos, err := yt.GetVideosByIDList([]string{videoID})
@@ -474,6 +474,7 @@ func (yt *YouTube) Join(videoID string) error {
}
func (yt *YouTube) JoinChat(videoID, channelID, liveChatID string) error {
+ yt.joined[videoID] = true
yt.videoToChannel[videoID] = channelID
liveBroadcastListResponse := &youtube.LiveBroadcastListResponse{
@@ -40,7 +40,7 @@ func (p *YouTubeJoinPlugin) Load(bot *bruxism.Bot, service bruxism.Service, data
}
for channel, _ := range p.Channels {
- p.Monitor(channel)
+ p.ytLiveChannel.MonitorAll(channel, p.liveVideoChan)
}
go p.Run(bot, service)
@@ -65,10 +65,14 @@ func (p *YouTubeJoinPlugin) Run(bot *bruxism.Bot, service bruxism.Service) {
func (p *YouTubeJoinPlugin) Monitor(channel string) error {
p.Lock()
+ defer p.Unlock()
+ if p.Channels[channel] {
+ return nil
+ }
+
p.Channels[channel] = true
- p.Unlock()
- return p.ytLiveChannel.Monitor(channel, p.liveVideoChan)
+ return p.ytLiveChannel.MonitorAll(channel, p.liveVideoChan)
}
// Save will save plugin state to a byte array.
View
@@ -13,38 +13,44 @@ import (
type YTLiveChannel struct {
sync.RWMutex
service *youtube.Service
+
// map channelID -> chan
- liveVideoChans map[string][]chan *youtube.Video
- channelNames map[string]string
+ filteredLiveVideoChans map[string][]chan *youtube.Video
+ liveVideoChans map[string][]chan *youtube.Video
+
+ channelNames map[string]string
}
func NewYTLiveChannel(service *youtube.Service) *YTLiveChannel {
- return &YTLiveChannel{service: service}
+ return &YTLiveChannel{
+ service: service,
+ channelNames: map[string]string{},
+ filteredLiveVideoChans: map[string][]chan *youtube.Video{},
+ liveVideoChans: map[string][]chan *youtube.Video{},
+ }
}
+// Monitor monitors a channel for new live videos and sends them down liveVideoChan.
+// If the channel is live when this is called, it will not send the video down the channel.
func (y *YTLiveChannel) Monitor(channel string, liveVideoChan chan *youtube.Video) error {
y.Lock()
- defer y.Unlock()
+ created := len(y.filteredLiveVideoChans[channel])+len(y.liveVideoChans[channel]) == 0
+ y.filteredLiveVideoChans[channel] = append(y.filteredLiveVideoChans[channel], liveVideoChan)
+ y.Unlock()
- if y.channelNames[channel] == "" {
- clr, err := y.service.Channels.List("snippet").Id(channel).Do()
- if err != nil {
- return errors.New("Error loading channel.")
- }
- if len(clr.Items) != 1 {
- return errors.New("No channel found.")
- }
- if y.channelNames == nil {
- y.channelNames = map[string]string{}
- }
- y.channelNames[channel] = clr.Items[0].Snippet.Title
+ if created {
+ go y.poll(channel)
}
+ return nil
+}
- if y.liveVideoChans == nil {
- y.liveVideoChans = map[string][]chan *youtube.Video{}
- }
- created := len(y.liveVideoChans[channel]) == 0
+// MonitorAll monitors a channel for live videos and sends them down liveVideoChan.
+func (y *YTLiveChannel) MonitorAll(channel string, liveVideoChan chan *youtube.Video) error {
+ y.Lock()
+ created := len(y.filteredLiveVideoChans[channel])+len(y.liveVideoChans[channel]) == 0
y.liveVideoChans[channel] = append(y.liveVideoChans[channel], liveVideoChan)
+ y.Unlock()
+
if created {
go y.poll(channel)
}
@@ -64,9 +70,17 @@ func (y *YTLiveChannel) poll(channel string) {
first := true
for {
videos, _ := y.getLiveVideos(channel)
+ y.Lock()
for k, v := range videos {
if !seen[k] {
seen[k] = true
+
+ y.channelNames[channel] = v.Snippet.ChannelTitle
+
+ for _, c := range y.liveVideoChans[channel] {
+ c <- v
+ }
+
// Don't announce the videos that are already live.
if first {
continue
@@ -77,13 +91,13 @@ func (y *YTLiveChannel) poll(channel string) {
continue
}
lastAnnounce = now
- y.RLock()
- for _, c := range y.liveVideoChans[channel] {
+
+ for _, c := range y.filteredLiveVideoChans[channel] {
c <- v
}
- y.RUnlock()
}
}
+ y.Unlock()
first = false
<-time.After(5 * time.Minute)
}

0 comments on commit 3d71e98

Please sign in to comment.