Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        /// <param name="innerTubeApiKey"></param>
        /// <param name="cc"></param>
        /// <param name="loginInfo"></param>
        /// <returns></returns>
        /// <exception cref="HttpRequestException">多分500番台のエラーだけ</exception>
        public static async Task <GetLiveChat> GetGetLiveChat(DataToPost data, string innerTubeApiKey, CookieContainer cc, ILoginState loginInfo, ILogger logger)
        {
            //dataの構造
            //context
            //+client
            //+request
            //++sessionId
            //+user
            //++onBehalfOfUser
            //continuation

            var url     = $"https://www.youtube.com/youtubei/v1/live_chat/get_live_chat?key={innerTubeApiKey}";
            var handler = new HttpClientHandler
            {
                UseCookies      = true,
                CookieContainer = cc,
            };
            var client = new HttpClient(handler);
            var c      = new StringContent(data.ToString(), Encoding.UTF8, "application/json");

            if (loginInfo is LoggedIn loggedIn)
            {
                var hash = SapiSidHashGenerator.CreateHash(cc, DateTime.Now);
                client.DefaultRequestHeaders.Add("Authorization", $"SAPISIDHASH {hash}");
            }

            client.DefaultRequestHeaders.Add("Origin", "https://www.youtube.com");
            HttpResponseMessage k;

            try
            {
                k = await client.PostAsync(url, c);
            }
            catch (Exception ex)
            {
                throw new GetGetLiveChatException(ex)
                      {
                          Url        = url,
                          DataToPost = data.ToString(),
                          IsLoggedIn = loginInfo is LoggedIn,
                      };
            }
            var s = await k.Content.ReadAsStringAsync();

            var getLiveChat = GetLiveChat.Parse(s);

            //var getLiveChat = new GetLiveChat(s);
            return(getLiveChat);
        }
Пример #2
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;
                //    }
                //}
            }
        }