sponsorblock check

This commit is contained in:
Adam R Grey 2023-04-05 22:02:37 -04:00
parent b04e870cd8
commit dbb342302e

View File

@ -1,6 +1,7 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Linq; using System.Linq;
using System.Diagnostics; using System.Diagnostics;
using System.Text.RegularExpressions;
namespace ttrss_co_client namespace ttrss_co_client
{ {
@ -29,6 +30,11 @@ namespace ttrss_co_client
labelsWRTFeed.Where(l => l.@checked).Select(l => l.caption).Contains(fa.triggerlabelCaption))?.ToList(); labelsWRTFeed.Where(l => l.@checked).Select(l => l.caption).Contains(fa.triggerlabelCaption))?.ToList();
if (actionsForFeed != null && actionsForFeed.Any()) if (actionsForFeed != null && actionsForFeed.Any())
{ {
if(!(await SponsorCheck(hl)))
{
await ttrssClient.UpdateArticleNote($"{hl.note}\n[{DateTime.Now.ToLongTimeString()}] waiting for sponsorblock", hl.id);
continue;
}
foreach (var action in actionsForFeed) foreach (var action in actionsForFeed)
{ {
Console.WriteLine($" {hl.title} -> action: {action.command}"); Console.WriteLine($" {hl.title} -> action: {action.command}");
@ -289,5 +295,38 @@ namespace ttrss_co_client
return new Tuple<bool, string>(false, $"{e.ToString()}: {e.Message}.\n{e.StackTrace}"); return new Tuple<bool, string>(false, $"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
} }
} }
public static async Task<bool> SponsorCheck(ttrss.datastructures.Headline hl)
{
if(!hl.link.Host.EndsWith("youtube.com"))
{
//sponsorblock, sadly, only exists for youtube
return true;
}
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))
{
Console.WriteLine($"updated {updateTimestamp} (more than 45 minutes ago), going to give up waiting for sponsorblock");
return true;
}
else
{
Console.WriteLine($"going to wait a bit for segments to show up.");
return false;
}
}
else
{
return true;
}
}
} }
} }