-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path7TVEmoteProvider.cs
89 lines (72 loc) · 2.39 KB
/
7TVEmoteProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using SixLabors.ImageSharp;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace MessageHeightTwitch
{
public class SevenTVEmoteProvider : IEmoteProvider
{
public Dictionary<string, SizeF> EmoteCache { get; } = new Dictionary<string, SizeF>();
private static readonly HttpClient Client = new HttpClient();
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 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://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 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, imageSize);
}
}
foreach (var emote in emotes) {
addToList(emote);
}
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 emotes) {
addToList(emote);
}
}
public bool IsEmojiSupported(string Emoji) => false;
}
}