Skip to content

Commit

Permalink
Support parsing live video URLs (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
migueloliveiradev authored Sep 23, 2023
1 parent af9b5a3 commit 2095aca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions YoutubeExplode.Tests/VideoIdSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void I_can_parse_a_video_ID_from_an_ID_string(string videoId)
[InlineData("youtu.be/yIVRs6YSbOM", "yIVRs6YSbOM")]
[InlineData("youtube.com/embed/yIVRs6YSbOM", "yIVRs6YSbOM")]
[InlineData("youtube.com/shorts/sKL1vjP0tIo", "sKL1vjP0tIo")]
[InlineData("youtube.com/live/jfKfPfyJRdk", "jfKfPfyJRdk")]
public void I_can_parse_a_video_ID_from_a_URL_string(string videoUrl, string expectedVideoId)
{
// Act
Expand All @@ -41,6 +42,7 @@ public void I_can_parse_a_video_ID_from_a_URL_string(string videoUrl, string exp
[InlineData("youtube.com/xxx?v=pI2I2zqzeKg")]
[InlineData("youtu.be/watch?v=xxx")]
[InlineData("youtube.com/embed/")]
[InlineData("youtube.com/live/")]
public void I_cannot_parse_a_video_ID_from_an_invalid_string(string videoId)
{
// Act & assert
Expand Down
9 changes: 9 additions & 0 deletions YoutubeExplode/Videos/VideoId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ private static bool IsValid(string videoId) =>
if (!string.IsNullOrWhiteSpace(shortsMatch) && IsValid(shortsMatch))
return shortsMatch;

// Live URL
// https://www.youtube.com/live/jfKfPfyJRdk
var liveMatch = Regex
.Match(videoIdOrUrl, @"youtube\..+?/live/(.*?)(?:\?|&|/|$)")
.Groups[1].Value.Pipe(WebUtility.UrlDecode);

if (!string.IsNullOrWhiteSpace(liveMatch) && IsValid(liveMatch))
return liveMatch;

// Invalid input
return null;
}
Expand Down

0 comments on commit 2095aca

Please sign in to comment.