/// <summary> /// すぱこーRSSフィードの新しい話がWeb上にあるかどうか判別します。 /// </summary> private async Task CheckNewContents() { // 最新話が見つかったフラグ bool newContentsFound = false; try { foreach (var sause in spacoSause) { string url = $"{sause.RSSFeedURL}?count=1"; // このforeachブロック専用のCancellationTokenを生成します。 using (CancellationTokenSource cancellationTokenSourceInstant = new CancellationTokenSource()) { // タイムアウトは5秒間に設定します。 cancellationTokenSourceInstant.CancelAfter(5000); using (XmlReader reader = await Task.Run(() => SpacoRSSClient.GetXmlReaderAsync(url, cancellationTokenSourceInstant.Token))) { SpacoRSSReader srr = await Task.Run(() => SpacoRSSReader.LoadAsync(reader, cancellationTokenSourceInstant.Token)); // 最新話のVolumeが、Items上の同じソースの最新のVolumeより大きい時、フラグをオンにします。 if (srr.Items.First().Volume > Items.Where(_ => _.Type == sause.Type).Max(_ => _.Volume)) { newContentsFound = true; } } } } // 最新話が見つかったら、ViewModelに通知します。 if (newContentsFound) { NewRSSContentsFound?.Invoke(this, new EventArgs()); } } catch (Exception) {} }
/// <summary> /// WebからRSSフィードを取得します。 /// </summary> /// <returns>すぱこーRSSフィードのコンテンツを格納したリスト</returns> private async Task <IEnumerable <SpacomicRSSItem> > GetRSSCore() { List <SpacomicRSSItem> list = new List <SpacomicRSSItem>(); foreach (var sause in spacoSause) { // オフセット位置 int offset = 0; // 続けて取得する必要があるかを表すフラグ bool isContinue = true; do { string url = $"{sause.RSSFeedURL}?offset={offset}"; using (XmlReader reader = await Task.Run(() => SpacoRSSClient.GetXmlReaderAsync(url, cancellationTokenSource.Token))) { SpacoRSSReader srr = await Task.Run(() => SpacoRSSReader.LoadAsync(reader, cancellationTokenSource.Token)); // すぱこーRSSフィードのチャネル情報を設定します。 if (!SauseItems.ContainsKey(sause.Type)) { SauseItems[sause.Type] = new SpacoRSSSause { Title = srr.Title, Description = srr.Description, Author = srr.Author, Link = srr.Link, PubDate = srr.PubDate, BannerURL = srr.BannerURL }; } // 取得したコンテンツの数を取得します。 int count = srr.Items.Count(); if (count > 0) { offset += count; } // なければ、現在ソースからののRSSフィードの取得を終了します。 else { isContinue = false; } // 利用可能なコンテンツを抽出します。 list.AddRange( srr.Items.Where(_ => _.IsAvailable) .Select(_ => new SpacomicRSSItem(sause.Type, _)) .ToList() ); } } while(isContinue); } return(list); }