From 4ea57e65686caaba56d3e750c10af7aac721f7d9 Mon Sep 17 00:00:00 2001 From: Ivan <4sotnikovi@mail.ru> Date: Fri, 2 Jul 2021 20:36:19 +0000 Subject: [PATCH] Add 7tv emotes support (#193) * feat: add 7tv support (probably) * Add 7tv and it's cli argument to README's Also in main README make the cli README link relative * Add missing pieces to 7tv support It should now fully support 7tv emotes --- README.md | 4 +- TwitchDownloaderCLI/Options.cs | 2 + TwitchDownloaderCLI/Program.cs | 1 + TwitchDownloaderCLI/README.md | 3 + TwitchDownloaderCore/ChatRenderer.cs | 2 +- .../Options/ChatRenderOptions.cs | 1 + TwitchDownloaderCore/TwitchHelper.cs | 60 ++++++++++++++++++- TwitchDownloaderWPF/App.config | 3 + TwitchDownloaderWPF/PageChatRender.xaml | 2 + TwitchDownloaderWPF/PageChatRender.xaml.cs | 4 +- .../Properties/Settings.settings | 5 +- 11 files changed, 81 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c83380f4..88c924dd 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Twitch VOD/Clip/Chat downloader and chat renderer I wrote. - Download Twitch VODs - Download Twitch Clips - Download chat for VODS and Clips, in either a [JSON with all the information](https://pastebin.com/raw/YDgRe6X4) or a [simple text file](https://pastebin.com/raw/016azeQX) -- Use a previously generated JSON chat file to render the chat with FFZ and BTTV support (including GIFS) +- Use a previously generated JSON chat file to render the chat with FFZ, BTTV and 7TV support (including GIFS) ## Things still to be done - Fix bugs that slipped by @@ -32,7 +32,7 @@ Sorry, the GUI version is only avaliable for Windows :( The CLI is cross platform and performs the main functions of the program. It works on Windows and Linux, but has not been tested on MacOS. -[Documentation here](https://github.com/lay295/TwitchDownloader/blob/master/TwitchDownloaderCLI/README.md). +[Documentation here](TwitchDownloaderCLI/README.md). I've never really made a command line utility before so things may change in the future. If you're on Linux, make sure `fontconfig` and `libfontconfig1` are installed `(apt-get install fontconfig libfontconfig1)`. diff --git a/TwitchDownloaderCLI/Options.cs b/TwitchDownloaderCLI/Options.cs index ce3047d2..9294e09c 100644 --- a/TwitchDownloaderCLI/Options.cs +++ b/TwitchDownloaderCLI/Options.cs @@ -52,6 +52,8 @@ class Options public bool BttvEmotes { get; set; } [Option("ffz", Default = true, HelpText = "Enable FFZ emotes in chat render.")] public bool FfzEmotes { get; set; } + [Option("stv", Default = true, HelpText = "Enable 7tv emotes in chat render.")] + public bool StvEmotes { get; set; } [Option("outline", Default = false, HelpText = "Enable outline in chat render.")] public bool Outline { get; set; } [Option("sub-messages", Default = true, HelpText = "Enable sub messages.")] diff --git a/TwitchDownloaderCLI/Program.cs b/TwitchDownloaderCLI/Program.cs index a52d2b67..8c3d3ac9 100644 --- a/TwitchDownloaderCLI/Program.cs +++ b/TwitchDownloaderCLI/Program.cs @@ -187,6 +187,7 @@ private static void RenderChat(Options inputOptions) renderOptions.ChatWidth = inputOptions.ChatWidth; renderOptions.BttvEmotes = inputOptions.BttvEmotes; renderOptions.FfzEmotes = inputOptions.FfzEmotes; + renderOptions.StvEmotes = inputOptions.StvEmotes; renderOptions.Outline = inputOptions.Outline; renderOptions.OutlineSize = inputOptions.OutlineSize; renderOptions.Font = inputOptions.Font; diff --git a/TwitchDownloaderCLI/README.md b/TwitchDownloaderCLI/README.md index f7199323..75721abf 100644 --- a/TwitchDownloaderCLI/README.md +++ b/TwitchDownloaderCLI/README.md @@ -88,6 +88,9 @@ Path to JSON chat file input. **-\-ffz** (Default: true) Enable FFZ emotes. +**-\-stv** +(Default: true) Enable 7TV emotes. + **-\-sub-messages** (Default: true) Enable sub/re-sub messages. diff --git a/TwitchDownloaderCore/ChatRenderer.cs b/TwitchDownloaderCore/ChatRenderer.cs index 717bf985..09968c3c 100644 --- a/TwitchDownloaderCore/ChatRenderer.cs +++ b/TwitchDownloaderCore/ChatRenderer.cs @@ -53,7 +53,7 @@ public async Task RenderVideoAsync(IProgress progress, Cancellat progress.Report(new ProgressReport() { reportType = ReportType.Message, data = "Fetching Emotes" }); List chatEmotes = await Task.Run(() => TwitchHelper.GetEmotes(chatJson.comments, cacheFolder, chatJson.emotes, true)); progress.Report(new ProgressReport() { reportType = ReportType.Message, data = "Fetching 3rd Party Emotes" }); - List thirdPartyEmotes = await Task.Run(() => TwitchHelper.GetThirdPartyEmotes(chatJson.streamer.id, cacheFolder, chatJson.emotes, renderOptions.BttvEmotes, renderOptions.FfzEmotes)); + List thirdPartyEmotes = await Task.Run(() => TwitchHelper.GetThirdPartyEmotes(chatJson.streamer.id, cacheFolder, chatJson.emotes, renderOptions.BttvEmotes, renderOptions.FfzEmotes, renderOptions.StvEmotes)); progress.Report(new ProgressReport() { reportType = ReportType.Message, data = "Fetching Cheer Emotes" }); List cheerEmotes = await Task.Run(() => TwitchHelper.GetBits(cacheFolder)); progress.Report(new ProgressReport() { reportType = ReportType.Message, data = "Fetching Emojis" }); diff --git a/TwitchDownloaderCore/Options/ChatRenderOptions.cs b/TwitchDownloaderCore/Options/ChatRenderOptions.cs index 5a8e6cfc..b73f22ee 100644 --- a/TwitchDownloaderCore/Options/ChatRenderOptions.cs +++ b/TwitchDownloaderCore/Options/ChatRenderOptions.cs @@ -16,6 +16,7 @@ public class ChatRenderOptions public int ChatWidth { get; set; } public bool BttvEmotes { get; set; } public bool FfzEmotes { get; set; } + public bool StvEmotes { get; set; } public bool Outline { get; set; } public double OutlineSize { get; set; } public string Font { get; set; } diff --git a/TwitchDownloaderCore/TwitchHelper.cs b/TwitchDownloaderCore/TwitchHelper.cs index 983159b1..3d0c66ed 100644 --- a/TwitchDownloaderCore/TwitchHelper.cs +++ b/TwitchDownloaderCore/TwitchHelper.cs @@ -76,13 +76,14 @@ public static async Task GetClipLinks(object clipId) } } - public static List GetThirdPartyEmotes(int streamerId, string cacheFolder, Emotes embededEmotes = null, bool bttv = true, bool ffz = true) + public static List GetThirdPartyEmotes(int streamerId, string cacheFolder, Emotes embededEmotes = null, bool bttv = true, bool ffz = true, bool stv = true) { List returnList = new List(); List alreadyAdded = new List(); string bttvFolder = Path.Combine(cacheFolder, "bttv"); string ffzFolder = Path.Combine(cacheFolder, "ffz"); + string stvFolder = Path.Combine(cacheFolder, "stv"); if (embededEmotes != null) { @@ -245,6 +246,63 @@ public static List GetThirdPartyEmotes(int streamerId, string cache } catch { } } + + if (stv) + { + if (!Directory.Exists(stvFolder)) + Directory.CreateDirectory(stvFolder); + + //Global 7tv Emotes + JArray STV = JArray.Parse(client.DownloadString("https://api.7tv.app/v2/emotes/global")); + foreach (var emote in STV) + { + string id = emote["id"].ToString(); + string name = emote["name"].ToString(); + string url2x = emote["urls"][1][1].ToString(); // 2x + if (alreadyAdded.Contains(name)) + continue; + byte[] bytes; + string fileName = Path.Combine(stvFolder, id + "_2x.png"); + if (File.Exists(fileName)) + bytes = File.ReadAllBytes(fileName); + else + { + bytes = client.DownloadData(url2x); + File.WriteAllBytes(fileName, bytes); + } + + MemoryStream ms = new MemoryStream(bytes); + returnList.Add(new TwitchEmote(new List() { SKBitmap.Decode(bytes) }, SKCodec.Create(ms), name, emote["mime"].ToString().Split('/')[1], id, 2, bytes)); + alreadyAdded.Add(name); + } + + //Channel specific 7tv emotes + try + { + JArray STV_channel = JArray.Parse(client.DownloadString(String.Format("https://api.7tv.app/v2/users/{0}/emotes", streamerId))); + foreach (var emote in STV_channel) + { + string id = emote["id"].ToString(); + string name = emote["name"].ToString(); + string url2x = emote["urls"][1][1].ToString(); // 2x + if (alreadyAdded.Contains(name)) + continue; + byte[] bytes; + string fileName = Path.Combine(stvFolder, id + "_2x.png"); + if (File.Exists(fileName)) + bytes = File.ReadAllBytes(fileName); + else + { + bytes = client.DownloadData(url2x); + File.WriteAllBytes(fileName, bytes); + } + MemoryStream ms = new MemoryStream(bytes); + returnList.Add(new TwitchEmote(new List() { SKBitmap.Decode(bytes) }, SKCodec.Create(ms), name, emote["mime"].ToString().Split('/')[1], id, 2, bytes)); + alreadyAdded.Add(name); + } + } + catch { } + } } return returnList; diff --git a/TwitchDownloaderWPF/App.config b/TwitchDownloaderWPF/App.config index b7c1d8a5..d80e3e0c 100644 --- a/TwitchDownloaderWPF/App.config +++ b/TwitchDownloaderWPF/App.config @@ -56,6 +56,9 @@ True + + True + 17 diff --git a/TwitchDownloaderWPF/PageChatRender.xaml b/TwitchDownloaderWPF/PageChatRender.xaml index 026d35ae..dbc3d0af 100644 --- a/TwitchDownloaderWPF/PageChatRender.xaml +++ b/TwitchDownloaderWPF/PageChatRender.xaml @@ -37,6 +37,7 @@ + @@ -46,6 +47,7 @@ + diff --git a/TwitchDownloaderWPF/PageChatRender.xaml.cs b/TwitchDownloaderWPF/PageChatRender.xaml.cs index a06f08ff..f9f19331 100644 --- a/TwitchDownloaderWPF/PageChatRender.xaml.cs +++ b/TwitchDownloaderWPF/PageChatRender.xaml.cs @@ -80,6 +80,7 @@ private async void btnRender_Click(object sender, RoutedEventArgs e) ChatWidth = Int32.Parse(textWidth.Text), BttvEmotes = (bool)checkBTTV.IsChecked, FfzEmotes = (bool)checkFFZ.IsChecked, + StvEmotes = (bool)checkSTV.IsChecked, Outline = (bool)checkOutline.IsChecked, Font = (string)comboFont.SelectedItem, FontSize = Double.Parse(textFontSize.Text), @@ -219,6 +220,7 @@ private void SaveSettings() Settings.Default.BackgroundColorA = colorBackground.SelectedColor.Value.A; Settings.Default.FFZEmotes = (bool)checkFFZ.IsChecked; Settings.Default.BTTVEmotes = (bool)checkBTTV.IsChecked; + Settings.Default.STVEmotes = (bool)checkSTV.IsChecked; Settings.Default.FontColorR = colorFont.SelectedColor.Value.R; Settings.Default.FontColorG = colorFont.SelectedColor.Value.G; Settings.Default.FontColorB = colorFont.SelectedColor.Value.B; @@ -430,4 +432,4 @@ public override string ToString() return Name; } } -} \ No newline at end of file +} diff --git a/TwitchDownloaderWPF/Properties/Settings.settings b/TwitchDownloaderWPF/Properties/Settings.settings index ca76b054..d4b32470 100644 --- a/TwitchDownloaderWPF/Properties/Settings.settings +++ b/TwitchDownloaderWPF/Properties/Settings.settings @@ -32,6 +32,9 @@ True + + True + 17 @@ -99,4 +102,4 @@ True - \ No newline at end of file +