Skip to content

Commit

Permalink
Handle old emotes no longer being avaliable
Browse files Browse the repository at this point in the history
  • Loading branch information
lay295 committed Sep 18, 2019
1 parent bed0812 commit ddb7dc5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
6 changes: 3 additions & 3 deletions TwitchDownloader/frmChatRender.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 41 additions & 6 deletions TwitchDownloader/frmChatRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ private void SetAntiAlias(Graphics g)
private void GetEmotes(List<KeyValuePair<int, Image>> chatEmotes, JToken comments, RenderOptions renderOptions)
{
List<int> alreadyAdded = new List<int>();
List<int> failedEmotes = new List<int>();
using (WebClient client = new WebClient())
{
foreach (var comment in comments)
Expand All @@ -498,20 +499,54 @@ private void GetEmotes(List<KeyValuePair<int, Image>> chatEmotes, JToken comment
if (fragment["emoticon"] != null)
{
int id = fragment["emoticon"]["emoticon_id"].ToObject<int>();
if (!alreadyAdded.Contains(id))
if (!alreadyAdded.Contains(id) && !failedEmotes.Contains(id))
{
alreadyAdded.Add(id);
byte[] bytes = client.DownloadData(String.Format("https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", id));
MemoryStream ms = new MemoryStream(bytes);
Image emoteImage = System.Drawing.Image.FromStream(ms);
chatEmotes.Add(new KeyValuePair<int, Image>(id, emoteImage));
try
{
byte[] bytes = client.DownloadData(String.Format("https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", id));
alreadyAdded.Add(id);
MemoryStream ms = new MemoryStream(bytes);
Image emoteImage = System.Drawing.Image.FromStream(ms);
chatEmotes.Add(new KeyValuePair<int, Image>(id, emoteImage));
}
catch
{
string emoteName = fragment["text"].ToString();
//sometimes emote still exists but id is different, I use twitch metrics because I can't find an api to find an emote by name
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.twitchmetrics.net/e/" + emoteName);
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string redirUrl = response.Headers["Location"];
response.Close();
string newId = redirUrl.Split('/').Last().Split('-').First();
byte[] bytes = client.DownloadData(String.Format("https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", newId));
alreadyAdded.Add(id);
MemoryStream ms = new MemoryStream(bytes);
Image emoteImage = System.Drawing.Image.FromStream(ms);
chatEmotes.Add(new KeyValuePair<int, Image>(id, emoteImage));
}
catch
{
AppendLog("Unable to fetch emote " + emoteName);
failedEmotes.Add(id);
}
}
}
}
}
}
}
}

private void AppendLog(string message)
{
textLog.BeginInvoke((Action)(() =>
textLog.AppendText(message + Environment.NewLine)
));
}

private void DrawUsername(Graphics g, RenderOptions renderOptions, Font nameFont, string userName, Color userColor, ref Size canvasSize, ref Point drawPos)
{
if (renderOptions.outline)
Expand Down

0 comments on commit ddb7dc5

Please sign in to comment.