From 0e909286e1e670af4e9a8c7cfee421cb2cc71f16 Mon Sep 17 00:00:00 2001 From: Yami Odymel Date: Wed, 25 Jun 2025 05:49:34 +0800 Subject: [PATCH] Fixed #122 --- channel/channel.go | 11 ++++++++--- channel/channel_record.go | 10 ++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/channel/channel.go b/channel/channel.go index 79d49a7..6b1aefd 100644 --- a/channel/channel.go +++ b/channel/channel.go @@ -111,13 +111,12 @@ func (ch *Channel) ExportInfo() *entity.ChannelInfo { // Pause pauses the channel and cancels the context. func (ch *Channel) Pause() { - // Stop the monitoring loop + // Stop the monitoring loop, this also updates `ch.IsOnline` to false + // `context.Canceled` → `ch.Monitor()` → `onRetry` → `ch.UpdateOnlineStatus(false)`. ch.CancelFunc() ch.Config.IsPaused = true ch.Sequence = 0 - ch.IsOnline = false - ch.Update() ch.Info("channel paused") } @@ -143,3 +142,9 @@ func (ch *Channel) Resume(startSeq int) { <-time.After(time.Duration(startSeq) * time.Second) go ch.Monitor() } + +// UpdateOnlineStatus updates the online status of the channel. +func (ch *Channel) UpdateOnlineStatus(isOnline bool) { + ch.IsOnline = isOnline + ch.Update() +} diff --git a/channel/channel_record.go b/channel/channel_record.go index 849bfde..aa738e4 100644 --- a/channel/channel_record.go +++ b/channel/channel_record.go @@ -32,8 +32,10 @@ func (ch *Channel) Monitor() { return ch.RecordStream(ctx, client) } onRetry := func(_ uint, err error) { - if errors.Is(err, internal.ErrChannelOffline) { - ch.Info("channel is offline, try again in %d min(s)", server.Config.Interval) + ch.UpdateOnlineStatus(false) + + if errors.Is(err, internal.ErrChannelOffline) || errors.Is(err, internal.ErrPrivateStream) { + ch.Info("channel is offline or private, try again in %d min(s)", server.Config.Interval) } else if errors.Is(err, internal.ErrCloudflareBlocked) { ch.Info("channel was blocked by Cloudflare; try with `-cookies` and `-user-agent`? try again in %d min(s)", server.Config.Interval) } else if errors.Is(err, context.Canceled) { @@ -76,10 +78,8 @@ func (ch *Channel) Update() { func (ch *Channel) RecordStream(ctx context.Context, client *chaturbate.Client) error { stream, err := client.GetStream(ctx, ch.Config.Username) if err != nil { - ch.IsOnline = false return fmt.Errorf("get stream: %w", err) } - ch.IsOnline = true ch.StreamedAt = time.Now().Unix() if err := ch.NextFile(); err != nil { @@ -97,6 +97,8 @@ func (ch *Channel) RecordStream(ctx context.Context, client *chaturbate.Client) if err != nil { return fmt.Errorf("get playlist: %w", err) } + ch.UpdateOnlineStatus(true) // Update online status after `GetPlaylist` is OK + ch.Info("stream quality - resolution %dp (target: %dp), framerate %dfps (target: %dfps)", playlist.Resolution, ch.Config.Resolution, playlist.Framerate, ch.Config.Framerate) return playlist.WatchSegments(ctx, ch.HandleSegment)