using System; using System.Text; using Newtonsoft.Json; using System.Linq; using System.Diagnostics; using System.Text.RegularExpressions; using ttrss_co_client.ttrss; using ttrss_co_client.ttrss.datastructures; namespace ttrss_co_client.tasks { ///Move to output public class Ytdlp : Phase2Task { public override string TaskName => "yt-dlp"; public override async Task>ActOn(WorkOrder workOrder) { var article = (await TtrssClient.GetArticles(workOrder.articleId)).FirstOrDefault(); var sw = new Stopwatch(); var ytdl = new YoutubeDLSharp.YoutubeDL(); ytdl.YoutubeDLPath = "yt-dlp"; ytdl.FFmpegPath = "ffmpeg"; ytdl.OutputFolder = $"{Conf.WorkingDirectory}/{workOrder.guid}"; ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s"; if(!Path.Exists(ytdl.OutputFolder)) { Directory.CreateDirectory(ytdl.OutputFolder); } sw.Start(); var res = await ytdl.RunVideoDownload(workOrder.data["ytdlp-link"]); sw.Stop(); if (!res.Success) { await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] yt-dlp fatal error. \n{string.Join('\n', res.ErrorOutput)}", article.id); return null; } await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] yt-dlp {(res.Success ? "success" : "fail")} in {sw.Elapsed}", article.id); var outputFilename = res.Data; foreach(char c in "'\"") { outputFilename = outputFilename.Replace(c, '_'); } if(outputFilename != res.Data) { File.Move(res.Data, outputFilename); } workOrder.data["path"] = outputFilename; return new Tuple(TaskStatus.ContinueNow, workOrder); } } }