/// <summary> /// /// </summary> /// <param name="initialContinuation"></param> /// <returns></returns> /// <exception cref="ReloadException"></exception> public async Task ReceiveAsync(string initialContinuation) { _cts = new CancellationTokenSource(); //var continuation = initialContinuation.Continuation; var continuation = initialContinuation; while (!_cts.IsCancellationRequested) { var getLiveChatUrl = $"https://www.youtube.com/live_chat/get_live_chat?continuation={System.Web.HttpUtility.UrlEncode(continuation)}&pbj=1"; string getLiveChatJson = null; try { var getLiveChatBytes = await _server.GetBytesAsync(getLiveChatUrl); getLiveChatJson = Encoding.UTF8.GetString(getLiveChatBytes); var(c, a, sessionToken) = Tools.ParseGetLiveChat(getLiveChatJson); continuation = c.Continuation; if (a.Count > 0) { if (c is ITimedContinuation timed) { var interval = c.TimeoutMs / a.Count; foreach (var action in a) { ActionsReceived?.Invoke(this, new List <CommentData> { action }); await Task.Delay(interval, _cts.Token); } } else if (c is IInvalidationContinuation invalid) { ActionsReceived?.Invoke(this, a); await Task.Delay(1000, _cts.Token); } else if (c is IReloadContinuation) { throw new ReloadException(); } else { } } else { await Task.Delay(1000, _cts.Token); } } catch (WebException ex) { throw new ReloadException(ex); } catch (HttpRequestException ex) { throw new ReloadException(ex); } catch (ParseException ex) { _logger.LogException(ex, "get_live_chatのパースに失敗", getLiveChatJson); } catch (TaskCanceledException) { break; } catch (ContinuationContentsNullException) { //放送終了 break; } catch (ContinuationNotExistsException) { break; } } }
/// <summary> /// /// </summary> /// <param name="vid"></param> /// <param name="initialContinuation"></param> /// <param name="cc"></param> /// <returns></returns> /// <exception cref="ReloadException"></exception> public async Task ReceiveAsync(string vid, IContinuation initialContinuation, CookieContainer cc) { _cts = new CancellationTokenSource(); var continuation = initialContinuation; while (!_cts.IsCancellationRequested) { var getLiveChatUrl = $"https://www.youtube.com/live_chat/get_live_chat?continuation={System.Web.HttpUtility.UrlEncode( continuation.Continuation)}&pbj=1"; //var wc = new MyWebClient(cc); var ua = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.2924.87 Safari/537.36"; string getLiveChatJson = null; try { var getLiveChatBytes = await _server.GetBytesAsync(getLiveChatUrl); getLiveChatJson = Encoding.UTF8.GetString(getLiveChatBytes); var(c, a, sessionToken) = Tools.ParseGetLiveChat(getLiveChatJson); continuation = c; if (a.Count > 0) { if (c is ITimedContinuation timed) { var interval = c.TimeoutMs / a.Count; foreach (var action in a) { ActionsReceived?.Invoke(this, new List <CommentData> { action }); await Task.Delay(interval, _cts.Token); } } else { ActionsReceived?.Invoke(this, a); await Task.Delay(1000, _cts.Token); } } else { await Task.Delay(1000, _cts.Token); } } catch (WebException ex) { throw new ReloadException(ex); } catch (HttpRequestException ex) { throw new ReloadException(ex); } catch (ParseException ex) { _logger.LogException(ex, "get_live_chatのパースに失敗", getLiveChatJson); } catch (TaskCanceledException) { break; } catch (ContinuationContentsNullException) { //放送終了 break; } catch (ContinuationNotExistsException) { break; } } }