mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-03 00:29:20 +00:00
Add support for Twitch embed projections (#1199)
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -365,6 +365,15 @@
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
// Twitch embed
|
||||
else if (embed.TryGetTwitchClip() is { } twitchClipEmbed)
|
||||
{
|
||||
<div class="chatlog__embed">
|
||||
<div class="chatlog__embed-twitch-container">
|
||||
<iframe class="chatlog__embed-twitch" src="@twitchClipEmbed.Url" width="400" height="225"></iframe>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
// YouTube embed
|
||||
else if (embed.TryGetYouTubeVideo() is { } youTubeVideoEmbed)
|
||||
{
|
||||
|
||||
@@ -702,6 +702,10 @@
|
||||
.chatlog__embed-spotify {
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.chatlog__embed-twitch {
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.chatlog__embed-youtube-container {
|
||||
margin-top: 0.6rem;
|
||||
|
||||
Reference in New Issue
Block a user