diff --git a/src/ZonyLrcTools.Cli/BlockWords.json b/src/ZonyLrcTools.Cli/BlockWords.json new file mode 100644 index 0000000..cd4d756 --- /dev/null +++ b/src/ZonyLrcTools.Cli/BlockWords.json @@ -0,0 +1,3 @@ +{ + "fuckking": "***kking" +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Config/ToolOptions.cs b/src/ZonyLrcTools.Cli/Config/ToolOptions.cs index 1ad955c..b256f97 100644 --- a/src/ZonyLrcTools.Cli/Config/ToolOptions.cs +++ b/src/ZonyLrcTools.Cli/Config/ToolOptions.cs @@ -32,5 +32,10 @@ namespace ZonyLrcTools.Cli.Config /// 歌词下载器相关的配置属性。 /// public IEnumerable LyricDownloaderOptions { get; set; } + + /// + /// 屏蔽词功能相关配置。 + /// + public BlockWordOption BlockWordOptions { get; set; } } } \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordDictionary.cs b/src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordDictionary.cs new file mode 100644 index 0000000..53736b5 --- /dev/null +++ b/src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordDictionary.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using ZonyLrcTools.Cli.Config; +using ZonyLrcTools.Cli.Infrastructure.DependencyInject; + +namespace ZonyLrcTools.Cli.Infrastructure.Tag +{ + public class BlockWordDictionary : IBlockWordDictionary, ISingletonDependency + { + private readonly ToolOptions _options; + + private readonly Lazy> _wordsDictionary; + + public BlockWordDictionary(IOptions options) + { + _options = options.Value; + + _wordsDictionary = new Lazy>(() => + { + var jsonData = File.ReadAllText(_options.BlockWordOptions.BlockWordDictionaryFile); + return JsonConvert.DeserializeObject>(jsonData); + }); + } + + public string GetValue(string key) + { + if (_wordsDictionary.Value.TryGetValue(key, out var value)) + { + return value; + } + + return null; + } + } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordOption.cs b/src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordOption.cs new file mode 100644 index 0000000..51f40af --- /dev/null +++ b/src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordOption.cs @@ -0,0 +1,15 @@ +namespace ZonyLrcTools.Cli.Infrastructure.Tag +{ + public class BlockWordOption + { + /// + /// 是否启用本功能。 + /// + public bool IsEnable { get; set; } + + /// + /// 屏蔽词字典文件,用于替换歌曲名或者歌手名称。 + /// + public string BlockWordDictionaryFile { get; set; } + } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Tag/IBlockWordDictionary.cs b/src/ZonyLrcTools.Cli/Infrastructure/Tag/IBlockWordDictionary.cs new file mode 100644 index 0000000..6e7960a --- /dev/null +++ b/src/ZonyLrcTools.Cli/Infrastructure/Tag/IBlockWordDictionary.cs @@ -0,0 +1,7 @@ +namespace ZonyLrcTools.Cli.Infrastructure.Tag +{ + public interface IBlockWordDictionary + { + string GetValue(string key); + } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj index 8788abd..bb3436d 100644 --- a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj +++ b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj @@ -30,6 +30,10 @@ Always + + + Always + diff --git a/src/ZonyLrcTools.Cli/appsettings.json b/src/ZonyLrcTools.Cli/appsettings.json index b32a9a0..0f38432 100644 --- a/src/ZonyLrcTools.Cli/appsettings.json +++ b/src/ZonyLrcTools.Cli/appsettings.json @@ -33,6 +33,10 @@ "LyricOption": { "IsOneLine": true, "LineBreak": "\n" + }, + "BlockWordOptions": { + "IsEnable": false, + "BlockWordDictionaryFile": "BlockWords.json" } } } \ No newline at end of file diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Tag/BlockWordDictionaryTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Tag/BlockWordDictionaryTests.cs new file mode 100644 index 0000000..1065fae --- /dev/null +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Tag/BlockWordDictionaryTests.cs @@ -0,0 +1,18 @@ +using Shouldly; +using Xunit; +using ZonyLrcTools.Cli.Infrastructure.Tag; + +namespace ZonyLrcTools.Tests.Infrastructure.Tag +{ + public class BlockWordDictionaryTests : TestBase + { + [Fact] + public void GetValue_Test() + { + var dictionary = GetService(); + var result = dictionary.GetValue("fuckking"); + + result.ShouldBe("***kking"); + } + } +} \ No newline at end of file