Skip to content

Commit

Permalink
Support URLs ending with /
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Jul 20, 2024
1 parent 508b5d4 commit 2114078
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions TwitchDownloaderCore/Tools/TwitchRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace TwitchDownloaderCore.Tools
public static class TwitchRegex
{
// TODO: Use source generators when .NET7
private static readonly Regex VideoId = new(@"(?<=^|twitch\.tv\/videos\/)\d+(?=$|\?|\s)", RegexOptions.Compiled);
private static readonly Regex HighlightId = new(@"(?<=^|twitch\.tv\/\w+\/v(?:ideo)?\/)\d+(?=$|\?|\s)", RegexOptions.Compiled);
private static readonly Regex ClipId = new(@"(?<=^|(?:clips\.)?twitch\.tv\/(?:\w+\/clip\/)?)[\w-]+?(?=$|\?|\s)", RegexOptions.Compiled);
private static readonly Regex VideoId = new(@"(?<=^|twitch\.tv\/videos\/)\d+(?=\/?(?:$|\?))", RegexOptions.Compiled);
private static readonly Regex HighlightId = new(@"(?<=^|twitch\.tv\/\w+\/v(?:ideo)?\/)\d+(?=\/?(?:$|\?))", RegexOptions.Compiled);
private static readonly Regex ClipId = new(@"(?<=^|(?:clips\.)?twitch\.tv\/(?:\w+\/clip\/)?)[\w-]+?(?=\/?(?:$|\?))", RegexOptions.Compiled);

public static readonly Regex UrlTimeCode = new(@"(?<=(?:\?|&)t=)\d+h\d+m\d+s(?=$|\?|\s)", RegexOptions.Compiled);
public static readonly Regex BitsRegex = new(
Expand All @@ -20,6 +20,8 @@ public static class TwitchRegex
[return: MaybeNull]
public static Match MatchVideoId(string text)
{
text = text.Trim();

var videoIdMatch = VideoId.Match(text);
if (videoIdMatch.Success)
{
Expand All @@ -39,6 +41,8 @@ public static Match MatchVideoId(string text)
[return: MaybeNull]
public static Match MatchClipId(string text)
{
text = text.Trim();

var clipIdMatch = ClipId.Match(text);
if (clipIdMatch.Success && !clipIdMatch.Value.All(char.IsDigit))
{
Expand All @@ -52,6 +56,8 @@ public static Match MatchClipId(string text)
[return: MaybeNull]
public static Match MatchVideoOrClipId(string text)
{
text = text.Trim();

var videoIdMatch = MatchVideoId(text);
if (videoIdMatch is { Success: true })
{
Expand Down

0 comments on commit 2114078

Please sign in to comment.