Compare commits

..

No commits in common. "036dff3b1262deac6039de4b5a567dbd14381c09" and "3fcc5f376df6a4693cd3b88c063aedd58e5d8813" have entirely different histories.

5 changed files with 268 additions and 168 deletions

View File

@ -198,5 +198,84 @@ namespace ttrss_co_client
return conf; return conf;
} }
private static async Task<Tuple<bool, string>> podcastifyYT(string articleLink, string podcastName)
{
Console.WriteLine($" youtube-podcastifying {articleLink} ({podcastName})");
try
{
var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg";
ytdl.OutputFolder = $"./tmp/Podcasts/{podcastName}";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
var sw = new Stopwatch();
sw.Start();
var res = await ytdl.RunAudioDownload(articleLink);
sw.Stop();
var outputStr = $"{(res.Success ? "Success" : "fail")} in {sw.Elapsed}";
if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
{
outputStr += "\n" + string.Join('\n', res.ErrorOutput);
}
Console.WriteLine($" {(res.Success ? "Success" : "fail")} in {sw.Elapsed} - {res.Data}");
if (res.Success && !res.Data.EndsWith(".mp3"))
{
Console.WriteLine(" must convert audio");
sw.Reset();
var outputFilename = res.Data.Substring(0, res.Data.LastIndexOf('.')) + ".mp3";
sw.Start();
var conversionProc = Process.Start("ffmpeg", $"-y -i \"{res.Data}\" \"{outputFilename}\"");
conversionProc.WaitForExit();
sw.Stop();
File.Delete(res.Data);
outputStr += $"\nconverted in {sw.Elapsed}";
}
return new Tuple<bool, string>(res.Success, outputStr);
}
catch (Exception e)
{
Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
return new Tuple<bool, string>(false, $"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
}
}
private static async Task<Tuple<bool, string>> podcastifyAttachment(string attachmentLink, string podcastName, string episodeTitle)
{
Console.WriteLine($" attachment-podcastifying {attachmentLink} ({podcastName})");
try
{
var extensionUpstream = attachmentLink.Substring(attachmentLink.LastIndexOf('.'));
var containingDirectory = $"./tmp/Podcasts/{podcastName}/";
var outputFilename = $"{containingDirectory}{episodeTitle}{extensionUpstream}";
if (!Directory.Exists(containingDirectory))
{
Directory.CreateDirectory(containingDirectory);
}
var downloader = new HttpClient();
var sw = new Stopwatch();
sw.Start();
File.WriteAllBytes(outputFilename, await (await downloader.GetAsync(attachmentLink)).Content.ReadAsByteArrayAsync());
sw.Stop();
var outputStr = $"{(File.Exists(outputFilename) ? "Success" : "fail")} in {sw.Elapsed}";
Console.WriteLine($" {outputStr} - {outputFilename}");
if (File.Exists(outputFilename) && extensionUpstream != ".mp3")
{
Console.WriteLine(" must convert audio");
sw.Reset();
var targetFilename = outputFilename.Substring(0, outputFilename.LastIndexOf('.')) + ".mp3";
sw.Start();
var conversionProc = Process.Start("ffmpeg", $"-y -i \"{outputFilename}\" \"{targetFilename}\"");
conversionProc.WaitForExit();
sw.Stop();
File.Delete(outputFilename);
outputStr += $"\nconverted in {sw.Elapsed}";
}
return new Tuple<bool, string>(true, outputStr);
}
catch (Exception e)
{
Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
return new Tuple<bool, string>(false, $"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
}
}
} }
} }

View File

