mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 01:38:27 -04:00
Fixed some crashes in response strings source generator, reorganized more submodules into their folders
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
#nullable disable
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public class PicartoChannelResponse
|
||||
{
|
||||
[JsonProperty("user_id")]
|
||||
public int UserId { get; set; }
|
||||
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonProperty("avatar")]
|
||||
public string Avatar { get; set; }
|
||||
|
||||
[JsonProperty("online")]
|
||||
public bool Online { get; set; }
|
||||
|
||||
[JsonProperty("viewers")]
|
||||
public int Viewers { get; set; }
|
||||
|
||||
[JsonProperty("viewers_total")]
|
||||
public int ViewersTotal { get; set; }
|
||||
|
||||
[JsonProperty("thumbnails")]
|
||||
public Thumbnails Thumbnails { get; set; }
|
||||
|
||||
[JsonProperty("followers")]
|
||||
public int Followers { get; set; }
|
||||
|
||||
[JsonProperty("subscribers")]
|
||||
public int Subscribers { get; set; }
|
||||
|
||||
[JsonProperty("adult")]
|
||||
public bool Adult { get; set; }
|
||||
|
||||
[JsonProperty("category")]
|
||||
public string Category { get; set; }
|
||||
|
||||
[JsonProperty("account_type")]
|
||||
public string AccountType { get; set; }
|
||||
|
||||
[JsonProperty("commissions")]
|
||||
public bool Commissions { get; set; }
|
||||
|
||||
[JsonProperty("recordings")]
|
||||
public bool Recordings { get; set; }
|
||||
|
||||
[JsonProperty("title")]
|
||||
public string Title { get; set; }
|
||||
|
||||
[JsonProperty("description_panels")]
|
||||
public List<DescriptionPanel> DescriptionPanels { get; set; }
|
||||
|
||||
[JsonProperty("private")]
|
||||
public bool Private { get; set; }
|
||||
|
||||
[JsonProperty("private_message")]
|
||||
public string PrivateMessage { get; set; }
|
||||
|
||||
[JsonProperty("gaming")]
|
||||
public bool Gaming { get; set; }
|
||||
|
||||
[JsonProperty("chat_settings")]
|
||||
public ChatSettings ChatSettings { get; set; }
|
||||
|
||||
[JsonProperty("last_live")]
|
||||
public DateTime LastLive { get; set; }
|
||||
|
||||
[JsonProperty("tags")]
|
||||
public List<string> Tags { get; set; }
|
||||
|
||||
[JsonProperty("multistream")]
|
||||
public List<Multistream> Multistream { get; set; }
|
||||
|
||||
[JsonProperty("languages")]
|
||||
public List<Language> Languages { get; set; }
|
||||
|
||||
[JsonProperty("following")]
|
||||
public bool Following { get; set; }
|
||||
}
|
||||
|
||||
public class Thumbnails
|
||||
{
|
||||
[JsonProperty("web")]
|
||||
public string Web { get; set; }
|
||||
|
||||
[JsonProperty("web_large")]
|
||||
public string WebLarge { get; set; }
|
||||
|
||||
[JsonProperty("mobile")]
|
||||
public string Mobile { get; set; }
|
||||
|
||||
[JsonProperty("tablet")]
|
||||
public string Tablet { get; set; }
|
||||
}
|
||||
|
||||
public class DescriptionPanel
|
||||
{
|
||||
[JsonProperty("title")]
|
||||
public string Title { get; set; }
|
||||
|
||||
[JsonProperty("body")]
|
||||
public string Body { get; set; }
|
||||
|
||||
[JsonProperty("image")]
|
||||
public string Image { get; set; }
|
||||
|
||||
[JsonProperty("image_link")]
|
||||
public string ImageLink { get; set; }
|
||||
|
||||
[JsonProperty("button_text")]
|
||||
public string ButtonText { get; set; }
|
||||
|
||||
[JsonProperty("button_link")]
|
||||
public string ButtonLink { get; set; }
|
||||
|
||||
[JsonProperty("position")]
|
||||
public int Position { get; set; }
|
||||
}
|
||||
|
||||
public class ChatSettings
|
||||
{
|
||||
[JsonProperty("guest_chat")]
|
||||
public bool GuestChat { get; set; }
|
||||
|
||||
[JsonProperty("links")]
|
||||
public bool Links { get; set; }
|
||||
|
||||
[JsonProperty("level")]
|
||||
public int Level { get; set; }
|
||||
}
|
||||
|
||||
public class Multistream
|
||||
{
|
||||
[JsonProperty("user_id")]
|
||||
public int UserId { get; set; }
|
||||
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonProperty("online")]
|
||||
public bool Online { get; set; }
|
||||
|
||||
[JsonProperty("adult")]
|
||||
public bool Adult { get; set; }
|
||||
}
|
||||
|
||||
public class Language
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Db.Models;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public class StreamData
|
||||
{
|
||||
public FollowedStream.FType StreamType { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string UniqueName { get; set; }
|
||||
public int Viewers { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Game { get; set; }
|
||||
public string Preview { get; set; }
|
||||
public bool IsLive { get; set; }
|
||||
public string StreamUrl { get; set; }
|
||||
public string AvatarUrl { get; set; }
|
||||
|
||||
public StreamDataKey CreateKey()
|
||||
=> new(StreamType, UniqueName.ToLower());
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
#nullable disable
|
||||
using NadekoBot.Db.Models;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public readonly struct StreamDataKey
|
||||
{
|
||||
public FollowedStream.FType Type { get; }
|
||||
public string Name { get; }
|
||||
|
||||
[JsonConstructor]
|
||||
public StreamDataKey(FollowedStream.FType type, string name)
|
||||
{
|
||||
Type = type;
|
||||
Name = name;
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
#nullable disable
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
//
|
||||
// public class TwitchResponse
|
||||
// {
|
||||
// public List<StreamApiData> Data { get; set; }
|
||||
//
|
||||
// public class StreamApiData
|
||||
// {
|
||||
// public string Id { get; set; }
|
||||
// public string UserId { get; set; }
|
||||
// public string UserName { get; set; }
|
||||
// public string GameId { get; set; }
|
||||
// public string Type { get; set; }
|
||||
// public string Title { get; set; }
|
||||
// public int ViewerCount { get; set; }
|
||||
// public string Language { get; set; }
|
||||
// public string ThumbnailUrl { get; set; }
|
||||
// public DateTime StartedAt { get; set; }
|
||||
// }
|
||||
// }
|
@@ -0,0 +1,114 @@
|
||||
#nullable disable
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public class TwitchResponseV5
|
||||
{
|
||||
public List<Stream> Streams { get; set; }
|
||||
|
||||
public class Channel
|
||||
{
|
||||
[JsonProperty("_id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[JsonProperty("broadcaster_language")]
|
||||
public string BroadcasterLanguage { get; set; }
|
||||
|
||||
[JsonProperty("created_at")]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
[JsonProperty("display_name")]
|
||||
public string DisplayName { get; set; }
|
||||
|
||||
[JsonProperty("followers")]
|
||||
public int Followers { get; set; }
|
||||
|
||||
[JsonProperty("game")]
|
||||
public string Game { get; set; }
|
||||
|
||||
[JsonProperty("language")]
|
||||
public string Language { get; set; }
|
||||
|
||||
[JsonProperty("logo")]
|
||||
public string Logo { get; set; }
|
||||
|
||||
[JsonProperty("mature")]
|
||||
public bool Mature { get; set; }
|
||||
|
||||
[JsonProperty("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonProperty("partner")]
|
||||
public bool Partner { get; set; }
|
||||
|
||||
[JsonProperty("profile_banner")]
|
||||
public string ProfileBanner { get; set; }
|
||||
|
||||
[JsonProperty("profile_banner_background_color")]
|
||||
public object ProfileBannerBackgroundColor { get; set; }
|
||||
|
||||
[JsonProperty("status")]
|
||||
public string Status { get; set; }
|
||||
|
||||
[JsonProperty("updated_at")]
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
|
||||
[JsonProperty("url")]
|
||||
public string Url { get; set; }
|
||||
|
||||
[JsonProperty("video_banner")]
|
||||
public string VideoBanner { get; set; }
|
||||
|
||||
[JsonProperty("views")]
|
||||
public int Views { get; set; }
|
||||
}
|
||||
|
||||
public class Preview
|
||||
{
|
||||
[JsonProperty("large")]
|
||||
public string Large { get; set; }
|
||||
|
||||
[JsonProperty("medium")]
|
||||
public string Medium { get; set; }
|
||||
|
||||
[JsonProperty("small")]
|
||||
public string Small { get; set; }
|
||||
|
||||
[JsonProperty("template")]
|
||||
public string Template { get; set; }
|
||||
}
|
||||
|
||||
public class Stream
|
||||
{
|
||||
[JsonProperty("_id")]
|
||||
public long Id { get; set; }
|
||||
|
||||
[JsonProperty("average_fps")]
|
||||
public double AverageFps { get; set; }
|
||||
|
||||
[JsonProperty("channel")]
|
||||
public Channel Channel { get; set; }
|
||||
|
||||
[JsonProperty("created_at")]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
[JsonProperty("delay")]
|
||||
public double Delay { get; set; }
|
||||
|
||||
[JsonProperty("game")]
|
||||
public string Game { get; set; }
|
||||
|
||||
[JsonProperty("is_playlist")]
|
||||
public bool IsPlaylist { get; set; }
|
||||
|
||||
[JsonProperty("preview")]
|
||||
public Preview Preview { get; set; }
|
||||
|
||||
[JsonProperty("video_height")]
|
||||
public int VideoHeight { get; set; }
|
||||
|
||||
[JsonProperty("viewers")]
|
||||
public int Viewers { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
#nullable disable
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common;
|
||||
|
||||
public class TwitchUsersResponseV5
|
||||
{
|
||||
[JsonProperty("users")]
|
||||
public List<User> Users { get; set; }
|
||||
|
||||
public class User
|
||||
{
|
||||
[JsonProperty("_id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
// [JsonProperty("bio")]
|
||||
// public string Bio { get; set; }
|
||||
//
|
||||
// [JsonProperty("created_at")]
|
||||
// public DateTime CreatedAt { get; set; }
|
||||
//
|
||||
// [JsonProperty("display_name")]
|
||||
// public string DisplayName { get; set; }
|
||||
//
|
||||
// [JsonProperty("logo")]
|
||||
// public string Logo { get; set; }
|
||||
//
|
||||
// [JsonProperty("name")]
|
||||
// public string Name { get; set; }
|
||||
//
|
||||
// [JsonProperty("type")]
|
||||
// public string Type { get; set; }
|
||||
//
|
||||
// [JsonProperty("updated_at")]
|
||||
// public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,221 @@
|
||||
using NadekoBot.Db.Models;
|
||||
using NadekoBot.Modules.Searches.Common.StreamNotifications.Providers;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common.StreamNotifications;
|
||||
|
||||
public class NotifChecker
|
||||
{
|
||||
public event Func<List<StreamData>, Task> OnStreamsOffline = _ => Task.CompletedTask;
|
||||
public event Func<List<StreamData>, Task> OnStreamsOnline = _ => Task.CompletedTask;
|
||||
private readonly ConnectionMultiplexer _multi;
|
||||
private readonly string _key;
|
||||
|
||||
private readonly Dictionary<FollowedStream.FType, Provider> _streamProviders;
|
||||
private readonly HashSet<(FollowedStream.FType, string)> _offlineBuffer;
|
||||
|
||||
public NotifChecker(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ConnectionMultiplexer multi,
|
||||
string uniqueCacheKey,
|
||||
bool isMaster)
|
||||
{
|
||||
_multi = multi;
|
||||
_key = $"{uniqueCacheKey}_followed_streams_data";
|
||||
_streamProviders = new()
|
||||
{
|
||||
{ FollowedStream.FType.Twitch, new TwitchProvider(httpClientFactory) },
|
||||
{ FollowedStream.FType.Picarto, new PicartoProvider(httpClientFactory) }
|
||||
};
|
||||
_offlineBuffer = new();
|
||||
if (isMaster) CacheClearAllData();
|
||||
}
|
||||
|
||||
// gets all streams which have been failing for more than the provided timespan
|
||||
public IEnumerable<StreamDataKey> GetFailingStreams(TimeSpan duration, bool remove = false)
|
||||
{
|
||||
var toReturn = _streamProviders.SelectMany(prov => prov.Value
|
||||
.FailingStreams
|
||||
.Where(fs => DateTime.UtcNow - fs.ErroringSince
|
||||
> duration)
|
||||
.Select(fs => new StreamDataKey(prov.Value.Platform,
|
||||
fs.Item1)))
|
||||
.ToList();
|
||||
|
||||
if (remove)
|
||||
foreach (var toBeRemoved in toReturn)
|
||||
_streamProviders[toBeRemoved.Type].ClearErrorsFor(toBeRemoved.Name);
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public Task RunAsync()
|
||||
=> Task.Run(async () =>
|
||||
{
|
||||
while (true)
|
||||
try
|
||||
{
|
||||
var allStreamData = CacheGetAllData();
|
||||
|
||||
var oldStreamDataDict = allStreamData
|
||||
// group by type
|
||||
.GroupBy(entry => entry.Key.Type)
|
||||
.ToDictionary(entry => entry.Key,
|
||||
entry => entry.AsEnumerable()
|
||||
.ToDictionary(x => x.Key.Name, x => x.Value));
|
||||
|
||||
var newStreamData = await oldStreamDataDict.Select(x =>
|
||||
{
|
||||
// get all stream data for the streams of this type
|
||||
if (_streamProviders.TryGetValue(x.Key,
|
||||
out var provider))
|
||||
return 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(new List<StreamData>());
|
||||
})
|
||||
.WhenAll();
|
||||
|
||||
var newlyOnline = new List<StreamData>();
|
||||
var newlyOffline = new List<StreamData>();
|
||||
// go through all new stream data, compare them with the old ones
|
||||
foreach (var newData in newStreamData.SelectMany(x => x))
|
||||
{
|
||||
// update cached data
|
||||
var key = newData.CreateKey();
|
||||
CacheAddData(key, newData, true);
|
||||
|
||||
// compare old data with new data
|
||||
var oldData = oldStreamDataDict[key.Type][key.Name];
|
||||
|
||||
// this is the first pass
|
||||
if (oldData is null)
|
||||
continue;
|
||||
|
||||
// if the stream is offline, we need to check if it was
|
||||
// marked as offline once previously
|
||||
// if it was, that means this is second time we're getting offline
|
||||
// status for that stream -> notify subscribers
|
||||
// Note: This is done because twitch api will sometimes return an offline status
|
||||
// shortly after the stream is already online, which causes duplicate notifications.
|
||||
// (stream is online -> stream is offline -> stream is online again (and stays online))
|
||||
// This offlineBuffer will make it so that the stream has to be marked as offline TWICE
|
||||
// before it sends an offline notification to the subscribers.
|
||||
var streamId = (key.Type, key.Name);
|
||||
if (!newData.IsLive && _offlineBuffer.Remove(streamId))
|
||||
{
|
||||
newlyOffline.Add(newData);
|
||||
}
|
||||
else if (newData.IsLive != oldData.IsLive)
|
||||
{
|
||||
if (newData.IsLive)
|
||||
{
|
||||
_offlineBuffer.Remove(streamId);
|
||||
newlyOnline.Add(newData);
|
||||
}
|
||||
else
|
||||
{
|
||||
_offlineBuffer.Add(streamId);
|
||||
// newlyOffline.Add(newData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var tasks = new List<Task> { Task.Delay(30_000) };
|
||||
|
||||
if (newlyOnline.Count > 0) tasks.Add(OnStreamsOnline(newlyOnline));
|
||||
|
||||
if (newlyOffline.Count > 0) tasks.Add(OnStreamsOffline(newlyOffline));
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Error getting stream notifications: {ErrorMessage}", ex.Message);
|
||||
}
|
||||
});
|
||||
|
||||
public bool CacheAddData(StreamDataKey key, StreamData? data, bool replace)
|
||||
{
|
||||
var db = _multi.GetDatabase();
|
||||
return db.HashSet(_key,
|
||||
JsonConvert.SerializeObject(key),
|
||||
JsonConvert.SerializeObject(data),
|
||||
replace ? When.Always : When.NotExists);
|
||||
}
|
||||
|
||||
public void CacheDeleteData(StreamDataKey key)
|
||||
{
|
||||
var db = _multi.GetDatabase();
|
||||
db.HashDelete(_key, JsonConvert.SerializeObject(key));
|
||||
}
|
||||
|
||||
public void CacheClearAllData()
|
||||
{
|
||||
var db = _multi.GetDatabase();
|
||||
db.KeyDelete(_key);
|
||||
}
|
||||
|
||||
public Dictionary<StreamDataKey, StreamData?> CacheGetAllData()
|
||||
{
|
||||
var db = _multi.GetDatabase();
|
||||
if (!db.KeyExists(_key)) return new();
|
||||
|
||||
return db.HashGetAll(_key)
|
||||
.ToDictionary(entry => JsonConvert.DeserializeObject<StreamDataKey>(entry.Name),
|
||||
entry => entry.Value.IsNullOrEmpty
|
||||
? default
|
||||
: JsonConvert.DeserializeObject<StreamData>(entry.Value));
|
||||
}
|
||||
|
||||
public async Task<StreamData?> GetStreamDataByUrlAsync(string url)
|
||||
{
|
||||
// loop through all providers and see which regex matches
|
||||
foreach (var (_, provider) in _streamProviders)
|
||||
{
|
||||
var isValid = await provider.IsValidUrl(url);
|
||||
if (!isValid)
|
||||
continue;
|
||||
// if it's not a valid url, try another provider
|
||||
var data = await provider.GetStreamDataByUrlAsync(url);
|
||||
return data;
|
||||
}
|
||||
|
||||
// if no provider found, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return currently available stream data, get new one if none available, and start tracking the stream.
|
||||
/// </summary>
|
||||
/// <param name="url">Url of the stream</param>
|
||||
/// <returns>Stream data, if any</returns>
|
||||
public async Task<StreamData?> TrackStreamByUrlAsync(string url)
|
||||
{
|
||||
var data = await GetStreamDataByUrlAsync(url);
|
||||
EnsureTracked(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make sure a stream is tracked using its stream data.
|
||||
/// </summary>
|
||||
/// <param name="data">Data to try to track if not already tracked</param>
|
||||
/// <returns>Whether it's newly added</returns>
|
||||
private bool EnsureTracked(StreamData? data)
|
||||
{
|
||||
// something failed, don't add anything to cache
|
||||
if (data is null)
|
||||
return false;
|
||||
|
||||
// if stream is found, add it to the cache for tracking only if it doesn't already exist
|
||||
// because stream will be checked and events will fire in a loop. We don't want to override old state
|
||||
return CacheAddData(data.CreateKey(), data, false);
|
||||
}
|
||||
|
||||
public void UntrackStreamByKey(in StreamDataKey key)
|
||||
=> CacheDeleteData(key);
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
using NadekoBot.Db.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common.StreamNotifications.Providers;
|
||||
|
||||
public class PicartoProvider : Provider
|
||||
{
|
||||
private static Regex Regex { get; } = new(@"picarto.tv/(?<name>.+[^/])/?",
|
||||
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public override FollowedStream.FType Platform
|
||||
=> FollowedStream.FType.Picarto;
|
||||
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
|
||||
public PicartoProvider(IHttpClientFactory httpClientFactory)
|
||||
=> _httpClientFactory = httpClientFactory;
|
||||
|
||||
public override Task<bool> IsValidUrl(string url)
|
||||
{
|
||||
var match = Regex.Match(url);
|
||||
if (!match.Success)
|
||||
return Task.FromResult(false);
|
||||
|
||||
// var username = match.Groups["name"].Value;
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
public override Task<StreamData?> GetStreamDataByUrlAsync(string url)
|
||||
{
|
||||
var match = Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
var name = match.Groups["name"].Value;
|
||||
return GetStreamDataAsync(name);
|
||||
}
|
||||
|
||||
return Task.FromResult<StreamData?>(null);
|
||||
}
|
||||
|
||||
public override async Task<StreamData?> GetStreamDataAsync(string id)
|
||||
{
|
||||
var data = await GetStreamDataAsync(new List<string> { id });
|
||||
|
||||
return data.FirstOrDefault();
|
||||
}
|
||||
|
||||
public override async Task<List<StreamData>> GetStreamDataAsync(List<string> logins)
|
||||
{
|
||||
if (logins.Count == 0)
|
||||
return new();
|
||||
|
||||
using var http = _httpClientFactory.CreateClient();
|
||||
var toReturn = new List<StreamData>();
|
||||
foreach (var login in logins)
|
||||
try
|
||||
{
|
||||
http.DefaultRequestHeaders.Accept.Add(new("application/json"));
|
||||
// get id based on the username
|
||||
var res = await http.GetAsync($"https://api.picarto.tv/v1/channel/name/{login}");
|
||||
|
||||
if (!res.IsSuccessStatusCode)
|
||||
continue;
|
||||
|
||||
var userData =
|
||||
JsonConvert.DeserializeObject<PicartoChannelResponse>(await res.Content.ReadAsStringAsync())!;
|
||||
|
||||
toReturn.Add(ToStreamData(userData));
|
||||
_failingStreams.TryRemove(login, out _);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning("Something went wrong retreiving {StreamPlatform} stream data for {Login}: {ErrorMessage}",
|
||||
Platform,
|
||||
login,
|
||||
ex.Message);
|
||||
_failingStreams.TryAdd(login, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private StreamData ToStreamData(PicartoChannelResponse stream)
|
||||
=> new()
|
||||
{
|
||||
StreamType = FollowedStream.FType.Picarto,
|
||||
Name = stream.Name,
|
||||
UniqueName = stream.Name,
|
||||
Viewers = stream.Viewers,
|
||||
Title = stream.Title,
|
||||
IsLive = stream.Online,
|
||||
Preview = stream.Thumbnails.Web,
|
||||
Game = stream.Category,
|
||||
StreamUrl = $"https://picarto.tv/{stream.Name}",
|
||||
AvatarUrl = stream.Avatar
|
||||
};
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
using NadekoBot.Db.Models;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common.StreamNotifications.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract class implemented by providers of all supported platforms
|
||||
/// </summary>
|
||||
public abstract class Provider
|
||||
{
|
||||
/// <summary>
|
||||
/// Type of the platform.
|
||||
/// </summary>
|
||||
public abstract FollowedStream.FType Platform { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the stream usernames which fail to execute due to an error, and when they started throwing errors.
|
||||
/// This can happen if stream name is invalid, or if the stream doesn't exist anymore.
|
||||
/// </summary>
|
||||
public IEnumerable<(string Login, DateTime ErroringSince)> FailingStreams
|
||||
=> _failingStreams.Select(entry => (entry.Key, entry.Value)).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// When was the first time the stream continually had errors while being retrieved
|
||||
/// </summary>
|
||||
protected readonly ConcurrentDictionary<string, DateTime> _failingStreams = new();
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the specified url is a valid stream url for this platform.
|
||||
/// </summary>
|
||||
/// <param name="url">Url to check</param>
|
||||
/// <returns>True if valid, otherwise false</returns>
|
||||
public abstract Task<bool> IsValidUrl(string url);
|
||||
|
||||
/// <summary>
|
||||
/// Gets stream data of the stream on the specified url on this <see cref="Platform" />
|
||||
/// </summary>
|
||||
/// <param name="url">Url of the stream</param>
|
||||
/// <returns><see cref="StreamData" /> of the specified stream. Null if none found</returns>
|
||||
public abstract Task<StreamData?> GetStreamDataByUrlAsync(string url);
|
||||
|
||||
/// <summary>
|
||||
/// Gets stream data of the specified id/username on this <see cref="Platform" />
|
||||
/// </summary>
|
||||
/// <param name="id">Name (or id where applicable) of the user on the platform</param>
|
||||
/// <returns><see cref="StreamData" /> of the user. Null if none found</returns>
|
||||
public abstract Task<StreamData?> GetStreamDataAsync(string id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets stream data of all specified ids/usernames on this <see cref="Platform" />
|
||||
/// </summary>
|
||||
/// <param name="usernames">List of ids/usernames</param>
|
||||
/// <returns><see cref="StreamData" /> of all users, in the same order. Null for every id/user not found.</returns>
|
||||
public abstract Task<List<StreamData>> GetStreamDataAsync(List<string> usernames);
|
||||
|
||||
public void ClearErrorsFor(string login)
|
||||
=> _failingStreams.TryRemove(login, out _);
|
||||
}
|
@@ -0,0 +1,181 @@
|
||||
#nullable disable
|
||||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using System.Net.Http;
|
||||
// using System.Text.Json;
|
||||
// using System.Text.Json.Serialization;
|
||||
// using System.Text.RegularExpressions;
|
||||
// using System.Threading.Tasks;
|
||||
// using NadekoBot.Services.Database.Models;
|
||||
// using NadekoBot.Extensions;
|
||||
// using Serilog;
|
||||
// using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
//
|
||||
// namespace NadekoBot.Modules.Searches.Common.StreamNotifications.Providers
|
||||
// {
|
||||
// public sealed class TwitchHelixProvider : Provider
|
||||
// {
|
||||
// private readonly IHttpClientFactory _httpClientFactory;
|
||||
//
|
||||
// //
|
||||
// private static Regex Regex { get; } = new Regex(@"twitch.tv/(?<name>.+[^/])/?",
|
||||
// RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
//
|
||||
// public override FollowedStream.FType Platform => FollowedStream.FType.Twitch;
|
||||
//
|
||||
// private (string Token, DateTime Expiry) _token = default;
|
||||
//
|
||||
// public TwitchHelixProvider(IHttpClientFactory httpClientFactory)
|
||||
// {
|
||||
// _httpClientFactory = httpClientFactory;
|
||||
// }
|
||||
//
|
||||
// private async Task EnsureTokenValidAsync()
|
||||
// {
|
||||
// if (_token != default && (DateTime.UtcNow - _token.Expiry) > TimeSpan.FromHours(1))
|
||||
// return;
|
||||
//
|
||||
// const string clientId = string.Empty;
|
||||
// const string clientSecret = string.Empty;
|
||||
//
|
||||
// var client = _httpClientFactory.CreateClient();
|
||||
// var res = await client.PostAsync("https://id.twitch.tv/oauth2/token" +
|
||||
// $"?client_id={clientId}" +
|
||||
// $"&client_secret={clientSecret}" +
|
||||
// "&grant_type=client_credentials", new StringContent(""));
|
||||
//
|
||||
// var data = JsonDocument.Parse(await res.Content.ReadAsStringAsync()).RootElement;
|
||||
//
|
||||
// _token = (data.GetProperty("access_token").GetString(),
|
||||
// DateTime.UtcNow + TimeSpan.FromSeconds(data.GetProperty("expires_in").GetInt32()));
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public override Task<bool> IsValidUrl(string url)
|
||||
// {
|
||||
// var match = Regex.Match(url);
|
||||
// if (!match.Success)
|
||||
// return Task.FromResult(false);
|
||||
//
|
||||
// var username = match.Groups["name"].Value;
|
||||
// return Task.FromResult(true);
|
||||
// }
|
||||
//
|
||||
// public override Task<StreamData?> GetStreamDataByUrlAsync(string url)
|
||||
// {
|
||||
// var match = Regex.Match(url);
|
||||
// if (match.Success)
|
||||
// {
|
||||
// var name = match.Groups["name"].Value;
|
||||
// return GetStreamDataAsync(name);
|
||||
// }
|
||||
//
|
||||
// return Task.FromResult<StreamData?>(null);
|
||||
// }
|
||||
//
|
||||
// public override async Task<StreamData?> GetStreamDataAsync(string id)
|
||||
// {
|
||||
// var data = await GetStreamDataAsync(new List<string> {id});
|
||||
//
|
||||
// return data.FirstOrDefault();
|
||||
// }
|
||||
//
|
||||
// public override async Task<List<StreamData>> GetStreamDataAsync(List<string> logins)
|
||||
// {
|
||||
// if (logins.Count == 0)
|
||||
// return new List<StreamData>();
|
||||
//
|
||||
// await EnsureTokenValidAsync();
|
||||
//
|
||||
// using var http = _httpClientFactory.CreateClient();
|
||||
// http.DefaultRequestHeaders.Clear();
|
||||
// http.DefaultRequestHeaders.Add("client-id","67w6z9i09xv2uoojdm9l0wsyph4hxo6");
|
||||
// http.DefaultRequestHeaders.Add("Authorization",$"Bearer {_token.Token}");
|
||||
//
|
||||
// var res = new TwitchResponse()
|
||||
// {
|
||||
// Data = new List<TwitchResponse.StreamApiData>()
|
||||
// };
|
||||
// foreach (var chunk in logins.Chunk(500))
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// var str = await http.GetStringAsync($"https://api.twitch.tv/helix/streams" +
|
||||
// $"?user_login={chunk.JoinWith(',')}" +
|
||||
// $"&first=100");
|
||||
//
|
||||
// res = JsonSerializer.Deserialize<TwitchResponse>(str);
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Log.Warning(ex, "Something went wrong retreiving {StreamPlatform} streams", Platform);
|
||||
// return new List<StreamData>();
|
||||
// }
|
||||
//
|
||||
// if (res.Data.Count == 0)
|
||||
// {
|
||||
// return new List<StreamData>();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return res.Data.Select(ToStreamData).ToList();
|
||||
// }
|
||||
//
|
||||
// private StreamData ToStreamData(TwitchResponse.StreamApiData apiData)
|
||||
// {
|
||||
// return new StreamData()
|
||||
// {
|
||||
// StreamType = FollowedStream.FType.Twitch,
|
||||
// Name = apiData.UserName,
|
||||
// UniqueName = apiData.UserId,
|
||||
// Viewers = apiData.ViewerCount,
|
||||
// Title = apiData.Title,
|
||||
// IsLive = apiData.Type == "live",
|
||||
// Preview = apiData.ThumbnailUrl
|
||||
// ?.Replace("{width}", "640")
|
||||
// ?.Replace("{height}", "480"),
|
||||
// Game = apiData.GameId,
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public class TwitchResponse
|
||||
// {
|
||||
// [JsonPropertyName("data")]
|
||||
// public List<StreamApiData> Data { get; set; }
|
||||
//
|
||||
// public class StreamApiData
|
||||
// {
|
||||
// [JsonPropertyName("id")]
|
||||
// public string Id { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("user_id")]
|
||||
// public string UserId { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("user_name")]
|
||||
// public string UserName { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("game_id")]
|
||||
// public string GameId { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("type")]
|
||||
// public string Type { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("title")]
|
||||
// public string Title { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("viewer_count")]
|
||||
// public int ViewerCount { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("language")]
|
||||
// public string Language { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("thumbnail_url")]
|
||||
// public string ThumbnailUrl { get; set; }
|
||||
//
|
||||
// [JsonPropertyName("started_at")]
|
||||
// public DateTime StartedAt { get; set; }
|
||||
// }
|
||||
// }
|
||||
// }
|
@@ -0,0 +1,125 @@
|
||||
using NadekoBot.Db.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace NadekoBot.Modules.Searches.Common.StreamNotifications.Providers;
|
||||
|
||||
public class TwitchProvider : Provider
|
||||
{
|
||||
private static Regex Regex { get; } = new(@"twitch.tv/(?<name>.+[^/])/?",
|
||||
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public override FollowedStream.FType Platform
|
||||
=> FollowedStream.FType.Twitch;
|
||||
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
|
||||
public TwitchProvider(IHttpClientFactory httpClientFactory)
|
||||
=> _httpClientFactory = httpClientFactory;
|
||||
|
||||
public override Task<bool> IsValidUrl(string url)
|
||||
{
|
||||
var match = Regex.Match(url);
|
||||
if (!match.Success)
|
||||
return Task.FromResult(false);
|
||||
|
||||
// var username = match.Groups["name"].Value;
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
public override Task<StreamData?> GetStreamDataByUrlAsync(string url)
|
||||
{
|
||||
var match = Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
var name = match.Groups["name"].Value;
|
||||
return GetStreamDataAsync(name);
|
||||
}
|
||||
|
||||
return Task.FromResult<StreamData?>(null);
|
||||
}
|
||||
|
||||
public override async Task<StreamData?> GetStreamDataAsync(string id)
|
||||
{
|
||||
var data = await GetStreamDataAsync(new List<string> { id });
|
||||
|
||||
return data.FirstOrDefault();
|
||||
}
|
||||
|
||||
public override async Task<List<StreamData>> GetStreamDataAsync(List<string> logins)
|
||||
{
|
||||
if (logins.Count == 0)
|
||||
return new();
|
||||
|
||||
using var http = _httpClientFactory.CreateClient();
|
||||
http.DefaultRequestHeaders.Add("Client-Id", "67w6z9i09xv2uoojdm9l0wsyph4hxo6");
|
||||
http.DefaultRequestHeaders.Add("Accept", "application/vnd.twitchtv.v5+json");
|
||||
|
||||
var toReturn = new List<StreamData>();
|
||||
foreach (var login in logins)
|
||||
try
|
||||
{
|
||||
// get id based on the username
|
||||
var idsStr = await http.GetStringAsync($"https://api.twitch.tv/kraken/users?login={login}");
|
||||
var userData = JsonConvert.DeserializeObject<TwitchUsersResponseV5>(idsStr);
|
||||
var user = userData?.Users.FirstOrDefault();
|
||||
|
||||
// if user can't be found, skip, it means there is no such user
|
||||
if (user is null)
|
||||
continue;
|
||||
|
||||
// get stream data
|
||||
var str = await http.GetStringAsync($"https://api.twitch.tv/kraken/streams/{user.Id}");
|
||||
var resObj = JsonConvert.DeserializeAnonymousType(str, new { Stream = new TwitchResponseV5.Stream() });
|
||||
|
||||
// if stream is null, user is not streaming
|
||||
if (resObj?.Stream is null)
|
||||
{
|
||||
// if user is not streaming, get his offline banner
|
||||
var chStr = await http.GetStringAsync($"https://api.twitch.tv/kraken/channels/{user.Id}");
|
||||
var ch = JsonConvert.DeserializeObject<TwitchResponseV5.Channel>(chStr)!;
|
||||
|
||||
toReturn.Add(new()
|
||||
{
|
||||
StreamType = FollowedStream.FType.Twitch,
|
||||
Name = ch.DisplayName,
|
||||
UniqueName = ch.Name,
|
||||
Title = ch.Status,
|
||||
IsLive = false,
|
||||
AvatarUrl = ch.Logo,
|
||||
StreamUrl = $"https://twitch.tv/{ch.Name}",
|
||||
Preview = ch.VideoBanner // set video banner as the preview,
|
||||
});
|
||||
continue; // move on
|
||||
}
|
||||
|
||||
toReturn.Add(ToStreamData(resObj.Stream));
|
||||
_failingStreams.TryRemove(login, out _);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning("Something went wrong retreiving {StreamPlatform} stream data for {Login}: {ErrorMessage}",
|
||||
Platform,
|
||||
login,
|
||||
ex.Message);
|
||||
_failingStreams.TryAdd(login, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private StreamData ToStreamData(TwitchResponseV5.Stream stream)
|
||||
=> new()
|
||||
{
|
||||
StreamType = FollowedStream.FType.Twitch,
|
||||
Name = stream.Channel.DisplayName,
|
||||
UniqueName = stream.Channel.Name,
|
||||
Viewers = stream.Viewers,
|
||||
Title = stream.Channel.Status,
|
||||
IsLive = true,
|
||||
Preview = stream.Preview.Large,
|
||||
Game = stream.Channel.Game,
|
||||
StreamUrl = $"https://twitch.tv/{stream.Channel.Name}",
|
||||
AvatarUrl = stream.Channel.Logo
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user