podcast from attachment works

This commit is contained in:
Adam R Grey 2023-04-09 18:00:28 -04:00
parent 3b11384708
commit 036dff3b12
4 changed files with 83 additions and 176 deletions

View File

@ -198,84 +198,5 @@ namespace ttrss_co_client
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,102 +1,88 @@
// using System.Diagnostics;
// using ttrss_co_client.ttrss;
// using ttrss_co_client.ttrss.datastructures;
using System.Diagnostics;
using ttrss_co_client.ttrss;
using ttrss_co_client.ttrss.datastructures;
// namespace ttrss_co_client.tasks
// {
// ///<summary>download directly from an RSS attachment</summary>
// public class PodcastifyAttachment : Phase1Task
// {
// public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
// {
namespace ttrss_co_client.tasks
{
///<summary>podcast, with attachment that is podcast</summary>
public class PodcastifyAttachment : Phase1Task
{
public override string TaskName => "podcastifyAttachment";
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}";
// // case "podcastifyAttachment":
// // nameLabel = labelsWRTArticle.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true && l.@checked);
// // 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;
Console.WriteLine($" {attachmentLink}\n{outputStr}");
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
Console.WriteLine($" {headline.title}: download trigger label removed");
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.");
// // Console.WriteLine($" youtube podcastify: {headline.link.ToString()}");
// // var myGuid = Guid.NewGuid();
// // 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();
var toReturn = new WorkOrder()
{
articleId = headline.id,//<-- that way later tasks can update the note
Phase2TaskList = new Dictionary<int, string>(),
data = new Dictionary<string, string>(),
guid = Guid.Parse(myGuid)
};
toReturn.data["path"] = downloadPath;
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();
// // var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}";
// // if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
// // {
// // outputStr += "\n" + string.Join('\n', res.ErrorOutput);
// // }
// // Console.WriteLine($" {headline.link} -> {res.Data}\n{outputStr}");
// // await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
// // Console.WriteLine($" {headline.title}: label removed");
// // 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;
// // }
// }
// }
// }
return toReturn;
}
}
catch (Exception e)
{
Console.Error.WriteLine($"fatal error in podcast attachment 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

@ -81,7 +81,7 @@ namespace ttrss_co_client.tasks
}
catch (Exception e)
{
Console.Error.WriteLine($"fatal error in standard DL for {headline.link}");
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);
var article = (await TtrssClient.GetArticles(workOrder.articleId))?.FirstOrDefault();
await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToShortDateString()}] - copied", article.id);
await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] - copied", article.id);
return new Tuple<TaskStatus, WorkOrder>(TaskStatus.Done, workOrder);
}
}