2024-03-12 14:05:27 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using TwitchChatTTS.Helpers;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-12 14:05:27 -04:00
|
|
|
using TwitchChatTTS.Seven;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public class SevenApiClient
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
public const string API_URL = "https://7tv.io/v3";
|
|
|
|
public const string WEBSOCKET_URL = "wss://events.7tv.io/v3";
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly WebClientWrap _web;
|
|
|
|
private readonly ILogger _logger;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public SevenApiClient(ILogger logger)
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger = logger;
|
|
|
|
_web = new WebClientWrap(new JsonSerializerOptions()
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public async Task<EmoteSet?> FetchChannelEmoteSet(string twitchId)
|
|
|
|
{
|
2024-06-16 20:19:31 -04:00
|
|
|
try
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
var details = await _web.GetJson<UserDetails>($"{API_URL}/users/twitch/" + twitchId);
|
2024-06-16 20:19:31 -04:00
|
|
|
return details?.EmoteSet;
|
|
|
|
}
|
|
|
|
catch (JsonException e)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Error(e, "Failed to fetch channel emotes from 7tv due to improper JSON.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Error(e, "Failed to fetch channel emotes from 7tv.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IEnumerable<Emote>?> FetchGlobalSevenEmotes()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
var emoteSet = await _web.GetJson<EmoteSet>($"{API_URL}/emote-sets/6353512c802a0e34bac96dd2");
|
2024-06-16 20:19:31 -04:00
|
|
|
return emoteSet?.Emotes;
|
|
|
|
}
|
|
|
|
catch (JsonException e)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Error(e, "Failed to fetch global emotes from 7tv due to improper JSON.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Error(e, "Failed to fetch global emotes from 7tv.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|