more debug info, and log out after awaiting final tasks

like updating notes
This commit is contained in:
Adam R Grey 2023-04-05 12:43:58 -04:00
parent c4b6a79273
commit fe273d9a77
2 changed files with 80 additions and 55 deletions

View File

@ -19,69 +19,80 @@ namespace ttrss_co_client
var unreadFeeds = await ttrssClient.GetFeeds(cat_id: -3, unread_only: true);
foreach (var uf in unreadFeeds)
{
var headlines = await ttrssClient.GetHeadlines(uf.id, view_mode: ttrss.ApiClient.VIEWMODE.Unread);
foreach (var hl in headlines)
Console.WriteLine($"unread feed: {uf.title}");
var headlines = await ttrssClient.GetHeadlines(uf.id, view_mode: ttrss.ApiClient.VIEWMODE.Unread);
foreach (var hl in headlines)
{
var labelsWRTFeed = (await ttrssClient.GetLabels(hl.id));
var actionsForFeed = conf.feedActions.Where(fa =>
labelsWRTFeed.Where(l => l.@checked).Select(l => l.caption).Contains(fa.triggerlabelCaption))?.ToList();
if(actionsForFeed != null && actionsForFeed.Any())
{
var labelsWRTFeed = (await ttrssClient.GetLabels(hl.id));
var actionsForFeed = conf.feedActions.Where(fa =>
labelsWRTFeed.Where(l => l.@checked).Select(l => l.caption).Contains(fa.triggerlabelCaption))?.ToList();
if(actionsForFeed != null && actionsForFeed.Any())
foreach(var action in actionsForFeed)
{
foreach(var action in actionsForFeed)
Console.WriteLine($" {hl.title} -> action: {action.command}");
var noteString = hl.note;
if (!string.IsNullOrWhiteSpace(noteString))
{
var noteString = hl.note;
if (!string.IsNullOrWhiteSpace(noteString))
{
noteString += $"{hl.note}\n";
}
switch (action.command)
{
case "dl":
var stdDLResult = await standardDL(hl.link.ToString());
miscTasks.Add(ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {stdDLResult.Item2}", hl.id));
if (stdDLResult.Item1 == true)
{
miscTasks.Add(
ttrssClient.SetArticleLabel(
labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id,
false,
hl.id));
}
break;
case "podcastify":
var nameLabel = labelsWRTFeed.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true);
var podcastName = nameLabel?.caption.Substring(conf.PodcastTitlePrefix.Length)
?? hl.feed_title;
var podcastifyResult = await podcastify(hl.link.ToString(), podcastName);
miscTasks.Add(ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {podcastifyResult.Item2}", hl.id));
if (podcastifyResult.Item1 == true)
{
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));
}
}
break;
default:
noteString += $"[{DateTime.Now.ToLongTimeString()}] - feed configured but action not recognized";
break;
}
miscTasks.Add(ttrssClient.UpdateArticleNote(noteString, hl.id));
noteString += $"{hl.note}\n";
}
switch (action.command)
{
case "dl":
var stdDLResult = await standardDL(hl.link.ToString());
miscTasks.Add(ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {stdDLResult.Item2}", hl.id));
if (stdDLResult.Item1 == true)
{
Console.WriteLine($" {hl.title} -> dl success, removing label");
miscTasks.Add(
ttrssClient.SetArticleLabel(
labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id,
false,
hl.id));
}
else
{
Console.WriteLine($" {hl.title} -> dl failed");
}
break;
case "podcastify":
var nameLabel = labelsWRTFeed.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true);
var podcastName = nameLabel?.caption.Substring(conf.PodcastTitlePrefix.Length)
?? hl.feed_title;
var podcastifyResult = await podcastify(hl.link.ToString(), podcastName);
miscTasks.Add(ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {podcastifyResult.Item2}", hl.id));
if (podcastifyResult.Item1 == true)
{
Console.WriteLine($" {hl.title} -> podcastify 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 failed");
}
break;
default:
noteString += $"[{DateTime.Now.ToLongTimeString()}] - feed configured but action not recognized";
break;
}
miscTasks.Add(ttrssClient.UpdateArticleNote(noteString, hl.id));
}
}
}
}
miscTasks.Add(ttrssClient.Logout());
Console.WriteLine($"awaiting remaining download tasks");
Task.WaitAll(miscTasks.ToArray());
await ttrssClient.Logout();
Console.WriteLine($"done, moving files from temporary location");
var resulted = Directory.GetFiles("tmp", "*.*", SearchOption.AllDirectories);
@ -129,11 +140,12 @@ namespace ttrss_co_client
private static async Task<Tuple<bool, string>> standardDL(string articleLink)
{
Console.WriteLine($" standard downloading {articleLink}");
var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg";
ytdl.OutputFolder = "./tmp/recent episodes";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].mp4";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
var sw = new Stopwatch();
sw.Start();
var res = await ytdl.RunVideoDownload(articleLink);
@ -143,6 +155,20 @@ namespace ttrss_co_client
{
outputStr += "\n" + string.Join('\n', res.ErrorOutput);
}
Console.WriteLine($" download {(res.Success ? "success" : "failed")}: {articleLink} -> {res.Data}");
if(!res.Data.EndsWith(".mp4"))
{
Console.WriteLine("must convert video");
sw.Reset();
var outputFilename = res.Data.Substring(0, res.Data.LastIndexOf('.')) + ".mp4";
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}";
}
return new Tuple<bool, string>(res.Success, outputStr);
}
private static async Task<Tuple<bool, string>> podcastify(string articleLink, string podcastName)
@ -154,7 +180,7 @@ namespace ttrss_co_client
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
var sw = new Stopwatch();
sw.Start();
var res = await ytdl.RunVideoDownload(articleLink);
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)

View File

@ -12,5 +12,4 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="youtubedlsharp" Version="0.4.3" />
</ItemGroup>
</Project>