@ -1,88 +1,102 @@
using System.Diagnostics; // using System.Diagnostics;
using ttrss_co_client.ttrss; // using ttrss_co_client.ttrss;
using ttrss_co_client.ttrss.datastructures; // using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.tasks // namespace ttrss_co_client.tasks
{ // {
///<summary>podcast, with attachment that is podcast</summary> // ///<summary>download directly from an RSS attachment</summary>
public class PodcastifyAttachment : Phase1Task // public class PodcastifyAttachment : Phase1Task
{ // {
public override string TaskName => "podcastifyAttachment"; // public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle) // {
{
Console.WriteLine($" podcast-is-attachment: {headline.link.ToString()}");
var myGuid = Guid.NewGuid().ToString();
var workingFolder = $"{Conf.WorkingDirectory}/{myGuid}";
var sw = new Stopwatch();
if(!Path.Exists(workingFolder))
{
Directory.CreateDirectory(workingFolder);
}
try
{
var attachmentLink = headline.attachments.Select(a => a.content_url)?.FirstOrDefault().ToString();
var extensionUpstream = attachmentLink.Substring(attachmentLink.LastIndexOf('.'));
var downloadPath = Path.Combine(workingFolder, headline.title) + extensionUpstream;
var downloader = new HttpClient();
sw.Start();
File.WriteAllBytes(downloadPath, await (await downloader.GetAsync(attachmentLink)).Content.ReadAsByteArrayAsync());
sw.Stop();
var outputStr = $"{(File.Exists(downloadPath) ? "Success" : "fail")} in {sw.Elapsed}";
Console.WriteLine($" {attachmentLink}\n{outputStr}"); // // case "podcastifyAttachment":
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id); // // nameLabel = labelsWRTArticle.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true && l.@checked);
Console.WriteLine($" {headline.title}: download trigger label removed"); // // podcastName = nameLabel?.caption.Substring(conf.PodcastTitlePrefix.Length)
// // ?? hl.feed_title;
// // var attachmentLink = hl.attachments.Select(a => a.content_url)?.FirstOrDefault();
// // var ATTpodcastifyResult = await podcastifyAttachment(attachmentLink.ToString(), podcastName, hl.title);
// // await ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToString("o")}] - {ATTpodcastifyResult.Item2}", hl.id);
// // if (ATTpodcastifyResult.Item1 == true)
// // {
// // Console.WriteLine($" {hl.title} -> podcastify (att) success, removing labels");
// // await ttrssClient.SetArticleLabel(
// // labelsWRTArticle.First(l => l.caption == action.triggerlabelCaption).id,
// // false,
// // hl.id);
// // if (nameLabel != null)
// // {
// // await ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id);
// // }
// // }
// // else
// // {
// // Console.WriteLine($" {hl.title} -> podcastify (att) failed");
// // }
// // break;
var podcastTitle = headline.feed_title;
var titlingLabel = labelsWRTArticle.FirstOrDefault(l => l.@checked && l.caption?.ToLower().StartsWith(Conf.PodcastTitlePrefix) == true);
if(titlingLabel != null)
{
await TtrssClient.SetArticleLabel(titlingLabel.id, false, headline.id);
Console.WriteLine($" {headline.title}: podcast titling label removed");
podcastTitle = titlingLabel.caption.Substring(Conf.PodcastTitlePrefix.Length);
}
Console.WriteLine($"article {headline.id} - podcastifying; {podcastTitle}");
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - podcastify attachment (dl): {outputStr}", headline.id);
if (!File.Exists(downloadPath))
{
return null;
}
else
{
Console.WriteLine($"{attachmentLink} downloaded.");
var toReturn = new WorkOrder() // // Console.WriteLine($" youtube podcastify: {headline.link.ToString()}");
{ // // var myGuid = Guid.NewGuid();
articleId = headline.id,//<-- that way later tasks can update the note // // var ytdl = new YoutubeDLSharp.YoutubeDL();
Phase2TaskList = new Dictionary<int, string>(), // // ytdl.YoutubeDLPath = "yt-dlp";
data = new Dictionary<string, string>(), // // ytdl.FFmpegPath = "ffmpeg";
guid = Guid.Parse(myGuid) // // ytdl.OutputFolder = $"{Conf.WorkingDirectory}/{myGuid}";
}; // // ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
toReturn.data["path"] = downloadPath; // // var sw = new Stopwatch();
toReturn.Phase2TaskList[1] = "filemovePublish";
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/Podcasts/{podcastTitle}/{Path.GetFileName(downloadPath)}";
if(extensionUpstream != ".mp3")
{
Console.WriteLine($"{headline.title} needs conversion task.");
toReturn.Phase2TaskList[0] = "convert";
toReturn.data["conversion-target"] = downloadPath.Substring(0, downloadPath.LastIndexOf('.')) + ".mp3";
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/Podcasts/{podcastTitle}/{Path.GetFileName(toReturn.data["conversion-target"])}";
}
// // try
// // {
// // sw.Start();
// // var res = await ytdl.RunVideoDownload(headline.link.ToString(), overrideOptions: new YoutubeDLSharp.Options.OptionSet() { });
// // sw.Stop();
return toReturn; // // var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}";
} // // if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
} // // {
catch (Exception e) // // outputStr += "\n" + string.Join('\n', res.ErrorOutput);
{ // // }
Console.Error.WriteLine($"fatal error in podcast attachment DL for {headline.link}"); // // Console.WriteLine($" {headline.link} -> {res.Data}\n{outputStr}");
Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}"); // // await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - fatal error {e.ToString()}: {e.Message}.\n{e.StackTrace}", headline.id); // // Console.WriteLine($" {headline.title}: label removed");
return null; // // if (!res.Success)
} // // {
} // // await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - standard dl failed; {string.Join('\n', res.ErrorOutput)}", hl.id);
} // // return null;
} // // }
// // else
// // {
// // var outputFilename = res.Data;
// // Console.WriteLine($"{headline.title} downloaded, shipping off to conversion. That task can determine if there's no work.");
// // var toReturn = new WorkOrder()
// // {
// // articleId = headline.id, //<-- that way adblocker, if I should want to suppress it, can be labelled on ttrss
// // nextTask = "convert",
// // data = new Dictionary<string, string>()
// // };
// // toReturn.data["path"] = outputFilename;
// // toReturn.data["target"] = outputFilename.Substring(0, res.Data.LastIndexOf('.')) + ".mp4";
// // return toReturn;
// // // sw.Reset();
// // // outputFilename =
// // // sw.Start();
// // // var conversionProc = Process.Start("ffmpeg", $"-y -i \"{res.Data}\" \"{outputFilename}\"");
// // // conversionProc.WaitForExit();
// // // sw.Stop();
// // // Console.WriteLine($" converted {res.Data} -> {outputFilename}");
// // // File.Delete(res.Data);
// // //outputStr += $"\nconverted in {sw.Elapsed}";
// // }
// // }
// // catch (Exception e)
// // {
// // Console.Error.WriteLine($"fatal error in standard DL for {headline.link}");
// // Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
// // await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - fatal error {e.ToString()}: {e.Message}.\n{e.StackTrace}", hl.id);
// // return null;
// // }
// }
// }
// }

