public async Task <bool> CanHandleSubscriptionUrl(Uri uri) { try { uri = YouTubeUrlHelper.FixYouTubeChannelUri(uri); var info = await ytdlService.UsingYoutubeDL(async ytdl => await ytdl.ExtractInformation(uri.ToString(), false)); return(info.Type == YoutubeDLWrapper.UrlType.Playlist || info.Type == YoutubeDLWrapper.UrlType.MultiVideo); } catch (Exception ex) { logger.LogDebug(ex, $"Cannot handle subscription URL {uri}"); return(false); } }
protected override async Task ExecuteJob(IJobExecutionContext context) { VideoId = context.MergedJobDataMap.GetInt("VideoId"); shouldRetry = false; video = dataContext.Videos.Find(VideoId); if (video == null) { throw new ArgumentException($"Download failed - invalid video id {VideoId}."); } if (video.DownloadedPath != null) { throw new ArgumentException($"Download failed - video {VideoId} is already downloaded!"); } var opts = ResolveDownloadOptions(video).ToArray(); shouldRetry = true; log.LogInformation("Running youtube-dl with arguments: {0}", string.Join(" ", opts)); try { await ytdlService.UsingYoutubeDL(ytdl => { int resultCode = ytdl.Run(opts, ProcessStdout, ProcessStderr, timeoutMs: 24 * 3600 * 1000, cancellationToken: cancellationTokenSrc.Token); if (resultCode != 0) { throw new Exception($"videoId={VideoId}: Download failed!\n"); } return(Task.CompletedTask); }); } catch (OperationCanceledException) { shouldRetry = false; log.LogInformation("Video download was canceled!"); throw; } finally { videoDownloader.OnDownloadFinished(VideoId); } using (var @lock = await videoMutex.LockAsync()) { video.DownloadedPath = outputPath; video.DownloadedSize = await videoStorage.CalculateSize(video); await dataContext.SaveChangesAsync(); } log.LogInformation($"videoId={VideoId}: Download completed!"); }