private async Task RetrieveCacheCompletedVideos() { var videoFolder = await _HohoemaApp.GetVideoCacheFolder(); if (videoFolder != null) { var files = await videoFolder.GetFilesAsync(); foreach (var file in files) { if (!(file.FileType == ".mp4" || file.FileType == ".flv")) { continue; } // ファイル名の最後方にある[]の中身の文字列を取得 // (動画タイトルに[]が含まれる可能性に配慮) var match = NicoVideoIdRegex.Match(file.Name); var id = match.Groups[1]?.Value; NicoVideoQuality quality = NicoVideoQuality.Unknown; if (string.IsNullOrEmpty(id)) { // 外部キャッシュとして取得可能かをチェック match = ExternalCachedNicoVideoIdRegex.Match(file.Name); if (match.Groups.Count > 0) { id = match.Groups[match.Groups.Count - 1].Value; } // 動画IDを抽出不可だった場合はスキップ if (string.IsNullOrEmpty(id)) { continue; } } else { quality = VideoCacheManager.GetQualityFromFileName(file.Name); } var info = new NicoVideoCacheInfo() { RawVideoId = id, Quality = quality, FilePath = file.Path, RequestAt = file.DateCreated.DateTime }; _CacheVideos.AddOrUpdate(info.RawVideoId, (x) => { return(new List <NicoVideoCacheInfo>() { info }); }, (x, y) => { var tempinfo = y.FirstOrDefault(z => z.Quality == info.Quality); if (tempinfo == null) { y.Add(info); } else { tempinfo.RequestAt = info.RequestAt; tempinfo.FilePath = info.FilePath; } return(y); }); VideoCacheStateChanged?.Invoke(this, new VideoCacheStateChangedEventArgs() { Request = info, CacheState = NicoVideoCacheState.Cached }); Debug.Write("."); } } }
// ダウンロード完了 private async Task OnDownloadCompleted(Task <DownloadOperation> prevTask) { // 進捗付きトースト表示を削除 RemoveProgressToast(); var progress = TaskIdToCacheProgress[prevTask.Id]; await RemoveDownloadOperation(progress); TaskIdToCacheProgress.Remove(prevTask.Id); if (prevTask.IsFaulted) { Debug.WriteLine("キャッシュ失敗"); VideoCacheStateChanged?.Invoke(this, new VideoCacheStateChangedEventArgs() { Request = new NicoVideoCacheRequest() { RawVideoId = progress.RawVideoId, RequestAt = progress.RequestAt, Quality = progress.Quality, IsRequireForceUpdate = progress.IsRequireForceUpdate }, CacheState = NicoVideoCacheState.Pending }); return; } Debug.WriteLine("キャッシュ完了"); if (prevTask.Result != null) { var op = progress.DownloadOperation; if (op.Progress.Status == BackgroundTransferStatus.Completed) { if (op.Progress.TotalBytesToReceive == op.Progress.BytesReceived) { Debug.WriteLine("キャッシュ済み: " + op.ResultFile.Name); var cacheInfo = new NicoVideoCacheInfo(progress, op.ResultFile.Path); _CacheVideos.AddOrUpdate(cacheInfo.RawVideoId, (x) => { return(new List <NicoVideoCacheInfo>() { cacheInfo }); }, (x, y) => { var tempinfo = y.FirstOrDefault(z => z.Quality == cacheInfo.Quality); if (tempinfo == null) { y.Add(cacheInfo); } else { tempinfo.RequestAt = cacheInfo.RequestAt; tempinfo.FilePath = cacheInfo.FilePath; } return(y); }); VideoCacheStateChanged?.Invoke(this, new VideoCacheStateChangedEventArgs() { Request = progress, CacheState = NicoVideoCacheState.Cached }); } else { Debug.WriteLine("キャッシュキャンセル: " + op.ResultFile.Name); VideoCacheStateChanged?.Invoke(this, new VideoCacheStateChangedEventArgs() { Request = new NicoVideoCacheRequest() { RawVideoId = progress.RawVideoId, RequestAt = progress.RequestAt, Quality = progress.Quality, IsRequireForceUpdate = progress.IsRequireForceUpdate }, CacheState = NicoVideoCacheState.Pending }); } } else { Debug.WriteLine($"キャッシュ失敗: {op.ResultFile.Name} (再ダウンロードします)"); VideoCacheStateChanged?.Invoke(this, new VideoCacheStateChangedEventArgs() { Request = new NicoVideoCacheRequest() { RawVideoId = progress.RawVideoId, RequestAt = progress.RequestAt, Quality = progress.Quality, IsRequireForceUpdate = progress.IsRequireForceUpdate }, CacheState = NicoVideoCacheState.Pending }); } } }