2024-03-12 14:05:27 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using TwitchChatTTS.Helpers;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using TwitchChatTTS.Seven;
|
|
|
|
|
|
|
|
public class SevenApiClient {
|
2024-03-15 08:27:35 -04:00
|
|
|
public static readonly string API_URL = "https://7tv.io/v3";
|
|
|
|
public static readonly string WEBSOCKET_URL = "wss://events.7tv.io/v3";
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
private WebClientWrap Web { get; }
|
|
|
|
private ILogger<SevenApiClient> Logger { get; }
|
|
|
|
private long? Id { get; }
|
|
|
|
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
public SevenApiClient(ILogger<SevenApiClient> logger, TwitchBotToken token) {
|
2024-03-12 14:05:27 -04:00
|
|
|
Logger = logger;
|
|
|
|
Id = long.TryParse(token?.BroadcasterId, out long id) ? id : -1;
|
|
|
|
|
|
|
|
Web = new WebClientWrap(new JsonSerializerOptions() {
|
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<EmoteDatabase?> GetSevenEmotes() {
|
2024-03-15 08:27:35 -04:00
|
|
|
if (Id == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
throw new NullReferenceException(nameof(Id));
|
|
|
|
|
|
|
|
try {
|
2024-03-15 08:27:35 -04:00
|
|
|
var details = await Web.GetJson<UserDetails>($"{API_URL}/users/twitch/" + Id);
|
|
|
|
if (details == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
return null;
|
|
|
|
|
|
|
|
var emotes = new EmoteDatabase();
|
2024-03-15 08:27:35 -04:00
|
|
|
if (details.EmoteSet != null)
|
2024-03-12 14:05:27 -04:00
|
|
|
foreach (var emote in details.EmoteSet.Emotes)
|
|
|
|
emotes.Add(emote.Name, emote.Id);
|
|
|
|
Logger.LogInformation($"Loaded {details.EmoteSet?.Emotes.Count() ?? 0} emotes from 7tv.");
|
|
|
|
return emotes;
|
|
|
|
} catch (JsonException e) {
|
|
|
|
Logger.LogError(e, "Failed to fetch emotes from 7tv. 2");
|
|
|
|
} catch (Exception e) {
|
|
|
|
Logger.LogError(e, "Failed to fetch emotes from 7tv.");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|