feat: 使标签加载器支持优先级配置。

This commit is contained in:
real-zony 2021-06-05 12:15:30 +08:00
parent 5520162a4c
commit a032f3e5eb
7 changed files with 36 additions and 27 deletions

View File

@ -1,4 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using ZonyLrcTools.Cli.Infrastructure.Lyric;
using ZonyLrcTools.Cli.Infrastructure.Network;
@ -19,9 +18,9 @@ namespace ZonyLrcTools.Cli.Config
public LyricItemCollectionOption LyricOption { get; set; }
/// <summary>
/// 标签加载器的加载配置项
/// 标签加载器相关的配置属性
/// </summary>
public TagInfoProviderOptions TagInfoProviderOptions { get; set; }
public IEnumerable<TagInfoProviderInstance> TagInfoProviderOptions { get; set; }
/// <summary>
/// 网络代理相关的配置信息。

View File

@ -11,7 +11,7 @@ namespace ZonyLrcTools.Cli.Infrastructure.Tag
/// <summary>
/// 默认的标签加载器 <see cref="ITagLoader"/> 实现。
/// </summary>
public class DefaultTagLoader : ITagLoader, ITransientDependency
public class DefaultTagLoader : ITagLoader, ISingletonDependency
{
protected readonly IEnumerable<ITagInfoProvider> TagInfoProviders;
protected readonly IBlockWordDictionary BlockWordDictionary;
@ -33,7 +33,7 @@ namespace ZonyLrcTools.Cli.Infrastructure.Tag
throw new ErrorCodeException(ErrorCodes.LoadTagInfoProviderError);
}
foreach (var provider in TagInfoProviders.OrderBy(p => p.Priority))
foreach (var provider in TagInfoProviders)
{
var info = await provider.LoadAsync(filePath);
if (info != null)

View File

@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
@ -10,25 +11,28 @@ namespace ZonyLrcTools.Cli.Infrastructure.Tag
/// <summary>
/// 基于正则表达式的标签解析器,从文件名当中解析歌曲的标签信息。
/// </summary>
public class FileNameTagInfoProvider : ITagInfoProvider, ITransientDependency
public class FileNameTagInfoProvider : ITagInfoProvider, ISingletonDependency
{
public int Priority => 2;
public string Name => ConstantName;
public const string ConstantName = "FileName";
public const string RegularExpressionsOption = "RegularExpressions";
private readonly ToolOptions Options;
private readonly ToolOptions _options;
public FileNameTagInfoProvider(IOptions<ToolOptions> options)
{
Options = options.Value;
_options = options.Value;
}
public async ValueTask<MusicInfo> LoadAsync(string filePath)
{
await ValueTask.CompletedTask;
var regex = _options.TagInfoProviderOptions
.First(t => t.Name == ConstantName)
.Extensions[RegularExpressionsOption];
var match = Regex.Match(Path.GetFileNameWithoutExtension(filePath), Options.TagInfoProviderOptions.FileNameRegularExpressions);
var match = Regex.Match(Path.GetFileNameWithoutExtension(filePath), regex);
if (match.Groups.Count != 3)
{

View File

@ -7,11 +7,6 @@ namespace ZonyLrcTools.Cli.Infrastructure.Tag
/// </summary>
public interface ITagInfoProvider
{
/// <summary>
/// 解析时的优先级。
/// </summary>
int Priority { get; }
/// <summary>
/// 标签解析器的唯一标识。
/// </summary>

View File

@ -1,10 +1,13 @@
using System.Collections.Generic;
namespace ZonyLrcTools.Cli.Infrastructure.Tag
{
public class TagInfoProviderOptions
public class TagInfoProviderInstance
{
/// <summary>
/// 用于从文件名当中提取歌曲名、歌手名。
/// </summary>
public string FileNameRegularExpressions { get; set; }
public string Name { get; set; }
public int Priority { get; set; }
public Dictionary<string, string> Extensions { get; set; }
}
}

View File

@ -8,10 +8,8 @@ namespace ZonyLrcTools.Cli.Infrastructure.Tag
/// <summary>
/// 基于 TagLib 的标签信息解析器。
/// </summary>
public class TaglibTagInfoProvider : ITagInfoProvider, ITransientDependency
public class TaglibTagInfoProvider : ITagInfoProvider, ISingletonDependency
{
public int Priority => 1;
public string Name => ConstantName;
public const string ConstantName = "Taglib";

View File

@ -13,9 +13,19 @@
"ProxyIp": "127.0.0.1",
"ProxyPort": 4780
},
"TagInfoProviderOptions": {
"FileNameRegularExpressions": "(?'artist'.+)\\s-\\s(?'name'.+)"
},
"TagInfoProviderOptions": [
{
"Name": "Taglib",
"Priority": 1
},
{
"Name": "FileName",
"Priority": 2,
"Extensions": {
"RegularExpressions": "(?'artist'.+)\\s-\\s(?'name'.+)"
}
}
],
"LyricDownloaderOptions": [
{
"Name": "NetEase",