Esempio n. 1
0
        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")
                    {
                        continue;
                    }

                    // ファイル名の最後方にある[]の中身の文字列を取得
                    // (動画タイトルに[]が含まれる可能性に配慮)
                    var match   = NicoVideoIdRegex.Match(file.Name);
                    var id      = match.Value;
                    var quality = NicoVideoQualityFileNameHelper.NicoVideoQualityFromFileNameExtention(file.Name);
                    var info    = new NicoVideoCacheRequest()
                    {
                        RawVideoId = id,
                        Quality    = quality,
                    };

                    if (string.IsNullOrEmpty(id))
                    {
                        continue;
                    }

                    var nicoVideo = await GetNicoVideoAsync(info.RawVideoId, false);

                    var div = nicoVideo.GetDividedQualityNicoVideo(quality);

                    await nicoVideo.RestoreCache(quality, file.Path);

                    await CacheRequested(info);

                    VideoCacheStateChanged?.Invoke(this, info, NicoVideoCacheState.Cached);

                    Debug.Write(".");
                }
            }
        }
Esempio n. 2
0
        // バックグラウンドで動画キャッシュのダウンロードを行うタスクを作成
        private async Task <DownloadOperation> DonwloadVideoInBackgroundTask(NicoVideoCacheRequest req)
        {
            using (var bgTaskLock = await _RegistrationBackgroundTaskLock.LockAsync())
            {
                using (var releaser = await _DownloadOperationsLock.LockAsync())
                {
                    if (_DownloadOperations.Keys.Any(x => x.RawVideoId == req.RawVideoId && x.Quality == req.Quality))
                    {
                        return(null);
                    }
                }

                Debug.WriteLine($"キャッシュ準備を開始: {req.RawVideoId} {req.Quality}");

                // 動画ダウンロードURLを取得
                var nicoVideo = await MediaManager.GetNicoVideoAsync(req.RawVideoId);

                var div = nicoVideo.GetDividedQualityNicoVideo(req.Quality);
                if (div.IsCached)
                {
                    if (!req.IsRequireForceUpdate)
                    {
                        using (var pendingVideoLockReleaser = await _CacheDownloadPendingVideosLock.LockAsync())
                        {
                            _CacheDownloadPendingVideos.Remove(req);
                        }
                    }
                    else
                    {
                        // キャッシュ済みのためダウンロードを行わない
                        // TODO: Completedイベントを発行
                        return(null);
                    }
                }

                Uri uri = null;
                try
                {
                    uri = await nicoVideo.SetupWatchPageVisit(req.Quality);

                    if (uri == null)
                    {
                        throw new Exception($"can't download {req.Quality} quality Video, in {req.RawVideoId}.");
                    }
                }
                catch
                {
                    return(null);
                }

                var downloader = await ResetDownloader();

                // 認証情報付きクッキーをダウンローダーのHttpヘッダにコピー
                // 動画ページアクセス後のクッキーが必須になるため、サインイン時ではなく
                // ダウンロード開始直前のこのタイミングでクッキーをコピーしています
                var httpclinet = HohoemaApp.NiconicoContext.HttpClient;
                foreach (var header in httpclinet.DefaultRequestHeaders)
                {
                    downloader.SetRequestHeader(header.Key, header.Value);
                }

                // 保存先ファイルの確保
                var filename    = div.VideoFileName;
                var videoFolder = await HohoemaApp.GetVideoCacheFolder();

                var videoFile = await videoFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);

                // ダウンロード操作を作成
                var operation = downloader.CreateDownload(uri, videoFile);

                downloader.CompletionGroup?.Enable();

                await AddDownloadOperation(req, operation);

                Debug.WriteLine($"キャッシュ準備完了: {req.RawVideoId} {req.Quality}");


                // ダウンロードを開始
                var action = operation.StartAsync();
                action.Progress = OnDownloadProgress;
                var task = action.AsTask().ContinueWith(OnDownloadCompleted).ConfigureAwait(false);

                Debug.WriteLine($"キャッシュ開始: {req.RawVideoId} {req.Quality}");

                return(operation);
            }
        }
Esempio n. 3
0
        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(".");
                }
            }
        }