Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate 7tv API to v3 #14

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions 7TVEmoteProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using NeoSmart.Unicode;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
Expand All @@ -16,42 +14,71 @@ public class SevenTVEmoteProvider : IEmoteProvider

class SevenTVEmoticon
{
internal class SevenTVEmoticonData
{
internal class SevenTVEmoticonHost
{
internal class SevenTVEmoticonFile
{
public int width { get; set; }
public int height { get; set; }
}

public List<SevenTVEmoticonFile> files { get; set; }
}

public SevenTVEmoticonHost host { get; set; }
}

public string name { get; set; }
public int visibility { get; set; }
public int[] width { get; set; }
public int[] height { get; set; }

public SevenTVEmoticon() { }
public int flags { get; set; }
public SevenTVEmoticonData data { get; set; }
}

private class SevenTVGlobalEmotes
{
public List<SevenTVEmoticon> emotes { get; set; }
}

private class SevenTVUserEmotes
{
internal class SevenTVEmoteSet
{
public List<SevenTVEmoticon> emotes { get; set; }
}

public SevenTVEmoteSet emote_set { get; set; }
}

public bool TryGetEmote(string Name, out SizeF Size) => EmoteCache.TryGetValue(Name, out Size);

public async Task Initialize(string ChannelID, CancellationToken Token)
{
var rawJson = await Client.GetAsync("https://api.7tv.app/v2/emotes/global", Token);
var json = JsonSerializer.Deserialize<List<SevenTVEmoticon>>(await rawJson.Content.ReadAsStringAsync());
var rawJson = await Client.GetAsync("https://7tv.io/v3/emote-sets/global", Token);
var emotes = (await JsonSerializer.DeserializeAsync<SevenTVGlobalEmotes>(await rawJson.Content.ReadAsStreamAsync(), cancellationToken: Token)).emotes;

void addToList(SevenTVEmoticon emote)
{
if (EmoteCache.ContainsKey(emote.name))
return;

const int zw = 1 << 7;
if ((emote.visibility & zw) == zw) {
EmoteCache.Add(emote.name, new SizeF(-emote.width[0], -emote.height[0]));
const int zeroWidth = 1 << 0;
var imageSize = new SizeF(emote.data.host.files[0].width, emote.data.host.files[0].height);
if ((emote.flags & zeroWidth) == zeroWidth) {
EmoteCache.Add(emote.name, imageSize * -1);
} else {
EmoteCache.Add(emote.name, new SizeF(emote.width[0], emote.height[0]));
EmoteCache.Add(emote.name, imageSize);
}
}

foreach (var emote in json) {
foreach (var emote in emotes) {
addToList(emote);
}

rawJson = await Client.GetAsync("https://api.7tv.app/v2/users/" + ChannelID + "/emotes", Token);
json = await JsonSerializer.DeserializeAsync<List<SevenTVEmoticon>>(await rawJson.Content.ReadAsStreamAsync());
rawJson = await Client.GetAsync("https://7tv.io/v3/users/twitch/" + ChannelID, Token);
emotes = (await JsonSerializer.DeserializeAsync<SevenTVUserEmotes>(await rawJson.Content.ReadAsStreamAsync(), cancellationToken: Token)).emote_set.emotes;

foreach (var emote in json) {
foreach (var emote in emotes) {
addToList(emote);
}

Expand Down
Loading