Example #1
0
        public async Task ReceiveInternalAsync(YtInitialData ytInitialData, YtCfg ytCfg, CookieContainer cc, ILoginState loginInfo)
        {
            var    dataToPost = new DataToPost(ytCfg);
            string initialContinuation;

            if (_siteOptions.IsAllChat)
            {
                initialContinuation = ytInitialData.ChatContinuation().AllChatContinuation;
            }
            else
            {
                initialContinuation = ytInitialData.ChatContinuation().JouiChatContinuation;
            }
            dataToPost.SetContinuation(initialContinuation);

            while (!_cts.IsCancellationRequested)
            {
                //例外はここではcatchしない。
                var s = await Tools.GetGetLiveChat(dataToPost, ytCfg.InnerTubeApiKey, cc, loginInfo);

                var actions      = s.GetActions();
                var continuation = s.GetContinuation();
                if (continuation is ReloadContinuation reload)
                {
                    throw new ReloadException();
                }
                else if (continuation is UnknownContinuation unknown)
                {
                    throw new SpecChangedException(unknown.Raw);
                }
                dataToPost.SetContinuation(continuation.Continuation);
                var count     = actions.Count;
                var timeoutMs = Math.Max(continuation.TimeoutMs, 1000);
                var waitTime  = count > 0 ? timeoutMs / count : 1000;
                foreach (var action in actions)
                {
                    ProcessAction(action);
                    try
                    {
                        await Task.Delay(waitTime, _cts.Token);
                    }
                    catch (TaskCanceledException)
                    {
                        return;
                    }
                }
            }
        }
        public async Task ReceiveInternalAsync(YtInitialData ytInitialData1, YtCfg ytCfg, CookieContainer cc, ILoginState loginInfo)
        {
            var    dataToPost = new DataToPost(ytCfg);
            string initialContinuation;

            if (_siteOptions.IsAllChat)
            {
                initialContinuation = ytInitialData1.AllChatContinuation;// ytInitialData.ChatContinuation().AllChatContinuation;
            }
            else
            {
                initialContinuation = ytInitialData1.JouiChatContinuation;// ytInitialData.ChatContinuation().JouiChatContinuation;
            }
            dataToPost.SetContinuation(initialContinuation);

            while (!_cts.IsCancellationRequested)
            {
                //例外はここではcatchしない。
                var getLiveChat = await Tools.GetGetLiveChat(dataToPost, ytCfg.InnertubeApiKey, cc, loginInfo, _logger);

                var actions      = getLiveChat.Actions;
                var continuation = getLiveChat.Continuation;// s.GetContinuation();
                if (continuation is null)
                {
                    throw new ContinuationNotExistsException();
                }
                if (continuation is ReloadContinuationData reload)
                {
                    throw new ReloadException();
                }
                else if (continuation is TimedContinuationData timed)
                {
                    dataToPost.SetContinuation(timed.Continaution);
                    await ProcessAction(actions, timed.TimeoutMs);
                }
                else if (continuation is InvalidationContinuationData invalid)
                {
                    dataToPost.SetContinuation(invalid.Continaution);
                    await ProcessAction(actions, invalid.TimeoutMs);
                }
                else if (continuation is UnknownContinuationData unknown)
                {
                    throw new SpecChangedException(unknown.Raw);
                }
                else
                {
                    //ここには来ない予定
                    throw new SpecChangedException("");
                }
                //if (continuation is ReloadContinuation reload)
                //{
                //    throw new ReloadException();
                //}
                //else if (continuation is UnknownContinuation unknown)
                //{
                //    throw new SpecChangedException(unknown.Raw);
                //}
                //dataToPost.SetContinuation(continuation.Continuation);
                //var timeoutMs = Math.Max(continuation.TimeoutMs, 1000);
                //if (actions.Count > 0)
                //{
                //    var waitTime = timeoutMs / actions.Count;
                //    foreach (var action in actions)
                //    {
                //        ProcessAction(action);
                //        try
                //        {
                //            await Task.Delay(waitTime, _cts.Token);
                //        }
                //        catch (TaskCanceledException)
                //        {
                //            return;
                //        }
                //    }
                //}
                //else
                //{
                //    var waitTime = timeoutMs;
                //    try
                //    {
                //        await Task.Delay(waitTime, _cts.Token);
                //    }
                //    catch (TaskCanceledException)
                //    {
                //        return;
                //    }
                //}
            }
        }