Ejemplo n.º 1
0
        public void Start()
        {
            if (!CanStart)
            {
                return;
            }

            IsActive     = true;
            IsSuccessful = false;
            IsCanceled   = false;
            IsFailed     = false;

            Task.Run(async() =>
            {
                // Create cancellation token source
                _cancellationTokenSource = new CancellationTokenSource();

                // Create progress operation
                ProgressOperation = ProgressManager.CreateOperation();

                try
                {
                    // If download option is not set - get the best download option
                    if (DownloadOption == null)
                    {
                        DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format);
                    }

                    await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token);

                    if (_settingsService.ShouldInjectTags)
                    {
                        await _taggingService.InjectTagsAsync(Video, Format, FilePath, _cancellationTokenSource.Token);
                    }

                    if (SubtitleOption != null && SubtitleOption.ClosedCaptionTrackInfos.Any())
                    {
                        await _downloadService.DownloadSubtitleAsync(SubtitleOption, FilePath);
                    }

                    IsSuccessful = true;
                }
                catch (OperationCanceledException)
                {
                    IsCanceled = true;
                }
                catch (Exception ex)
                {
                    IsFailed   = true;
                    FailReason = ex.Message;
                }
                finally
                {
                    IsActive = false;

                    _cancellationTokenSource.Dispose();
                    ProgressOperation.Dispose();
                }
            });
        }
Ejemplo n.º 2
0
        public void Start()
        {
            if (!CanStart)
            {
                return;
            }

            IsActive     = true;
            IsSuccessful = false;
            IsCanceled   = false;
            IsFailed     = false;

            Task.Run(async() =>
            {
                // Create cancellation token source
                _cancellationTokenSource = new CancellationTokenSource();

                // Create progress operation
                ProgressOperation = ProgressManager.CreateOperation();

                try
                {
                    // daca nu sunt setate obtiuni de descarcare se foloseste setarea de baza
                    // descarca video
                    if (DownloadOption == null)
                    {
                        DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format);
                    }

                    await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token);

                    if (_settingsService.ShouldInjectTags)
                    {
                        await _taggingService.InjectTagsAsync(Video, Format, FilePath, _cancellationTokenSource.Token);
                    }

                    IsSuccessful = true;
                }
                catch (OperationCanceledException)
                {
                    IsCanceled = true;
                }
                catch (Exception ex)
                {
                    IsFailed   = true;
                    FailReason = ex.Message;
                }
                finally
                {
                    IsActive = false;

                    _cancellationTokenSource.Dispose();
                    ProgressOperation.Dispose();
                }
            });
        }
Ejemplo n.º 3
0
        public void Start()
        {
            if (!CanStart)
            {
                return;
            }

            IsActive     = true;
            IsSuccessful = false;
            IsCanceled   = false;
            IsFailed     = false;

            Task.Run(async() =>
            {
                _cancellationTokenSource = new CancellationTokenSource();
                ProgressOperation        = ProgressManager?.CreateOperation();

                try
                {
                    // If download option is not set - get the best download option
                    VideoOption ??= await _downloadService.TryGetBestVideoDownloadOptionAsync(
                        Video.Id,
                        Format,
                        QualityPreference
                        );

                    // It's possible that video has no streams
                    if (VideoOption == null)
                    {
                        throw new InvalidOperationException($"Video '{Video.Id}' contains no streams.");
                    }

                    await _downloadService.DownloadAsync(
                        VideoOption,
                        SubtitleOption,
                        FilePath,
                        ProgressOperation,
                        _cancellationTokenSource.Token
                        );

                    if (_settingsService.ShouldInjectTags)
                    {
                        await _taggingService.InjectTagsAsync(
                            Video,
                            Format,
                            FilePath,
                            _cancellationTokenSource.Token
                            );
                    }

                    IsSuccessful = true;
                }
                catch (OperationCanceledException)
                {
                    IsCanceled = true;
                }
                catch (Exception ex)
                {
                    IsFailed = true;

                    // Short error message for expected errors, full for unexpected
                    FailReason = ex is YoutubeExplodeException
                        ? ex.Message
                        : ex.ToString();
                }
                finally
                {
                    IsActive = false;
                    _cancellationTokenSource?.Dispose();
                    _cancellationTokenSource = null;
                    ProgressOperation?.Dispose();
                }
            });
        }