Example #1
0
        private async Task DownloadEpisode(Uri episodeUrl, IEpisodeProvider episodeProvider, CancellationToken cancellationToken = default)
        {
            var episodeInfo = await episodeProvider.GetEpisodeInfoAsync(episodeUrl, cancellationToken);

            _logger.LogInformation($"Downloading {episodeInfo.GetFileName()}");

            var filename = _fileService.MakeValidFileName(episodeInfo.GetFileName());

            var savePathBuilder = new SavePathBuilder(_fileService)
                                  .ForEpisode(episodeInfo)
                                  .SetBasePath(_configuration.SavePath);

            if (_configuration.SaveShowsInFolders)
            {
                savePathBuilder.AddShowFolder();
            }
            if (_configuration.SaveSeasonsInFolders)
            {
                savePathBuilder.AddSeasonFolder();
            }
            var savePath = savePathBuilder.Build();

            var downloadFile = new File(filename, _configuration.DownloadExtension, _configuration.DownloadPath);
            var finalFile    = downloadFile.SetPath(savePath);

            _fileService.EnsureFolderExists(_configuration.DownloadPath);
            _fileService.EnsureFolderExists(savePath);

            _downloadService.DownloadEpisode(episodeInfo.StreamUrl, downloadFile.GetFullPath(), episodeInfo.Skip, episodeInfo.Duration);
            _fileService.MoveFile(downloadFile.GetFullPath(), finalFile.GetFullPath(), _configuration.Overwrite);

            await _historyService.AddDownloadedAsync(episodeInfo.GetFileName(), episodeUrl, episodeInfo.StreamUrl);
        }