podcastifying distinguished between ripping a YT video and directly-ish downloading

This commit is contained in:
Adam R Grey 2023-04-05 16:25:58 -04:00
parent fe273d9a77
commit f4ac000d73
4 changed files with 158 additions and 66 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
tmp/
# ---> VisualStudio # ---> VisualStudio
## Ignore Visual Studio temporary files, build results, and ## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons. ## files generated by popular Visual Studio add-ons.

View File

@ -20,18 +20,20 @@ namespace ttrss_co_client
foreach (var uf in unreadFeeds) foreach (var uf in unreadFeeds)
{ {
Console.WriteLine($"unread feed: {uf.title}"); Console.WriteLine($"unread feed: {uf.title}");
var headlines = await ttrssClient.GetHeadlines(uf.id, view_mode: ttrss.ApiClient.VIEWMODE.Unread); var headlines = await ttrssClient.GetHeadlines(uf.id, view_mode: ttrss.ApiClient.VIEWMODE.Unread, include_attachments: true);
foreach (var hl in headlines) foreach (var hl in headlines)
{ {
var labelsWRTFeed = (await ttrssClient.GetLabels(hl.id)); var labelsWRTFeed = (await ttrssClient.GetLabels(hl.id));
var actionsForFeed = conf.feedActions.Where(fa => var actionsForFeed = conf.feedActions.Where(fa =>
labelsWRTFeed.Where(l => l.@checked).Select(l => l.caption).Contains(fa.triggerlabelCaption))?.ToList(); labelsWRTFeed.Where(l => l.@checked).Select(l => l.caption).Contains(fa.triggerlabelCaption))?.ToList();
if(actionsForFeed != null && actionsForFeed.Any()) if (actionsForFeed != null && actionsForFeed.Any())
{ {
foreach(var action in actionsForFeed) foreach (var action in actionsForFeed)
{ {
Console.WriteLine($" {hl.title} -> action: {action.command}"); Console.WriteLine($" {hl.title} -> action: {action.command}");
var noteString = hl.note; var noteString = hl.note;
ttrss.datastructures.Label nameLabel;
string podcastName;
if (!string.IsNullOrWhiteSpace(noteString)) if (!string.IsNullOrWhiteSpace(noteString))
{ {
noteString += $"{hl.note}\n"; noteString += $"{hl.note}\n";
@ -55,28 +57,53 @@ namespace ttrss_co_client
Console.WriteLine($" {hl.title} -> dl failed"); Console.WriteLine($" {hl.title} -> dl failed");
} }
break; break;
case "podcastify": case "podcastifyYT":
var nameLabel = labelsWRTFeed.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true); nameLabel = labelsWRTFeed.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true && l.@checked);
var podcastName = nameLabel?.caption.Substring(conf.PodcastTitlePrefix.Length) podcastName = nameLabel?.caption.Substring(conf.PodcastTitlePrefix.Length)
?? hl.feed_title; ?? hl.feed_title;
var podcastifyResult = await podcastify(hl.link.ToString(), podcastName); var YTpodcastifyResult = await podcastifyYT(hl.link.ToString(), podcastName);
miscTasks.Add(ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {podcastifyResult.Item2}", hl.id)); miscTasks.Add(ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {YTpodcastifyResult.Item2}", hl.id));
if (podcastifyResult.Item1 == true) if (YTpodcastifyResult.Item1 == true)
{ {
Console.WriteLine($" {hl.title} -> podcastify success, removing labels"); Console.WriteLine($" {hl.title} -> podcastify (YT) success, removing labels");
miscTasks.Add( miscTasks.Add(
ttrssClient.SetArticleLabel( ttrssClient.SetArticleLabel(
labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id, labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id,
false, false,
hl.id)); hl.id));
if(nameLabel != null) if (nameLabel != null)
{ {
miscTasks.Add(ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id)); miscTasks.Add(ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id));
} }
} }
else else
{ {
Console.WriteLine($" {hl.title} -> podcastify failed"); Console.WriteLine($" {hl.title} -> podcastify (YT) failed");
}
break;
case "podcastifyAttachment":
nameLabel = labelsWRTFeed.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);
miscTasks.Add(ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {ATTpodcastifyResult.Item2}", hl.id));
if (ATTpodcastifyResult.Item1 == true)
{
Console.WriteLine($" {hl.title} -> podcastify (att) success, removing labels");
miscTasks.Add(
ttrssClient.SetArticleLabel(
labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id,
false,
hl.id));
if (nameLabel != null)
{
miscTasks.Add(ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id));
}
}
else
{
Console.WriteLine($" {hl.title} -> podcastify (att) failed");
} }
break; break;
default: default:
@ -96,12 +123,12 @@ namespace ttrss_co_client
Console.WriteLine($"done, moving files from temporary location"); Console.WriteLine($"done, moving files from temporary location");
var resulted = Directory.GetFiles("tmp", "*.*", SearchOption.AllDirectories); var resulted = Directory.GetFiles("tmp", "*.*", SearchOption.AllDirectories);
if(resulted.Count() > 0) if (resulted.Count() > 0)
{ {
foreach(var f in resulted) foreach (var f in resulted)
{ {
var moveTarget = Path.Combine(conf.OnDoneCopy, f.Substring("tmp/".Length)); var moveTarget = Path.Combine(conf.OnDoneCopy, f.Substring("tmp/".Length));
if(!Path.Exists(Path.GetDirectoryName(moveTarget))) if (!Path.Exists(Path.GetDirectoryName(moveTarget)))
{ {
Directory.CreateDirectory(Path.GetDirectoryName(moveTarget)); Directory.CreateDirectory(Path.GetDirectoryName(moveTarget));
} }
@ -141,6 +168,8 @@ namespace ttrss_co_client
private static async Task<Tuple<bool, string>> standardDL(string articleLink) private static async Task<Tuple<bool, string>> standardDL(string articleLink)
{ {
Console.WriteLine($" standard downloading {articleLink}"); Console.WriteLine($" standard downloading {articleLink}");
try
{
var ytdl = new YoutubeDLSharp.YoutubeDL(); var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp"; ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg"; ytdl.FFmpegPath = "ffmpeg";
@ -151,18 +180,18 @@ namespace ttrss_co_client
var res = await ytdl.RunVideoDownload(articleLink); var res = await ytdl.RunVideoDownload(articleLink);
sw.Stop(); sw.Stop();
var outputStr = $"{(res.Success ? "Success" : "fail")} in {sw.Elapsed}"; var outputStr = $"{(res.Success ? "Success" : "fail")} in {sw.Elapsed}";
if(res.ErrorOutput != null && res.ErrorOutput.Length > 0) if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
{ {
outputStr += "\n" + string.Join('\n', res.ErrorOutput); outputStr += "\n" + string.Join('\n', res.ErrorOutput);
} }
Console.WriteLine($" download {(res.Success ? "success" : "failed")}: {articleLink} -> {res.Data}"); Console.WriteLine($" download {(res.Success ? "success" : "failed")}: {articleLink} -> {res.Data}");
if(!res.Data.EndsWith(".mp4")) if (!res.Data.EndsWith(".mp4"))
{ {
Console.WriteLine("must convert video"); Console.WriteLine(" must convert video");
sw.Reset(); sw.Reset();
var outputFilename = res.Data.Substring(0, res.Data.LastIndexOf('.')) + ".mp4"; var outputFilename = res.Data.Substring(0, res.Data.LastIndexOf('.')) + ".mp4";
sw.Start(); sw.Start();
var conversionProc =Process.Start("ffmpeg", $"-y -i \"{res.Data}\" \"{outputFilename}\""); var conversionProc = Process.Start("ffmpeg", $"-y -i \"{res.Data}\" \"{outputFilename}\"");
conversionProc.WaitForExit(); conversionProc.WaitForExit();
sw.Stop(); sw.Stop();
Console.WriteLine($" converted {res.Data} -> {outputFilename}"); Console.WriteLine($" converted {res.Data} -> {outputFilename}");
@ -170,8 +199,18 @@ namespace ttrss_co_client
outputStr += $"\nconverted in {sw.Elapsed}"; outputStr += $"\nconverted in {sw.Elapsed}";
} }
return new Tuple<bool, string>(res.Success, outputStr); return new Tuple<bool, string>(res.Success, outputStr);
} }
private static async Task<Tuple<bool, string>> podcastify(string articleLink, string podcastName) 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>> podcastifyYT(string articleLink, string podcastName)
{
Console.WriteLine($" youtube-podcastifying {articleLink} ({podcastName})");
try
{ {
var ytdl = new YoutubeDLSharp.YoutubeDL(); var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp"; ytdl.YoutubeDLPath = "yt-dlp";
@ -183,16 +222,18 @@ namespace ttrss_co_client
var res = await ytdl.RunAudioDownload(articleLink); var res = await ytdl.RunAudioDownload(articleLink);
sw.Stop(); sw.Stop();
var outputStr = $"{(res.Success ? "Success" : "fail")} in {sw.Elapsed}"; var outputStr = $"{(res.Success ? "Success" : "fail")} in {sw.Elapsed}";
if(res.ErrorOutput != null && res.ErrorOutput.Length > 0) if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
{ {
outputStr += "\n" + string.Join('\n', res.ErrorOutput); outputStr += "\n" + string.Join('\n', res.ErrorOutput);
} }
if(!res.Data.EndsWith(".mp3")) 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(); sw.Reset();
var outputFilename = res.Data.Substring(0, res.Data.LastIndexOf('.')) + ".mp3"; var outputFilename = res.Data.Substring(0, res.Data.LastIndexOf('.')) + ".mp3";
sw.Start(); sw.Start();
var conversionProc =Process.Start("ffmpeg", $"-y -i \"{res.Data}\" \"{outputFilename}\""); var conversionProc = Process.Start("ffmpeg", $"-y -i \"{res.Data}\" \"{outputFilename}\"");
conversionProc.WaitForExit(); conversionProc.WaitForExit();
sw.Stop(); sw.Stop();
File.Delete(res.Data); File.Delete(res.Data);
@ -200,5 +241,50 @@ namespace ttrss_co_client
} }
return new Tuple<bool, string>(res.Success, outputStr); 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

@ -11,8 +11,12 @@
"command":"dl" "command":"dl"
}, },
{ {
"triggerlabelCaption":"podcastify plz", "triggerlabelCaption":"podcastify-yt plz",
"command":"podcastify" "command":"podcastifyYT"
},
{
"triggerlabelCaption":"podcastify-attachment plz",
"command":"podcastifyAttachment"
} }
] ]
} }

View File

@ -18,6 +18,7 @@ namespace ttrss_co_client.ttrss.datastructures
public IEnumerable<string> tags { get; set; } public IEnumerable<string> tags { get; set; }
public string content { get; set; } public string content { get; set; }
///See <cref name="ttrss_co_client.ttrss.datastructures.Label" /> ///See <cref name="ttrss_co_client.ttrss.datastructures.Label" />
public IEnumerable<Attachment> attachments { get; set; }
public IEnumerable<IEnumerable<string>> labels { get; set; } public IEnumerable<IEnumerable<string>> labels { get; set; }
public string feed_title { get; set; } public string feed_title { get; set; }
public int comments_count { get; set; } public int comments_count { get; set; }