From 6432ad672be39bd11c63ebef9bb292c71cb0d14c Mon Sep 17 00:00:00 2001 From: Adam R Grey Date: Wed, 5 Apr 2023 01:44:46 -0400 Subject: [PATCH] per feed internal actions --- Configuration.cs | 7 ++++ Program.cs | 74 +++++++++++++++++++++++++++++++++++++---- sample-appsettings.json | 9 ++++- ttrss-co-client.csproj | 5 +-- ttrss/ApiClient.cs | 22 ++++++++++-- 5 files changed, 104 insertions(+), 13 deletions(-) diff --git a/Configuration.cs b/Configuration.cs index 9f6d768..3309128 100644 --- a/Configuration.cs +++ b/Configuration.cs @@ -7,5 +7,12 @@ namespace ttrss_co_client public Uri BaseURI { get; set; } public string Username { get; set; } public string Password { get; set; } + public string CopilotDoneCaption { get; set; } + public IEnumerable feedActions{get;set;} + public class FeedAction + { + public string feedurl { get; set; } + public string command { get; set; } + } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index a748178..67d3da6 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using System.Linq; +using System.Diagnostics; namespace ttrss_co_client { @@ -14,16 +15,52 @@ namespace ttrss_co_client var loggedin = await ttrssClient.IsLoggedIn(); Console.WriteLine($"logged in: {loggedin}"); - var apiLevel = await ttrssClient.GetApiLevel(); - Console.WriteLine($"api level: {apiLevel}"); + var doneLabelCaption = conf.CopilotDoneCaption.ToLower(); - + var miscTasks = new List(); + var unreadFeeds = await ttrssClient.GetFeeds(cat_id: -3, unread_only: true); + foreach (var uf in unreadFeeds) + { + var actionForFeed = conf.feedActions.FirstOrDefault(fa => fa.feedurl == uf.feed_url.ToString()); + if (actionForFeed != null) + { + var headlines = await ttrssClient.GetHeadlines(uf.id, view_mode: ttrss.ApiClient.VIEWMODE.Unread); + foreach (var hl in headlines) + { + var doneLabel = (await ttrssClient.GetLabels(hl.id)).First(l => l.caption?.ToLower() == doneLabelCaption); + if (!doneLabel.@checked) + { + var noteString = hl.note; + if (!string.IsNullOrWhiteSpace(noteString)) + { + noteString += $"{hl.note}\n"; + } + switch (actionForFeed.command) + { + case "dl": + var tr = await standardDL(hl.link.ToString()); + miscTasks.Add(ttrssClient.UpdateArticleNote($"[{DateTime.Now.ToLongTimeString()}] - {tr.Item2}", hl.id)); + if (tr.Item1 == true) + { + miscTasks.Add(ttrssClient.SetArticleLabel(doneLabel.id, true, hl.id)); + } + break; + default: + noteString += $"[{DateTime.Now.ToLongTimeString()}] - feed configured but action not recognized"; + miscTasks.Add(ttrssClient.SetArticleLabel(doneLabel.id, true, hl.id)); + break; + } + miscTasks.Add(ttrssClient.UpdateArticleNote(noteString, hl.id)); + } + } + } + } - var loggedout = await ttrssClient.Logout(); - Console.WriteLine($"logged out: {loggedout}"); - loggedin = await ttrssClient.IsLoggedIn(); - Console.WriteLine($"logged in: {loggedin}"); + miscTasks.Add(ttrssClient.Logout()); + Console.WriteLine($"awaiting remaining tasks"); + Task.WaitAll(miscTasks.ToArray()); + Console.WriteLine($"done"); } static Configuration Configure(string configurationPath = "appsettings.json") { @@ -53,5 +90,28 @@ namespace ttrss_co_client return conf; } + private static async Task> standardDL(string articleLink) + { + var ytdl = new YoutubeDLSharp.YoutubeDL(); + ytdl.YoutubeDLPath = "yt-dlp"; + ytdl.FFmpegPath = "ffmpeg"; + ytdl.OutputFolder = "./tmp/"; + ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].mp4"; + var sw = new Stopwatch(); + // var data = await ytdl.RunVideoDataFetch(articleLink); + // if (data.Data.ReleaseDate != null && data.Data.ReleaseDate > DateTime.Now) + // { + // return new Tuple(false, $"release date in future? {data.Data.ReleaseDate}"); + // } + sw.Start(); + var res = await ytdl.RunVideoDownload(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); + } + return new Tuple(res.Success, outputStr); + } } } \ No newline at end of file diff --git a/sample-appsettings.json b/sample-appsettings.json index b150adb..0945a91 100644 --- a/sample-appsettings.json +++ b/sample-appsettings.json @@ -1,5 +1,12 @@ { "BaseUri": "https://ttrss.example.com/api/", "username": "guy who didn't configure", - "password": "sordph1sh" + "password": "sordph1sh", + "feedactions": + [ + { + "feedurl":"youtube.com/a_channel", + "command":"dl" + } + ] } \ No newline at end of file diff --git a/ttrss-co-client.csproj b/ttrss-co-client.csproj index e487660..64602a3 100644 --- a/ttrss-co-client.csproj +++ b/ttrss-co-client.csproj @@ -8,8 +8,9 @@ disable - - + + + diff --git a/ttrss/ApiClient.cs b/ttrss/ApiClient.cs index fa8fae1..83134e6 100644 --- a/ttrss/ApiClient.cs +++ b/ttrss/ApiClient.cs @@ -6,7 +6,6 @@ using ttrss_co_client.ttrss.messages; using ttrss_co_client.ttrss.datastructures; // https://tt-rss.org/wiki/ApiReference -//TODO: a lot of these are a given at API level 1, so double check all necessary API levels namespace ttrss_co_client.ttrss { @@ -161,7 +160,7 @@ namespace ttrss_co_client.ttrss public enum SORTORDER { Default, OldestFirst, NewestFirst } public async Task> GetHeadlines( int feed_id, - bool is_cat, + bool is_cat = false, int limit = 60, int skip = 0, /*string filter,*/ @@ -190,6 +189,23 @@ namespace ttrss_co_client.ttrss assertApiLevel(4); } + string viewmodestr = "all_articles"; + switch(view_mode) + { + case VIEWMODE.Unread: + viewmodestr = "unread"; + break; + case VIEWMODE.Adaptive: + viewmodestr = "adaptive"; + break; + case VIEWMODE.Marked: + viewmodestr = "marked"; + break; + case VIEWMODE.Updated: + viewmodestr = "updated"; + break; + } + string sortOrderString = ""; if (order_by != SORTORDER.Default) { @@ -225,7 +241,7 @@ namespace ttrss_co_client.ttrss skip = skip, show_excerpt = show_excerpt, show_content = show_content, - view_mode = view_mode.ToString("D"), + view_mode = viewmodestr, include_attachments = include_attachments, since_id = since_id, include_nested = include_nested,