341 lines
18 KiB
C#
341 lines
18 KiB
C#
using Newtonsoft.Json;
|
|
using System.Linq;
|
|
using System.Diagnostics;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace ttrss_co_client
|
|
{
|
|
class Program
|
|
{
|
|
static async Task Main(string[] args)
|
|
{
|
|
var conf = Configure();
|
|
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}");
|
|
|
|
var ttrssClient = new ttrss.ApiClient(conf.BaseURI);
|
|
await ttrssClient.Login(conf.Username, conf.Password);
|
|
|
|
var loggedin = await ttrssClient.IsLoggedIn();
|
|
Console.WriteLine($"logged in: {loggedin}");
|
|
|
|
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, include_attachments: true);
|
|
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 sponsorCheck = await SponsorCheck(hl);
|
|
if(!sponsorCheck.Item1)
|
|
{
|
|
await ttrssClient.UpdateArticleNote($"{hl.note}\n[{DateTime.Now.ToLongTimeString()}] sponsorcheck: {sponsorCheck.Item2}", hl.id);
|
|
continue;
|
|
}
|
|
foreach (var action in actionsForFeed)
|
|
{
|
|
Console.WriteLine($" {hl.title} -> action: {action.command}");
|
|
var noteString = $"{hl.note}\n[{DateTime.Now.ToLongTimeString()}] sponsorcheck: {sponsorCheck.Item2}";
|
|
ttrss.datastructures.Label nameLabel;
|
|
string podcastName;
|
|
if (!string.IsNullOrWhiteSpace(noteString))
|
|
{
|
|
noteString += $"{hl.note}\n";
|
|
}
|
|
switch (action.command)
|
|
{
|
|
case "dl":
|
|
var stdDLResult = await standardDL(hl.link.ToString());
|
|
await ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {stdDLResult.Item2}", hl.id);
|
|
if (stdDLResult.Item1 == true)
|
|
{
|
|
Console.WriteLine($" {hl.title} -> dl success, removing label");
|
|
await ttrssClient.SetArticleLabel(
|
|
labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id,
|
|
false,
|
|
hl.id);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($" {hl.title} -> dl failed");
|
|
}
|
|
break;
|
|
case "podcastifyYT":
|
|
nameLabel = labelsWRTFeed.FirstOrDefault(l => l.caption?.StartsWith(conf.PodcastTitlePrefix) == true && l.@checked);
|
|
podcastName = nameLabel?.caption.Substring(conf.PodcastTitlePrefix.Length)
|
|
?? hl.feed_title;
|
|
var YTpodcastifyResult = await podcastifyYT(hl.link.ToString(), podcastName);
|
|
await ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {YTpodcastifyResult.Item2}", hl.id);
|
|
if (YTpodcastifyResult.Item1 == true)
|
|
{
|
|
Console.WriteLine($" {hl.title} -> podcastify (YT) success, removing labels");
|
|
await ttrssClient.SetArticleLabel(
|
|
labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id,
|
|
false,
|
|
hl.id);
|
|
if (nameLabel != null)
|
|
{
|
|
await ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
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);
|
|
await ttrssClient.UpdateArticleNote($"{noteString}[{DateTime.Now.ToLongTimeString()}] - {ATTpodcastifyResult.Item2}", hl.id);
|
|
if (ATTpodcastifyResult.Item1 == true)
|
|
{
|
|
Console.WriteLine($" {hl.title} -> podcastify (att) success, removing labels");
|
|
await ttrssClient.SetArticleLabel(
|
|
labelsWRTFeed.First(l => l.caption == action.triggerlabelCaption).id,
|
|
false,
|
|
hl.id);
|
|
if (nameLabel != null)
|
|
{
|
|
await ttrssClient.SetArticleLabel(nameLabel.id, false, hl.id);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($" {hl.title} -> podcastify (att) failed");
|
|
}
|
|
break;
|
|
default:
|
|
noteString += $"[{DateTime.Now.ToLongTimeString()}] - feed configured but action not recognized";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Console.WriteLine($"logging out");
|
|
await ttrssClient.Logout();
|
|
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));
|
|
}
|
|
if(File.Exists(moveTarget))
|
|
{
|
|
Console.WriteLine("file already exists, abandoning");
|
|
}
|
|
else
|
|
{
|
|
File.Move(f, moveTarget);
|
|
}
|
|
}
|
|
}
|
|
Console.WriteLine($"done for real");
|
|
}
|
|
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;
|
|
}
|
|
|
|
private static async Task<Tuple<bool, string>> standardDL(string articleLink)
|
|
{
|
|
Console.WriteLine($" standard downloading {articleLink}");
|
|
try
|
|
{
|
|
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].%(ext)s";
|
|
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}";
|
|
}
|
|
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>> podcastifyYT(string articleLink, string podcastName)
|
|
{
|
|
Console.WriteLine($" youtube-podcastifying {articleLink} ({podcastName})");
|
|
try
|
|
{
|
|
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);
|
|
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($" {(res.Success ? "Success" : "fail")} in {sw.Elapsed} - {res.Data}");
|
|
if (res.Success && !res.Data.EndsWith(".mp3"))
|
|
{
|
|
Console.WriteLine(" must convert audio");
|
|
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);
|
|
}
|
|
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}");
|
|
}
|
|
}
|
|
private static async Task<Tuple<bool, string>> SponsorCheck(ttrss.datastructures.Headline hl)
|
|
{
|
|
if(!hl.link.Host.EndsWith("youtube.com"))
|
|
{
|
|
return new Tuple<bool, string>(true, "sponsorblock, sadly, only exists for youtube");
|
|
}
|
|
var match = Regex.Match(hl.link.Query, "v=([^&]+)(&|$)");
|
|
var videoId = match.Groups?[1].Value;
|
|
var c = new HttpClient();
|
|
|
|
var sponsorblockcheck = await c.GetAsync($"https://sponsor.ajay.app/api/skipSegments?videoID={videoId}&category=sponsor&category=selfpromo&category=interaciton&category=intro&category=outro&category=preview");
|
|
if(sponsorblockcheck.StatusCode == System.Net.HttpStatusCode.NotFound)
|
|
{
|
|
Console.WriteLine($"sponsorblock reports that {videoId} has no entries (yet)");
|
|
var updateTimestamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
|
updateTimestamp = updateTimestamp.AddSeconds(hl.updated).ToLocalTime();
|
|
if(DateTime.Now - updateTimestamp > TimeSpan.FromMinutes(45))
|
|
{
|
|
return new Tuple<bool, string>(true, $"updated {updateTimestamp} (more than 45 minutes ago), going to give up waiting for sponsorblock");
|
|
}
|
|
else
|
|
{
|
|
return new Tuple<bool, string>(false, "none found, waiting");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
var segments = JsonConvert.DeserializeObject<IEnumerable<sponsorblock.Segment>>(await sponsorblockcheck.Content.ReadAsStringAsync());
|
|
if(segments.Count() > 1)
|
|
{
|
|
return new Tuple<bool, string>(true, $"{segments.Count()} segments");
|
|
}
|
|
else
|
|
{
|
|
return new Tuple<bool, string>(false, $"no segments");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |