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,7 +20,7 @@ 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));
@ -32,6 +32,8 @@ namespace ttrss_co_client
{ {
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,15 +57,15 @@ 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,
@ -76,7 +78,32 @@ namespace ttrss_co_client
} }
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:
@ -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";
@ -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";
@ -187,8 +226,10 @@ namespace ttrss_co_client
{ {
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();
@ -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; }