feat: Support skip exist lyric files.

This commit is contained in:
real-zony 2022-04-25 12:02:55 +08:00
parent 2044a0b8fa
commit 61ca863770
3 changed files with 40 additions and 9 deletions

View File

@ -92,11 +92,34 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
throw new ErrorCodeException(ErrorCodes.NoFilesWereScanned);
}
files = RemoveExistLyricFiles(files);
_logger.LogInformation($"已经扫描到了 {files.Count} 个音乐文件。");
return files;
}
private List<string> RemoveExistLyricFiles(List<string> filePaths)
{
if (!_options.Provider.Lyric.Config.IsSkipExistLyricFiles)
{
return filePaths;
}
return filePaths
.Where(path =>
{
if (!File.Exists(Path.ChangeExtension(path, ".lrc")))
{
return true;
}
_logger.LogWarning($"已经存在歌词文件 {path},跳过。");
return false;
})
.ToList();
}
private async Task<ImmutableList<MusicInfo>> LoadMusicInfoAsync(IReadOnlyCollection<string> files)
{
_logger.LogInformation("开始加载音乐文件的标签信息...");
@ -125,7 +148,7 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
return downloader;
}
#region > <
#region > Lyric download logic <
private async ValueTask DownloadLyricFilesAsync(ImmutableList<MusicInfo> musicInfos)
{
@ -150,10 +173,12 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
try
{
var lyric = await downloader.DownloadAsync(info.Name, info.Artist);
var filePath = Path.Combine(Path.GetDirectoryName(info.FilePath)!, $"{Path.GetFileNameWithoutExtension(info.FilePath)}.lrc");
if (File.Exists(filePath))
var lyricFilePath = Path.Combine(Path.GetDirectoryName(info.FilePath)!,
$"{Path.GetFileNameWithoutExtension(info.FilePath)}.lrc");
if (File.Exists(lyricFilePath))
{
return true;
File.Delete(lyricFilePath);
}
if (lyric.IsPruneMusic)
@ -161,7 +186,7 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
return true;
}
await using var stream = new FileStream(filePath, FileMode.Create);
await using var stream = new FileStream(lyricFilePath, FileMode.Create);
await using var sw = new StreamWriter(stream);
await sw.WriteAsync(lyric.ToString());
await sw.FlushAsync();
@ -195,7 +220,7 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
#endregion
#region > <
#region > Ablum image download logic <
private async ValueTask DownloadAlbumAsync(ImmutableList<MusicInfo> musicInfos)
{

View File

@ -25,5 +25,10 @@ public class LyricConfigOption
/// <summary>
/// 是否启用歌词翻译功能。
/// </summary>
public bool IsEnableTranslation { get; set; } = false;
public bool IsEnableTranslation { get; set; } = true;
/// <summary>
/// 如果歌词文件已经存在,是否跳过这些文件
/// </summary>
public bool IsSkipExistLyricFiles { get; set; } = false;
}

View File

@ -34,7 +34,7 @@ globalOption:
# 支持的歌词下载器。
plugin:
- name: NetEase # 基于网易云音乐的歌词下载器。
priority: 1 # 优先级,升序排列
priority: 1 # 优先级,升序排列,改为 -1 时禁用
- name: QQ # 基于 QQ 音乐的歌词下载器。
priority: 2
- name: KuGou # 基于酷狗音乐的歌词下载器。
@ -43,4 +43,5 @@ globalOption:
config:
isOneLine: true # 双语歌词是否合并为一行展示。
lineBreak: "\n" # 换行符的类型,记得使用双引号指定。
isEnableTranslation: true # 是否启用翻译歌词。
isEnableTranslation: true # 是否启用翻译歌词。
isSkipExistLyricFiles: false # 如果歌词文件已经存在,是否跳过这些文件。