Exemple #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);
        }
        public PostCommentContext2 Create(string message)
        {
            var     context = "{\"context\":{\"client\":{\"clientName\":\"WEB\",\"clientVersion\":\"" + GetClientVersion() + "\"}}}";
            dynamic d       = JsonConvert.DeserializeObject(context);

            d.context.user    = JsonConvert.DeserializeObject("{\"onBehalfOfUser\":\"" + _delegatedSessionId + "\"}");
            d.@params         = GetParams();
            d.clientMessageId = GetClientIdPrefix() + _commentCounter;
            d.richMessage     = JsonConvert.DeserializeObject("{\"textSegments\":[{\"text\":\"" + message + "\"}]}");
            var payload = (string)JsonConvert.SerializeObject(d, Formatting.None);
            var hash    = SapiSidHashGenerator.CreateHash(_cc, DateTime.Now);

            //var url = GetUrl();
            return(new PostCommentContext2(payload, hash));
        }