From 3d047321cfecce143c2a93cf64966f03d81f5e95 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Wed, 31 May 2023 22:14:58 +0300 Subject: [PATCH] Parse YouTube video IDs natively --- .../Embeds/YouTubeVideoEmbedProjection.cs | 39 ++----------------- .../DiscordChatExporter.Core.csproj | 1 + 2 files changed, 4 insertions(+), 36 deletions(-) diff --git a/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs b/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs index cda1afb..f8a2e1b 100644 --- a/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs +++ b/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs @@ -1,6 +1,4 @@ -using System.Text.RegularExpressions; - -namespace DiscordChatExporter.Core.Discord.Data.Embeds; +namespace DiscordChatExporter.Core.Discord.Data.Embeds; public partial record YouTubeVideoEmbedProjection(string VideoId) { @@ -9,37 +7,6 @@ public partial record YouTubeVideoEmbedProjection(string VideoId) public partial record YouTubeVideoEmbedProjection { - // Adapted from YoutubeExplode - // https://github.com/Tyrrrz/YoutubeExplode/blob/bc700b631bd105d0be208a88116347034bdca88b/YoutubeExplode/Videos/VideoId.cs#L40-L62 - private static string? TryParseVideoId(string embedUrl) - { - // Regular URL - // https://www.youtube.com/watch?v=yIVRs6YSbOM - var regularMatch = Regex.Match(embedUrl, @"youtube\..+?/watch.*?v=(.*?)(?:&|/|$)").Groups[1].Value; - if (!string.IsNullOrWhiteSpace(regularMatch)) - return regularMatch; - - // Short URL - // https://youtu.be/yIVRs6YSbOM - var shortMatch = Regex.Match(embedUrl, @"youtu\.be/(.*?)(?:\?|&|/|$)").Groups[1].Value; - if (!string.IsNullOrWhiteSpace(shortMatch)) - return shortMatch; - - // Embed URL - // https://www.youtube.com/embed/yIVRs6YSbOM - var embedMatch = Regex.Match(embedUrl, @"youtube\..+?/embed/(.*?)(?:\?|&|/|$)").Groups[1].Value; - if (!string.IsNullOrWhiteSpace(embedMatch)) - return embedMatch; - - // Shorts URL - // https://www.youtube.com/shorts/sKL1vjP0tIo - var shortsMatch = Regex.Match(embedUrl, @"youtube\..+?/shorts/(.*?)(?:\?|&|/|$)").Groups[1].Value; - if (!string.IsNullOrWhiteSpace(shortsMatch)) - return shortsMatch; - - return null; - } - public static YouTubeVideoEmbedProjection? TryResolve(Embed embed) { if (embed.Kind != EmbedKind.Video) @@ -48,8 +15,8 @@ public partial record YouTubeVideoEmbedProjection if (string.IsNullOrWhiteSpace(embed.Url)) return null; - var videoId = TryParseVideoId(embed.Url); - if (string.IsNullOrWhiteSpace(videoId)) + var videoId = YoutubeExplode.Videos.VideoId.TryParse(embed.Url); + if (videoId is null) return null; return new YouTubeVideoEmbedProjection(videoId); diff --git a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj index cec267c..8eb16b7 100644 --- a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj +++ b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj @@ -8,6 +8,7 @@ + \ No newline at end of file