-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move id parsing related functions to IdParse, TwitchRegexTests -> IdP…
…arseTests
- Loading branch information
Showing
9 changed files
with
87 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace TwitchDownloaderCore.Tools | ||
{ | ||
public static class IdParse | ||
{ | ||
// TODO: Use source generators when .NET7 | ||
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); | ||
|
||
/// <returns>A <see cref="Match"/> of the video's id or <see langword="null"/>.</returns> | ||
[return: MaybeNull] | ||
public static Match MatchVideoId(string text) | ||
{ | ||
text = text.Trim(); | ||
|
||
var videoIdMatch = VideoId.Match(text); | ||
if (videoIdMatch.Success) | ||
{ | ||
return videoIdMatch; | ||
} | ||
|
||
var highlightIdMatch = HighlightId.Match(text); | ||
if (highlightIdMatch.Success) | ||
{ | ||
return highlightIdMatch; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <returns>A <see cref="Match"/> of the clip's id or <see langword="null"/>.</returns> | ||
[return: MaybeNull] | ||
public static Match MatchClipId(string text) | ||
{ | ||
text = text.Trim(); | ||
|
||
var clipIdMatch = ClipId.Match(text); | ||
if (clipIdMatch.Success && !clipIdMatch.Value.All(char.IsDigit)) | ||
{ | ||
return clipIdMatch; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <returns>A <see cref="Match"/> of the video/clip's id or <see langword="null"/>.</returns> | ||
[return: MaybeNull] | ||
public static Match MatchVideoOrClipId(string text) | ||
{ | ||
text = text.Trim(); | ||
|
||
var videoIdMatch = MatchVideoId(text); | ||
if (videoIdMatch is { Success: true }) | ||
{ | ||
return videoIdMatch; | ||
} | ||
|
||
var clipIdMatch = MatchClipId(text); | ||
if (clipIdMatch is { Success: true }) | ||
{ | ||
return clipIdMatch; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,12 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace TwitchDownloaderCore.Tools | ||
{ | ||
public static class TwitchRegex | ||
{ | ||
// TODO: Use source generators when .NET7 | ||
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( | ||
@"(?<=(?:\s|^)(?:4Head|Anon|Bi(?:bleThumb|tBoss)|bday|C(?:h(?:eer|arity)|orgo)|cheerwal|D(?:ansGame|oodleCheer)|EleGiggle|F(?:rankerZ|ailFish)|Goal|H(?:eyGuys|olidayCheer)|K(?:appa|reygasm)|M(?:rDestructoid|uxy)|NotLikeThis|P(?:arty|ride|JSalt)|RIPCheer|S(?:coops|h(?:owLove|amrock)|eemsGood|wiftRage|treamlabs)|TriHard|uni|VoHiYo))[1-9]\d{0,6}(?=\s|$)", | ||
RegexOptions.Compiled); | ||
|
||
/// <returns>A <see cref="Match"/> of the video's id or <see langword="null"/>.</returns> | ||
[return: MaybeNull] | ||
public static Match MatchVideoId(string text) | ||
{ | ||
text = text.Trim(); | ||
|
||
var videoIdMatch = VideoId.Match(text); | ||
if (videoIdMatch.Success) | ||
{ | ||
return videoIdMatch; | ||
} | ||
|
||
var highlightIdMatch = HighlightId.Match(text); | ||
if (highlightIdMatch.Success) | ||
{ | ||
return highlightIdMatch; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <returns>A <see cref="Match"/> of the clip's id or <see langword="null"/>.</returns> | ||
[return: MaybeNull] | ||
public static Match MatchClipId(string text) | ||
{ | ||
text = text.Trim(); | ||
|
||
var clipIdMatch = ClipId.Match(text); | ||
if (clipIdMatch.Success && !clipIdMatch.Value.All(char.IsDigit)) | ||
{ | ||
return clipIdMatch; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <returns>A <see cref="Match"/> of the video/clip's id or <see langword="null"/>.</returns> | ||
[return: MaybeNull] | ||
public static Match MatchVideoOrClipId(string text) | ||
{ | ||
text = text.Trim(); | ||
|
||
var videoIdMatch = MatchVideoId(text); | ||
if (videoIdMatch is { Success: true }) | ||
{ | ||
return videoIdMatch; | ||
} | ||
|
||
var clipIdMatch = MatchClipId(text); | ||
if (clipIdMatch is { Success: true }) | ||
{ | ||
return clipIdMatch; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters