fixed youtube stream notifications incase invalid channel was provided

This commit is contained in:
Toastie 2025-03-20 22:44:39 +13:00
parent 4a5bcd46e6
commit 9b8d00d184
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
2 changed files with 59 additions and 56 deletions
src/EllieBot/Modules/Searches/_common/StreamNotifications

View file

@ -63,20 +63,19 @@ public class NotifChecker
.ToDictionary(x => x.Key.Name, x => x.Value));
var newStreamData = await oldStreamDataDict
.Select(x =>
.Select(async x =>
{
// get all stream data for the streams of this type
if (_streamProviders.TryGetValue(x.Key,
out var provider))
{
return provider.GetStreamDataAsync(x.Value
return await provider.GetStreamDataAsync(x.Value
.Select(entry => entry.Key)
.ToList());
}
// this means there's no provider for this stream data, (and there was before?)
return Task.FromResult<IReadOnlyCollection<StreamData>>(
new List<StreamData>());
return [];
})
.WhenAll();

View file

@ -1,12 +1,7 @@
using System.Net;
using EllieBot.Db.Models;
using EllieBot.Services;
using EllieBot.Db.Models;
using System.Text.RegularExpressions;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Xml.Linq;
using AngleSharp.Browser;
namespace EllieBot.Modules.Searches.Common.StreamNotifications.Providers;
@ -113,56 +108,65 @@ public sealed partial class YouTubeProvider : Provider
/// <returns><see cref="StreamData"/> of the channel. Null if none found</returns>
public override async Task<StreamData?> GetStreamDataAsync(string channelId)
{
var instances = _scs.Data.InvidiousInstances;
if (instances is not { Count: > 0 })
return null;
var invInstance = instances[_rng.Next(0, instances.Count)];
var client = _httpFactory.CreateClient();
client.BaseAddress = new Uri(invInstance);
var channel = await client.GetFromJsonAsync<InvidiousChannelResponse>($"/api/v1/channels/{channelId}");
if (channel is null)
return null;
var response =
await client.GetFromJsonAsync<InvChannelStreamsResponse>($"/api/v1/channels/{channelId}/streams");
if (response is null)
return null;
var vid = response.Videos.FirstOrDefault(x => !x.IsUpcoming && x.LengthSeconds == 0);
var isLive = false;
if (vid is null)
try
{
vid = response.Videos.FirstOrDefault(x => !x.IsUpcoming);
var instances = _scs.Data.InvidiousInstances;
if (instances is not { Count: > 0 })
return null;
var invInstance = instances[_rng.Next(0, instances.Count)];
var client = _httpFactory.CreateClient();
client.BaseAddress = new Uri(invInstance);
var channel = await client.GetFromJsonAsync<InvidiousChannelResponse>($"/api/v1/channels/{channelId}");
if (channel is null)
return null;
var response =
await client.GetFromJsonAsync<InvChannelStreamsResponse>($"/api/v1/channels/{channelId}/streams");
if (response is null)
return null;
var vid = response.Videos.FirstOrDefault(x => !x.IsUpcoming && x.LengthSeconds == 0);
var isLive = false;
if (vid is null)
{
vid = response.Videos.FirstOrDefault(x => !x.IsUpcoming);
}
else
{
isLive = true;
}
if (vid is null)
return null;
var avatarUrl = channel?.AuthorThumbnails?.Select(x => x.Url).LastOrDefault();
return new StreamData()
{
Game = "Livestream",
Name = vid.Author,
Preview = vid.Thumbnails
.Skip(1)
.Select(x => "https://i.ytimg.com/" + x.Url)
.FirstOrDefault(),
Title = vid.Title,
Viewers = vid.ViewCount,
AvatarUrl = avatarUrl,
IsLive = isLive,
StreamType = FollowedStream.FType.Youtube,
StreamUrl = "https://youtube.com/watch?v=" + vid.VideoId,
UniqueName = vid.AuthorId,
};
}
else
catch (Exception ex)
{
isLive = true;
}
if (vid is null)
Log.Warning(ex, "Unable to get stream data for a youtube channel {ChannelId}", channelId);
_failingStreams.TryAdd(channelId, DateTime.UtcNow);
return null;
var avatarUrl = channel?.AuthorThumbnails?.Select(x => x.Url).LastOrDefault();
return new StreamData()
{
Game = "Livestream",
Name = vid.Author,
Preview = vid.Thumbnails
.Skip(1)
.Select(x => "https://i.ytimg.com/" + x.Url)
.FirstOrDefault(),
Title = vid.Title,
Viewers = vid.ViewCount,
AvatarUrl = avatarUrl,
IsLive = isLive,
StreamType = FollowedStream.FType.Youtube,
StreamUrl = "https://youtube.com/watch?v=" + vid.VideoId,
UniqueName = vid.AuthorId,
};
}
}
/// <summary>