mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2025-07-01 20:30:41 +00:00
feat: 完成屏蔽词功能。
解决问题(#77)(https://github.com/real-zony/ZonyLrcToolsX/issues/77)
This commit is contained in:
parent
a7130fd478
commit
0c889272dc
3
src/ZonyLrcTools.Cli/BlockWords.json
Normal file
3
src/ZonyLrcTools.Cli/BlockWords.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"fuckking": "***kking"
|
||||
}
|
@ -32,5 +32,10 @@ namespace ZonyLrcTools.Cli.Config
|
||||
/// 歌词下载器相关的配置属性。
|
||||
/// </summary>
|
||||
public IEnumerable<LyricDownloaderOption> LyricDownloaderOptions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 屏蔽词功能相关配置。
|
||||
/// </summary>
|
||||
public BlockWordOption BlockWordOptions { get; set; }
|
||||
}
|
||||
}
|
@ -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<Dictionary<string, string>> _wordsDictionary;
|
||||
|
||||
public BlockWordDictionary(IOptions<ToolOptions> options)
|
||||
{
|
||||
_options = options.Value;
|
||||
|
||||
_wordsDictionary = new Lazy<Dictionary<string, string>>(() =>
|
||||
{
|
||||
var jsonData = File.ReadAllText(_options.BlockWordOptions.BlockWordDictionaryFile);
|
||||
return JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonData);
|
||||
});
|
||||
}
|
||||
|
||||
public string GetValue(string key)
|
||||
{
|
||||
if (_wordsDictionary.Value.TryGetValue(key, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
15
src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordOption.cs
Normal file
15
src/ZonyLrcTools.Cli/Infrastructure/Tag/BlockWordOption.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace ZonyLrcTools.Cli.Infrastructure.Tag
|
||||
{
|
||||
public class BlockWordOption
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否启用本功能。
|
||||
/// </summary>
|
||||
public bool IsEnable { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 屏蔽词字典文件,用于替换歌曲名或者歌手名称。
|
||||
/// </summary>
|
||||
public string BlockWordDictionaryFile { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace ZonyLrcTools.Cli.Infrastructure.Tag
|
||||
{
|
||||
public interface IBlockWordDictionary
|
||||
{
|
||||
string GetValue(string key);
|
||||
}
|
||||
}
|
@ -30,6 +30,10 @@
|
||||
<Content Include="Resources\error_msg.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Remove="BlockWords.json" />
|
||||
<Content Include="BlockWords.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -33,6 +33,10 @@
|
||||
"LyricOption": {
|
||||
"IsOneLine": true,
|
||||
"LineBreak": "\n"
|
||||
},
|
||||
"BlockWordOptions": {
|
||||
"IsEnable": false,
|
||||
"BlockWordDictionaryFile": "BlockWords.json"
|
||||
}
|
||||
}
|
||||
}
|
@ -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<IBlockWordDictionary>();
|
||||
var result = dictionary.GetValue("fuckking");
|
||||
|
||||
result.ShouldBe("***kking");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user