Skip to content

Commit ed936fa

Browse files
authored
Merge pull request #267 from jitwxs/release/7_1
Release v7.1
2 parents 2882e59 + fcfed1b commit ed936fa

24 files changed

+568
-101
lines changed

cross-platform/MusicLyricApp/App.axaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Avalonia.Controls.ApplicationLifetimes;
44
using Avalonia.Data.Core.Plugins;
55
using Avalonia.Markup.Xaml;
6+
using Avalonia.Styling;
7+
using MusicLyricApp.Models;
68
using MusicLyricApp.Views;
79

810
namespace MusicLyricApp;
@@ -13,6 +15,17 @@ public override void Initialize()
1315
{
1416
AvaloniaXamlLoader.Load(this);
1517
}
18+
19+
public void SetTheme(ThemeModeEnum mode)
20+
{
21+
RequestedThemeVariant = mode switch
22+
{
23+
ThemeModeEnum.LIGHT => ThemeVariant.Light,
24+
ThemeModeEnum.DARK => ThemeVariant.Dark,
25+
ThemeModeEnum.FOLLOW_SYSTEM => ThemeVariant.Default,
26+
_ => RequestedThemeVariant
27+
};
28+
}
1629

1730
public override void OnFrameworkInitializationCompleted()
1831
{

cross-platform/MusicLyricApp/Core/Service/Music/NetEaseMusicApi.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,10 @@ protected override ResultVo<LyricVo> GetLyricVo0(string id, string displayId, bo
154154
{
155155
vo.Lyric = resp.Lrc.Lyric;
156156
}
157-
158157
if (resp.Tlyric != null)
159158
{
160159
vo.TranslateLyric = resp.Tlyric.Lyric;
161160
}
162-
163161
if (resp.Romalrc != null)
164162
{
165163
vo.TransliterationLyric = resp.Romalrc.Lyric;

cross-platform/MusicLyricApp/Core/Service/Music/NetEaseMusicNativeApi.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Numerics;
77
using System.Security.Cryptography;
88
using System.Text;
9+
using System.Threading;
910
using MusicLyricApp.Core.Utils;
1011
using MusicLyricApp.Models;
1112
using Newtonsoft.Json;
@@ -261,31 +262,43 @@ private SongUrls GetSongsUrl(string[] songId, long bitrate = 999000)
261262
/// <summary>
262263
/// 批量获得歌曲详情
263264
/// </summary>
264-
/// <param name="songIds">歌曲ID</param>
265+
/// <param name="inputSongIds">歌曲ID</param>
265266
/// <exception cref="WebException"></exception>
266267
/// <returns></returns>
267-
private DetailResult GetDetail(IEnumerable<string> songIds)
268+
private DetailResult GetDetail(IEnumerable<string> inputSongIds)
268269
{
269270
const string url = "https://music.163.com/weapi/v3/song/detail?csrf_token=";
270271

271-
var songRequests = new StringBuilder();
272-
foreach (var songId in songIds)
273-
{
274-
songRequests.Append("{'id':'").Append(songId).Append("'}").Append(',');
275-
}
276-
277-
var data = new Dictionary<string, string>
272+
var allResults = new List<Song>();
273+
var cnt = 1;
274+
275+
foreach (var songIds in GlobalUtils.Batch(inputSongIds, Constants.BatchQuerySize))
278276
{
277+
var songs = songIds.Select(id => new { id });
278+
var data = new Dictionary<string, string>
279279
{
280-
"c",
281-
"[" + songRequests.Remove(songRequests.Length - 1, 1) + "]"
282-
},
283-
{ "csrf_token", string.Empty },
284-
};
280+
{ "c", JsonConvert.SerializeObject(songs) },
281+
{ "csrf_token", string.Empty }
282+
};
285283

286-
var raw = SendPost(url, Prepare(JsonConvert.SerializeObject(data)));
284+
var raw = SendPost(url, Prepare(JsonConvert.SerializeObject(data)));
285+
var partialResult = JsonConvert.DeserializeObject<DetailResult>(raw);
286+
if (partialResult?.Code == 200)
287+
{
288+
allResults.AddRange(partialResult.Songs);
289+
}
287290

288-
return JsonConvert.DeserializeObject<DetailResult>(raw);
291+
if (cnt++ % 2 == 0)
292+
{
293+
Thread.Sleep(Constants.SleepMsBetweenBatchQuery); // sleep 500ms after every two batches
294+
}
295+
}
296+
297+
return new DetailResult
298+
{
299+
Code = 200,
300+
Songs = allResults.ToArray()
301+
};
289302
}
290303

291304
private Dictionary<string, string> Prepare(string raw)

cross-platform/MusicLyricApp/Core/Service/Music/QQMusicNativeApi.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Xml;
55
using MusicLyricApp.Core.Utils;
66
using MusicLyricApp.Models;
7+
using NLog;
78

89
namespace MusicLyricApp.Core.Service.Music;
910

@@ -16,6 +17,8 @@ public class QQMusicNativeApi(Func<string> cookieFunc) : BaseNativeApi(cookieFun
1617
{ "contentroma", "roma" }, // 罗马音
1718
{ "Lyric_1", "lyric" }, // 解压后的内容
1819
};
20+
21+
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
1922

2023
protected override string HttpRefer()
2124
{
@@ -164,8 +167,9 @@ public QQMusicBean.LyricResult GetLyric(string songId)
164167
{
165168
decompressText = Decrypter.DecryptLyrics(text) ?? "";
166169
}
167-
catch
170+
catch(Exception ex)
168171
{
172+
_logger.Error(ex, "QQMusicNativeApi GetLyric DecryptLyrics failed, songId: {SongId}", songId);
169173
continue;
170174
}
171175

cross-platform/MusicLyricApp/Core/Service/StorageService.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77
using MusicLyricApp.Core.Utils;
88
using MusicLyricApp.Models;
99
using MusicLyricApp.ViewModels;
10+
using NLog;
1011

1112
namespace MusicLyricApp.Core.Service;
1213

1314
public class StorageService : IStorageService
1415
{
1516
private ISearchService _searchService;
1617

18+
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
19+
1720
public void SetSearchService(ISearchService searchService)
1821
{
1922
_searchService = searchService;
2023
}
2124

2225
public SettingBean ReadAppConfig()
2326
{
24-
if (File.Exists(Constants.SettingPath))
27+
if (File.Exists(Constants.GetConfigFilePath()))
2528
{
26-
var text = File.ReadAllText(Constants.SettingPath);
29+
var text = File.ReadAllText(Constants.GetConfigFilePath());
2730
return text.ToEntity<SettingBean>();
2831
}
2932
else
@@ -34,7 +37,9 @@ public SettingBean ReadAppConfig()
3437

3538
public void SaveConfig(SettingBean settingBean)
3639
{
37-
File.WriteAllText(Constants.SettingPath, settingBean.ToJson(), Encoding.UTF8);
40+
var path = Constants.GetConfigFilePath();
41+
File.WriteAllText(path, settingBean.ToJson(), Encoding.UTF8);
42+
_logger.Info("Save config into {Path}", path);
3843
}
3944

4045
public Task<string> SaveResult(SearchResultViewModel searchResult, SettingBean settingBean,
@@ -204,7 +209,7 @@ private static async Task<IStorageFolder> SelectFolder(IWindowProvider windowPro
204209

205210
private static async Task WriteToFile(IStorageFolder folder, SaveVo saveVo, SettingBean settingBean)
206211
{
207-
var extension = settingBean.Param.OutputFileFormat.ToDescription();
212+
var extension = settingBean.Param.OutputFileFormat.ToDescription().ToLower();
208213
var encoding = GlobalUtils.GetEncoding(settingBean.Param.Encoding);
209214
var filename = GlobalUtils.GetOutputName(saveVo, settingBean.Config.OutputFileNameFormat,
210215
settingBean.Config.SingerSeparator);

cross-platform/MusicLyricApp/Core/Utils/GlobalUtils.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private static string ResolveCustomFunction(string content)
230230
}
231231
catch (Exception e)
232232
{
233-
Logger.Error("ResolveCustomFunction error, content: " + sourceContent + ", stack: " + e);
233+
Logger.Error(e, "ResolveCustomFunction error, content: " + sourceContent);
234234
return sourceContent;
235235
}
236236
}
@@ -329,6 +329,35 @@ public static string MergeStr(IEnumerable<string> strList)
329329
{
330330
return string.Join(Environment.NewLine, strList);
331331
}
332+
333+
/// <summary>
334+
/// 将序列拆分成指定大小的批次。
335+
/// </summary>
336+
/// <typeparam name="T">元素类型</typeparam>
337+
/// <param name="source">要拆分的源序列</param>
338+
/// <param name="size">每批的大小(必须 > 0)</param>
339+
/// <returns>批次列表,每批为一个 List&lt;T&gt;</returns>
340+
public static IEnumerable<List<T>> Batch<T>(IEnumerable<T> source, int size)
341+
{
342+
if (source == null) throw new ArgumentNullException(nameof(source));
343+
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size), "Batch size must be greater than 0.");
344+
345+
var batch = new List<T>(size);
346+
foreach (var item in source)
347+
{
348+
batch.Add(item);
349+
if (batch.Count == size)
350+
{
351+
yield return batch;
352+
batch = new List<T>(size);
353+
}
354+
}
355+
356+
if (batch.Count > 0)
357+
{
358+
yield return batch;
359+
}
360+
}
332361

333362
private static string ControlLength(string str)
334363
{

cross-platform/MusicLyricApp/Core/Utils/LyricUtils.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using MusicLyricApp.Core.Service.Translate;
1010
using MusicLyricApp.Models;
1111
using NTextCat;
12+
using ToolGood.Words;
1213

1314
namespace MusicLyricApp.Core.Utils;
1415

@@ -23,7 +24,7 @@ public static class LyricUtils
2324

2425
public static readonly Regex VerbatimLegalPrefixRegex = new(@"\[\d+,\d+\]");
2526

26-
public static readonly Regex CommonLegalPrefixRegex = new(@"\[\d+:\d+.\d+\]");
27+
public static readonly Regex CommonLegalPrefixRegex = new(@"\[\d+:\d+(?:\.\d+)?\]");
2728

2829
private const PinyinFormat PinyinDefineFormat = PinyinFormat.WITH_TONE_MARK | PinyinFormat.LOWERCASE | PinyinFormat.WITH_U_UNICODE;
2930

@@ -52,14 +53,24 @@ public static async Task<List<string>> GetOutputContent(LyricVo lyricVo, Setting
5253

5354
foreach (var voList in voListList)
5455
{
56+
string line;
5557
if (param.OutputFileFormat == OutputFormatEnum.SRT)
5658
{
57-
res.Add(SrtUtils.LrcToSrt(voList, timestampFormat, dotType, lyricVo.Duration));
59+
line = SrtUtils.LrcToSrt(voList, timestampFormat, dotType, lyricVo.Duration);
5860
}
5961
else
6062
{
61-
res.Add(string.Join(Environment.NewLine, from o in voList select o.Print(timestampFormat, dotType)));
63+
line = string.Join(Environment.NewLine, from o in voList select o.Print(timestampFormat, dotType));
6264
}
65+
66+
line = config.ChineseProcessRule switch
67+
{
68+
ChineseProcessRuleEnum.SIMPLIFIED_CHINESE => WordsHelper.ToSimplifiedChinese(line),
69+
ChineseProcessRuleEnum.TRADITIONAL_CHINESE => WordsHelper.ToTraditionalChinese(line),
70+
_ => line
71+
};
72+
73+
res.Add(line);
6374
}
6475

6576
return res;
@@ -324,11 +335,13 @@ private static async Task<List<List<LyricLineVo>>> FormatLyric(LyricVo lyricVo,
324335

325336
private static string[] SplitLrc(string lrc)
326337
{
327-
// 换行符统一
328338
return (lrc ?? "")
329339
.Replace("\r\n", "\n")
330340
.Replace("\r", "")
331-
.Split('\n');
341+
.Split('\n')
342+
.Select(line => line.Trim()) // 去除行首尾空白
343+
.Where(line => !string.IsNullOrEmpty(line)) // 过滤空行
344+
.ToArray();
332345
}
333346

334347
/**

0 commit comments

Comments
 (0)