diff --git a/DiscordChatExporter.Cli.Tests/Specs/HtmlEmbedSpecs.cs b/DiscordChatExporter.Cli.Tests/Specs/HtmlEmbedSpecs.cs index 8ed0fc5..765bc52 100644 --- a/DiscordChatExporter.Cli.Tests/Specs/HtmlEmbedSpecs.cs +++ b/DiscordChatExporter.Cli.Tests/Specs/HtmlEmbedSpecs.cs @@ -150,6 +150,26 @@ public class HtmlEmbedSpecs iframeUrl.Should().StartWith("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP"); } + [Fact] + public async Task I_can_export_a_channel_that_contains_a_message_with_a_Twitch_clip_embed() + { + // https://github.com/Tyrrrz/DiscordChatExporter/issues/1196 + + // Act + var message = await ExportWrapper.GetMessageAsHtmlAsync( + ChannelIds.EmbedTestCases, + Snowflake.Parse("1207002986128216074") + ); + + // Assert + var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src"); + iframeUrl + .Should() + .StartWith( + "https://clips.twitch.tv/embed?clip=SpicyMildCiderThisIsSparta--PQhbllrvej_Ee7v" + ); + } + [Fact] public async Task I_can_export_a_channel_that_contains_a_message_with_a_YouTube_video_embed() { diff --git a/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs b/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs index 1d2d0b3..ccc42d9 100644 --- a/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs +++ b/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs @@ -31,6 +31,9 @@ public partial record Embed( public SpotifyTrackEmbedProjection? TryGetSpotifyTrack() => SpotifyTrackEmbedProjection.TryResolve(this); + public TwitchClipEmbedProjection? TryGetTwitchClip() => + TwitchClipEmbedProjection.TryResolve(this); + public YouTubeVideoEmbedProjection? TryGetYouTubeVideo() => YouTubeVideoEmbedProjection.TryResolve(this); } diff --git a/DiscordChatExporter.Core/Discord/Data/Embeds/TwitchClipEmbedProjection.cs b/DiscordChatExporter.Core/Discord/Data/Embeds/TwitchClipEmbedProjection.cs new file mode 100644 index 0000000..3c7da13 --- /dev/null +++ b/DiscordChatExporter.Core/Discord/Data/Embeds/TwitchClipEmbedProjection.cs @@ -0,0 +1,53 @@ +using System.Text.RegularExpressions; + +namespace DiscordChatExporter.Core.Discord.Data.Embeds; + +public partial record TwitchClipEmbedProjection(string ClipId) +{ + public string Url => $"https://clips.twitch.tv/embed?clip={ClipId}&parent=localhost"; +} + +public partial record TwitchClipEmbedProjection +{ + private static string? TryParseClipId(string embedUrl) + { + // https://clips.twitch.tv/SpookyTenuousPidgeonPanicVis + { + var clipId = Regex + .Match(embedUrl, @"clips\.twitch\.tv/(.*?)(?:\?|&|/|$)") + .Groups[1] + .Value; + + if (!string.IsNullOrWhiteSpace(clipId)) + return clipId; + } + + // https://twitch.tv/clip/SpookyTenuousPidgeonPanicVis + { + var clipId = Regex + .Match(embedUrl, @"twitch\.tv/clip/(.*?)(?:\?|&|/|$)") + .Groups[1] + .Value; + + if (!string.IsNullOrWhiteSpace(clipId)) + return clipId; + } + + return null; + } + + public static TwitchClipEmbedProjection? TryResolve(Embed embed) + { + if (embed.Kind != EmbedKind.Video) + return null; + + if (string.IsNullOrWhiteSpace(embed.Url)) + return null; + + var clipId = TryParseClipId(embed.Url); + if (string.IsNullOrWhiteSpace(clipId)) + return null; + + return new TwitchClipEmbedProjection(clipId); + } +} diff --git a/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml b/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml index 92f8a43..45f4738 100644 --- a/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/MessageGroupTemplate.cshtml @@ -365,6 +365,15 @@ } + // Twitch embed + else if (embed.TryGetTwitchClip() is { } twitchClipEmbed) + { +
+
+ +
+
+ } // YouTube embed else if (embed.TryGetYouTubeVideo() is { } youTubeVideoEmbed) { diff --git a/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml b/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml index a71fd83..9e746e1 100644 --- a/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/PreambleTemplate.cshtml @@ -702,6 +702,10 @@ .chatlog__embed-spotify { border: 0; } + + .chatlog__embed-twitch { + border: 0; + } .chatlog__embed-youtube-container { margin-top: 0.6rem;