View File

@ -1,91 +1,99 @@
using System.Diagnostics; // using System.Diagnostics;
using ttrss_co_client.ttrss; // using ttrss_co_client.ttrss;
using ttrss_co_client.ttrss.datastructures; // using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.tasks // namespace ttrss_co_client.tasks
{ // {
///<summary>download from YT</summary> // ///<summary>download from YT, to be ripped into a podcast</summary>
public class PodcastifyYT : Phase1Task // public class PodcastifyYT : Phase1Task
{ // {
public override string TaskName => "podcastifyYT"; // public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle) // {
{
Console.WriteLine($" YT podcastify: {headline.link.ToString()}");
var myGuid = Guid.NewGuid().ToString();
var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg";
ytdl.OutputFolder = $"{Conf.WorkingDirectory}/{myGuid}";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
var sw = new Stopwatch();
if(!Path.Exists(ytdl.OutputFolder))
{
Directory.CreateDirectory(ytdl.OutputFolder);
}
try // // case "podcastifyYT":
{ // // nameLabel = labelsWRTArticle.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true && l.@checked);
sw.Start(); // // podcastName = nameLabel?.caption.Substring(conf.PodcastTitlePrefix.Length)
var res = await ytdl.RunVideoDownload(headline.link.ToString()); // // ?? hl.feed_title;
sw.Stop(); // // var YTpodcastifyResult = await podcastifyYT(hl.link.ToString(), podcastName);
// // await ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToString("o")}] - {YTpodcastifyResult.Item2}", hl.id);
// // if (YTpodcastifyResult.Item1 == true)
// // {
// // Console.WriteLine($" {hl.title} -> podcastify (YT) success, removing labels");
// // await ttrssClient.SetArticleLabel(
// // labelsWRTArticle.First(l => l.caption == action.triggerlabelCaption).id,
// // false,
// // hl.id);
// // if (nameLabel != null)
// // {
// // await ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id);
// // }
// // }
// // else
// // {
// // Console.WriteLine($" {hl.title} -> podcastify (YT) failed");
// // }
// // break;
var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}"; // // Console.WriteLine($" youtube podcastify: {headline.link.ToString()}");
if (res.ErrorOutput != null && res.ErrorOutput.Length > 0) // // var myGuid = Guid.NewGuid();
{ // // var ytdl = new YoutubeDLSharp.YoutubeDL();
outputStr += "\n" + string.Join('\n', res.ErrorOutput); // // ytdl.YoutubeDLPath = "yt-dlp";
} // // ytdl.FFmpegPath = "ffmpeg";
Console.WriteLine($" {headline.link} -> {res.Data}\n{outputStr}"); // // ytdl.OutputFolder = $"{Conf.WorkingDirectory}/{myGuid}";
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id); // // ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
Console.WriteLine($" {headline.title}: download trigger label removed"); // // var sw = new Stopwatch();
var podcastTitle = headline.feed_title; // // try
var titlingLabel = labelsWRTArticle.FirstOrDefault(l => l.@checked && l.caption?.ToLower().StartsWith(Conf.PodcastTitlePrefix) == true); // // {
if(titlingLabel != null) // // sw.Start();
{ // // var res = await ytdl.RunVideoDownload(headline.link.ToString(), overrideOptions: new YoutubeDLSharp.Options.OptionSet() { });
await TtrssClient.SetArticleLabel(titlingLabel.id, false, headline.id); // // sw.Stop();
podcastTitle = titlingLabel.caption.Substring(Conf.PodcastTitlePrefix.Length);
}
Console.WriteLine($"article {headline.id} - podcastifying; {podcastTitle}");
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - YT podcastify (dl) {(res.Success ? "success" : "failure")}; {string.Join('\n', res.ErrorOutput)}", headline.id);
if (!res.Success)
{
return null;
}
else
{
var outputFilename = res.Data;
Console.WriteLine($"{headline.title} downloaded.");
var toReturn = new WorkOrder() // // var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}";
{ // // if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
articleId = headline.id,//<-- that way later tasks can update the note // // {
Phase2TaskList = new Dictionary<int, string>(), // // outputStr += "\n" + string.Join('\n', res.ErrorOutput);
data = new Dictionary<string, string>(), // // }
guid = Guid.Parse(myGuid) // // Console.WriteLine($" {headline.link} -> {res.Data}\n{outputStr}");
}; // // await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
toReturn.data["path"] = outputFilename; // // Console.WriteLine($" {headline.title}: label removed");
if(!outputFilename.EndsWith(".mp3")) // // if (!res.Success)
{ // // {
Console.WriteLine($"{headline.title} needs conversion task."); // // await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - standard dl failed; {string.Join('\n', res.ErrorOutput)}", hl.id);
toReturn.Phase2TaskList[0] = "convert"; // // return null;
toReturn.data["conversion-target"] = outputFilename.Substring(0, res.Data.LastIndexOf('.')) + ".mp3"; // // }
} // // else
// // {
// // var outputFilename = res.Data;
// // Console.WriteLine($"{headline.title} downloaded, shipping off to conversion. That task can determine if there's no work.");
// // var toReturn = new WorkOrder()
// // {
// // articleId = headline.id, //<-- that way adblocker, if I should want to suppress it, can be labelled on ttrss
// // nextTask = "convert",
// // data = new Dictionary<string, string>()
// // };
// // toReturn.data["path"] = outputFilename;
// // toReturn.data["target"] = outputFilename.Substring(0, res.Data.LastIndexOf('.')) + ".mp4";
// // return toReturn;
// // // sw.Reset();
// // // outputFilename =
// // // sw.Start();
// // // var conversionProc = Process.Start("ffmpeg", $"-y -i \"{res.Data}\" \"{outputFilename}\"");
// // // conversionProc.WaitForExit();
// // // sw.Stop();
// // // Console.WriteLine($" converted {res.Data} -> {outputFilename}");
// // // File.Delete(res.Data);
// // //outputStr += $"\nconverted in {sw.Elapsed}";
// // }
// // }
// // catch (Exception e)
// // {
// // Console.Error.WriteLine($"fatal error in standard DL for {headline.link}");
// // Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
// // await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - fatal error {e.ToString()}: {e.Message}.\n{e.StackTrace}", hl.id);
// // return null;
// // }
toReturn.Phase2TaskList[1] = "sponsorblock"; // }
toReturn.Phase2TaskList[2] = "filemovePublish"; // }
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/Podcasts/{podcastTitle}/{Path.GetFileName(toReturn.data["conversion-target"])}"; // }
return toReturn;
}
}
catch (Exception e)
{
Console.Error.WriteLine($"fatal error in yt podcastify DL for {headline.link}");
Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - fatal error {e.ToString()}: {e.Message}.\n{e.StackTrace}", headline.id);
return null;
}
}
}
}

View File

@ -18,7 +18,7 @@ namespace ttrss_co_client.tasks
} }
File.Move(workOrder.data["path"], workOrder.data["publish-target"], true); File.Move(workOrder.data["path"], workOrder.data["publish-target"], true);
var article = (await TtrssClient.GetArticles(workOrder.articleId))?.FirstOrDefault(); var article = (await TtrssClient.GetArticles(workOrder.articleId))?.FirstOrDefault();
await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] - copied", article.id); await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToShortDateString()}] - copied", article.id);
return new Tuple<TaskStatus, WorkOrder>(TaskStatus.Done, workOrder); return new Tuple<TaskStatus, WorkOrder>(TaskStatus.Done, workOrder);
} }
} }

View File

@ -78,7 +78,6 @@ namespace ttrss_co_client.tasks
} }
} }
contentSegments.Add(new Tuple<double, double>(previousEdge, segments.First().videoDuration)); contentSegments.Add(new Tuple<double, double>(previousEdge, segments.First().videoDuration));
contentSegments = contentSegments.Except(contentSegments.Where(tup => tup.Item2 - tup.Item1 < 0.5)).ToList();
#region ffmpeg via intermediate files #region ffmpeg via intermediate files
var intermediateCount = 0; var intermediateCount = 0;