ttrss-co-pilot/Program.cs

204 lines
10 KiB
C#
Raw Permalink Normal View History

2023-04-02 23:32:03 -04:00
using Newtonsoft.Json;
2023-04-03 13:50:33 -04:00
using System.Linq;
2023-04-05 01:44:46 -04:00
using System.Diagnostics;
2023-04-02 23:32:03 -04:00
namespace ttrss_co_client
{
class Program
{
static async Task Main(string[] args)
{
var conf = Configure();
var ttrssClient = new ttrss.ApiClient(conf.BaseURI);
await ttrssClient.Login(conf.Username, conf.Password);
2023-04-03 00:36:53 -04:00
var loggedin = await ttrssClient.IsLoggedIn();
Console.WriteLine($"logged in: {loggedin}");
2023-04-05 01:44:46 -04:00
var miscTasks = new List<Task>();
var unreadFeeds = await ttrssClient.GetFeeds(cat_id: -3, unread_only: true);
foreach (var uf in unreadFeeds)
{
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())
2023-04-05 01:44:46 -04:00
{
foreach(var action in actionsForFeed)
2023-04-05 01:44:46 -04:00
{
Console.WriteLine($" {hl.title} -> action: {action.command}");
var noteString = hl.note;
if (!string.IsNullOrWhiteSpace(noteString))
2023-04-05 01:44:46 -04:00
{
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)
2023-04-05 02:52:26 -04:00
{
miscTasks.Add(ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id));
2023-04-05 02:52:26 -04:00
}
}
else
{
Console.WriteLine($" {hl.title} -> podcastify failed");
}
break;
default:
noteString += $"[{DateTime.Now.ToLongTimeString()}] - feed configured but action not recognized";
break;
2023-04-05 01:44:46 -04:00
}
miscTasks.Add(ttrssClient.UpdateArticleNote(noteString, hl.id));
2023-04-05 01:44:46 -04:00
}
}
}
2023-04-05 01:44:46 -04:00
}
2023-04-03 13:50:33 -04:00
2023-04-03 00:36:53 -04:00
2023-04-05 02:52:26 -04:00
Console.WriteLine($"awaiting remaining download tasks");
2023-04-05 01:44:46 -04:00
Task.WaitAll(miscTasks.ToArray());
await ttrssClient.Logout();
2023-04-05 02:52:26 -04:00
Console.WriteLine($"done, moving files from temporary location");
var resulted = Directory.GetFiles("tmp", "*.*", SearchOption.AllDirectories);
if(resulted.Count() > 0)
{
foreach(var f in resulted)
{
var moveTarget = Path.Combine(conf.OnDoneCopy, f.Substring("tmp/".Length));
if(!Path.Exists(Path.GetDirectoryName(moveTarget)))
{
Directory.CreateDirectory(Path.GetDirectoryName(moveTarget));
}
File.Move(f, moveTarget);
}
}
Console.WriteLine($"done for real");
2023-04-02 23:32:03 -04:00
}
static Configuration Configure(string configurationPath = "appsettings.json")
{
if (!File.Exists(configurationPath))
{
Console.Error.WriteLine($"could not find configuration at {configurationPath}! copying sample to that spot.");
File.Copy("sample-appsettings.json", configurationPath); //and you know what, if that explodes at the OS level, the OS should give you an error
return null;
}
var fileContents = File.ReadAllText(configurationPath);
if (string.IsNullOrWhiteSpace(fileContents))
{
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
File.Copy("sample-appsettings.json", configurationPath, true);
return null;
}
var conf = JsonConvert.DeserializeObject<Configuration>(fileContents);
if (conf == null)
{
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
File.Copy("sample-appsettings.json", configurationPath, true);
return null;
}
return conf;
}
2023-04-05 01:44:46 -04:00
private static async Task<Tuple<bool, string>> standardDL(string articleLink)
{
Console.WriteLine($" standard downloading {articleLink}");
2023-04-05 01:44:46 -04:00
var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg";
2023-04-05 02:52:26 -04:00
ytdl.OutputFolder = "./tmp/recent episodes";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
2023-04-05 01:44:46 -04:00
var sw = new Stopwatch();
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);
}
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}";
}
2023-04-05 01:44:46 -04:00
return new Tuple<bool, string>(res.Success, outputStr);
}
2023-04-05 02:52:26 -04:00
private static async Task<Tuple<bool, string>> podcastify(string articleLink, string podcastName)
{
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);
2023-04-05 02:52:26 -04:00
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);
}
if(!res.Data.EndsWith(".mp3"))
{
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);
}
2023-04-02 23:32:03 -04:00
}
}