fix(wpf): update roomid matching regex

fix #516
This commit is contained in:
genteure 2023-09-09 07:30:32 +08:00
parent bced5b41a0
commit 2d6d682165
4 changed files with 96 additions and 4 deletions

View File

@ -0,0 +1,9 @@
using System.Text.RegularExpressions;
namespace BililiveRecorder.Core
{
public static class RoomIdFromUrl
{
public static readonly Regex Regex = new Regex("""^(?:(?:https?:\/\/)?live\.bilibili\.com\/(?:blanc\/|h5\/)?)?(\d+)\/?(?:[#\?].*)?$""", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
}
}

View File

@ -23,9 +23,6 @@ namespace BililiveRecorder.WPF.Pages
public partial class RoomListPage public partial class RoomListPage
{ {
private static readonly ILogger logger = Log.ForContext<RoomListPage>(); private static readonly ILogger logger = Log.ForContext<RoomListPage>();
private static readonly Regex RoomIdRegex
= new Regex(@"^(?:https?:\/\/)?live\.bilibili\.com\/(?:blanc\/|h5\/)?(\d*)(?:\?.*)?$",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
private readonly IRoom?[] NullRoom = new IRoom?[] { null }; private readonly IRoom?[] NullRoom = new IRoom?[] { null };
@ -159,7 +156,7 @@ namespace BililiveRecorder.WPF.Pages
if (!int.TryParse(input, out var roomid)) if (!int.TryParse(input, out var roomid))
{ {
var m = RoomIdRegex.Match(input); var m = RoomIdFromUrl.Regex.Match(input);
if (m.Success && m.Groups.Count > 1 && int.TryParse(m.Groups[1].Value, out var result2)) if (m.Success && m.Groups.Count > 1 && int.TryParse(m.Groups[1].Value, out var result2))
{ {
roomid = result2; roomid = result2;

View File

@ -474,6 +474,10 @@ namespace BililiveRecorder.Core
public readonly Microsoft.Extensions.Caching.Memory.MemoryCache memoryCache; public readonly Microsoft.Extensions.Caching.Memory.MemoryCache memoryCache;
public PollyPolicy() { } public PollyPolicy() { }
} }
public static class RoomIdFromUrl
{
public static readonly System.Text.RegularExpressions.Regex Regex;
}
public class RoomStats : System.ComponentModel.INotifyPropertyChanged public class RoomStats : System.ComponentModel.INotifyPropertyChanged
{ {
public RoomStats() { } public RoomStats() { }

View File

@ -0,0 +1,82 @@
using System.Collections.Generic;
using Xunit;
namespace BililiveRecorder.Core.UnitTests
{
public class RoomIdFromUrlTests
{
[Theory]
[MemberData(nameof(ShouldMatchData))]
public void ShouldMatch(string id, string url)
{
var m = RoomIdFromUrl.Regex.Match(url);
Assert.True(m.Success);
Assert.Equal(id, m.Groups[1].Value);
}
public static TheoryData<string, string> ShouldMatchData()
{
var data = new TheoryData<string, string>();
static IEnumerable<string> basePaths()
{
yield return "123";
yield return "123?abc=def";
yield return "123#anything";
yield return "123#/";
yield return "123?abc=def#anything";
yield return "123/";
yield return "123/?abc=def";
yield return "123/#anything";
yield return "123/#/";
yield return "123/?abc=def#anything";
}
var prefix = new[]
{
"live.bilibili.com/",
"http://live.bilibili.com/",
"https://live.bilibili.com/",
"live.bilibili.com/blanc/",
"http://live.bilibili.com/blanc/",
"https://live.bilibili.com/blanc/",
"live.bilibili.com/h5/",
"http://live.bilibili.com/h5/",
"https://live.bilibili.com/h5/",
};
foreach (var p in prefix)
{
foreach (var b in basePaths())
{
data.Add("123", p + b);
}
}
return data;
}
[Theory]
[MemberData(nameof(ShouldNotMatchData))]
public void ShouldNotMatch(string url)
{
Assert.DoesNotMatch(RoomIdFromUrl.Regex, url);
}
public static TheoryData<string> ShouldNotMatchData()
{
var data = new TheoryData<string>
{
"https://live.bilibili.com/123/a",
"https://live.bilibili.com/123a",
"https://live.bilibili.com/123a/",
"https://live.bilibili.com/notnumber",
"live.bilibili.com/otherpage/123",
"ftp://live.bilibili.com/123"
};
return data;
}
}
}