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 to 7TV v3 api #412

Merged
merged 7 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 45 additions & 15 deletions TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TwitchDownloaderCore.Properties;
using TwitchDownloaderCore.TwitchObjects;
using static TwitchDownloaderCore.TwitchObjects.StvEmoteFlags;

namespace TwitchDownloaderCore
{
Expand Down Expand Up @@ -114,7 +116,7 @@ public static async Task<GqlClipSearchResponse> GetGqlClips(string channelName,

public static async Task<EmoteResponse> GetThirdPartyEmoteData(string streamerId, bool getBttv, bool getFfz, bool getStv)
{
EmoteResponse data = new EmoteResponse();
EmoteResponse emoteReponse = new EmoteResponse();

if (getBttv)
{
Expand All @@ -138,7 +140,7 @@ public static async Task<EmoteResponse> GetThirdPartyEmoteData(string streamerId
string name = emote["code"].ToString();
string mime = emote["imageType"].ToString();
string url = String.Format("https://cdn.betterttv.net/emote/{0}/[scale]x", id);
data.BTTV.Add(new EmoteResponseItem() { Id = id, Code = name, ImageType = mime, ImageUrl = url, IsZeroWidth = bttvZeroWidth.Contains(name) });
emoteReponse.BTTV.Add(new EmoteResponseItem() { Id = id, Code = name, ImageType = mime, ImageUrl = url, IsZeroWidth = bttvZeroWidth.Contains(name) });
}
}

Expand All @@ -162,39 +164,67 @@ public static async Task<EmoteResponse> GetThirdPartyEmoteData(string streamerId
string name = emote["code"].ToString();
string mime = emote["imageType"].ToString();
string url = String.Format("https://cdn.betterttv.net/frankerfacez_emote/{0}/[scale]", id);
data.FFZ.Add(new EmoteResponseItem() { Id = id, Code = name, ImageType = mime, ImageUrl = url });
emoteReponse.FFZ.Add(new EmoteResponseItem() { Id = id, Code = name, ImageType = mime, ImageUrl = url });
}
}

if (getStv)
{
JArray STV = JArray.Parse(await httpClient.GetStringAsync("https://7tv.io/v2/emotes/global"));
string globalEmoteResponse = await httpClient.GetStringAsync("https://7tv.io/v3/emote-sets/global");
JsonElement globalEmoteElement = JsonDocument.Parse(globalEmoteResponse).RootElement.GetProperty("emotes");
JArray stvEmotes = JArray.Parse(globalEmoteElement.ToString());

if (streamerId != null)
{
//Channel might not have 7TV emotes
try
{
STV.Merge(JArray.Parse(await httpClient.GetStringAsync(String.Format("https://api.7tv.app/v2/users/{0}/emotes", streamerId))));
string streamerEmoteResponse = await httpClient.GetStringAsync(string.Format("https://7tv.io/v3/users/twitch/{0}", streamerId));
JsonElement streamerEmoteSetElement = JsonDocument.Parse(streamerEmoteResponse).RootElement.GetProperty("emote_set");
ScrubN marked this conversation as resolved.
Show resolved Hide resolved
JArray streamerEmoteArray = JArray.Parse(streamerEmoteSetElement.GetProperty("emotes").ToString());
stvEmotes.Merge(streamerEmoteArray);
}
catch { }
}


foreach (var emote in STV)
foreach (var stvEmote in stvEmotes)
{
string id = emote["id"].ToString();
string name = emote["name"].ToString();
string mime = emote["mime"].ToString().Split('/')[1];
string url = String.Format("https://cdn.7tv.app/emote/{0}/[scale]x", id);
EmoteResponseItem emoteResponse = new EmoteResponseItem() { Id = id, Code = name, ImageType = mime, ImageUrl = url };
if (emote["visibility_simple"].ToList().Contains("ZERO_WIDTH"))
string emoteId = stvEmote["id"].ToString();
string emoteName = stvEmote["name"].ToString();
JObject emoteData = (JObject)stvEmote["data"];
JObject emoteHost = (JObject)emoteData["host"];
JArray emoteFiles = JArray.Parse(emoteHost["files"].ToString());
ScrubN marked this conversation as resolved.
Show resolved Hide resolved
string emoteFormat = "avif";
foreach (var fileItem in emoteFiles)
{
// prefer webp
if (fileItem["format"].ToString().ToLower().Equals("webp"))
{
emoteFormat = "webp";
break;
}
}
string emoteUrl = string.Format("https:{0}/{1}.{2}", emoteHost["url"].ToString(), "[scale]x", emoteFormat);
StvFlags emoteFlags = (StvFlags)int.Parse(emoteData["flags"].ToString());
bool emoteIsListed = (bool)emoteData["listed"];

EmoteResponseItem emoteResponse = new() { Id = emoteId, Code = emoteName, ImageType = emoteFormat, ImageUrl = emoteUrl };
if ((emoteFlags & StvFlags.ZeroWidth) == StvFlags.ZeroWidth)
{
emoteResponse.IsZeroWidth = true;
data.STV.Add(emoteResponse);
}
if ((emoteFlags & StvFlags.ContentTwitchDisallowed) == StvFlags.ContentTwitchDisallowed || (emoteFlags & StvFlags.Private) == StvFlags.Private)
{
continue;
}
if (emoteIsListed)
{
emoteReponse.STV.Add(emoteResponse);
}
}
}

return data;
return emoteReponse;
}
public static async Task<List<TwitchEmote>> GetThirdPartyEmotes(int streamerId, string cacheFolder, Emotes embededEmotes = null, bool bttv = true, bool ffz = true, bool stv = true)
{
Expand Down
29 changes: 29 additions & 0 deletions TwitchDownloaderCore/TwitchObjects/stvEmoteFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace TwitchDownloaderCore.TwitchObjects
{
public class StvEmoteFlags
{
// https://github.com/SevenTV/Common/blob/4139fcc3eb8d79003573b26b552ef112ec85b8df/structures/v3/type.emote.go#L62
[Flags]
public enum StvFlags
{
Private = 1 << 0, // The emote is private and can only be accessed by its owner, editors and moderators

Authentic = 1 << 1, // The emote was verified to be an original creation by the uploader

ZeroWidth = 1 << 8, // The emote is recommended to be enabled as Zero-Width


// Content Flags

ContentSexual = 1 << 16, // Sexually Suggesive

ContentEpilepsy = 1 << 17, // Rapid flashing

ContentEdgy = 1 << 18, // Edgy or distasteful, may be offensive to some users

ContentTwitchDisallowed = 1 << 24, // Not allowed specifically on the Twitch platform
};
}
}