Skip to content

Commit ddb7dc5

Browse files
committed
Handle old emotes no longer being avaliable
1 parent bed0812 commit ddb7dc5

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

TwitchDownloader/frmChatRender.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TwitchDownloader/frmChatRender.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ private void SetAntiAlias(Graphics g)
489489
private void GetEmotes(List<KeyValuePair<int, Image>> chatEmotes, JToken comments, RenderOptions renderOptions)
490490
{
491491
List<int> alreadyAdded = new List<int>();
492+
List<int> failedEmotes = new List<int>();
492493
using (WebClient client = new WebClient())
493494
{
494495
foreach (var comment in comments)
@@ -498,20 +499,54 @@ private void GetEmotes(List<KeyValuePair<int, Image>> chatEmotes, JToken comment
498499
if (fragment["emoticon"] != null)
499500
{
500501
int id = fragment["emoticon"]["emoticon_id"].ToObject<int>();
501-
if (!alreadyAdded.Contains(id))
502+
if (!alreadyAdded.Contains(id) && !failedEmotes.Contains(id))
502503
{
503-
alreadyAdded.Add(id);
504-
byte[] bytes = client.DownloadData(String.Format("https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", id));
505-
MemoryStream ms = new MemoryStream(bytes);
506-
Image emoteImage = System.Drawing.Image.FromStream(ms);
507-
chatEmotes.Add(new KeyValuePair<int, Image>(id, emoteImage));
504+
try
505+
{
506+
byte[] bytes = client.DownloadData(String.Format("https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", id));
507+
alreadyAdded.Add(id);
508+
MemoryStream ms = new MemoryStream(bytes);
509+
Image emoteImage = System.Drawing.Image.FromStream(ms);
510+
chatEmotes.Add(new KeyValuePair<int, Image>(id, emoteImage));
511+
}
512+
catch
513+
{
514+
string emoteName = fragment["text"].ToString();
515+
//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
516+
try
517+
{
518+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.twitchmetrics.net/e/" + emoteName);
519+
request.AllowAutoRedirect = false;
520+
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
521+
string redirUrl = response.Headers["Location"];
522+
response.Close();
523+
string newId = redirUrl.Split('/').Last().Split('-').First();
524+
byte[] bytes = client.DownloadData(String.Format("https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", newId));
525+
alreadyAdded.Add(id);
526+
MemoryStream ms = new MemoryStream(bytes);
527+
Image emoteImage = System.Drawing.Image.FromStream(ms);
528+
chatEmotes.Add(new KeyValuePair<int, Image>(id, emoteImage));
529+
}
530+
catch
531+
{
532+
AppendLog("Unable to fetch emote " + emoteName);
533+
failedEmotes.Add(id);
534+
}
535+
}
508536
}
509537
}
510538
}
511539
}
512540
}
513541
}
514542

543+
private void AppendLog(string message)
544+
{
545+
textLog.BeginInvoke((Action)(() =>
546+
textLog.AppendText(message + Environment.NewLine)
547+
));
548+
}
549+
515550
private void DrawUsername(Graphics g, RenderOptions renderOptions, Font nameFont, string userName, Color userColor, ref Size canvasSize, ref Point drawPos)
516551
{
517552
if (renderOptions.outline)

0 commit comments

Comments
 (0)