Skip to content

Commit

Permalink
Merge pull request #128 from jitwxs/dev
Browse files Browse the repository at this point in the history
Release v5.0
  • Loading branch information
jitwxs authored Oct 23, 2022
2 parents bd5d350 + 940e1aa commit 7c3131d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 84 deletions.
3 changes: 1 addition & 2 deletions MusicLyricApp/Api/IMusicApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public interface IMusicApiV2
/// 获取歌曲信息
/// </summary>
/// <param name="songIds">歌曲ID列表</param>
/// <param name="errorMsgDict">错误信息</param>
/// <returns></returns>
Dictionary<string, SongVo> GetSongVo(string[] songIds, out Dictionary<string, string> errorMsgDict);
Dictionary<string, ResultVo<SongVo>> GetSongVo(string[] songIds);

/// <summary>
/// 获取歌词信息
Expand Down
16 changes: 8 additions & 8 deletions MusicLyricApp/Api/MusicApiV2Cacheable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class MusicApiV2Cacheable : IMusicApiV2
{
protected abstract IEnumerable<string> GetSongIdsFromAlbum0(string albumId);

protected abstract Dictionary<string, SongVo> GetSongVo0(string[] songIds, out Dictionary<string, string> errorMsgDict);
protected abstract Dictionary<string, ResultVo<SongVo>> GetSongVo0(string[] songIds);

protected abstract LyricVo GetLyricVo0(string songId);

Expand All @@ -28,34 +28,34 @@ public IEnumerable<string> GetSongIdsFromAlbum(string albumId)
return result;
}

public Dictionary<string, SongVo> GetSongVo(string[] songIds, out Dictionary<string, string> errorMsgDict)
public Dictionary<string, ResultVo<SongVo>> GetSongVo(string[] songIds)
{
var result = new Dictionary<string, SongVo>();
var result = new Dictionary<string, ResultVo<SongVo>>();
var requestIds = new List<string>();

foreach (var songId in songIds)
{
if (GlobalCache.ContainsSong(songId))
{
result[songId] = GlobalCache.GetSong(songId);
result[songId] = new ResultVo<SongVo>(GlobalCache.GetSong(songId));
}
else
{
requestIds.Add(songId);
}
}

foreach(var pair in GetSongVo0(requestIds.ToArray(), out errorMsgDict))
foreach(var pair in GetSongVo0(requestIds.ToArray()))
{
var songId = pair.Key;
var resultVo = pair.Value;

if (errorMsgDict[songId] != ErrorMsg.SUCCESS)
if (resultVo.IsSuccess())
{
continue;
GlobalCache.PutSong(songId, resultVo.Data);
}

result[songId] = pair.Value;
GlobalCache.PutSong(songId, pair.Value);
}

return result;
Expand Down
35 changes: 17 additions & 18 deletions MusicLyricApp/Api/NetEaseMusicApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,34 @@ protected override IEnumerable<string> GetSongIdsFromAlbum0(string albumId)
throw new MusicLyricException(ErrorMsg.ALBUM_NOT_EXIST);
}

protected override Dictionary<string, SongVo> GetSongVo0(string[] songIds, out Dictionary<string, string> errorMsgDict)
protected override Dictionary<string, ResultVo<SongVo>> GetSongVo0(string[] songIds)
{
errorMsgDict = new Dictionary<string, string>();

var datumResp = _api.GetDatum(songIds);
var songResp = _api.GetSongs(songIds);
var result = new Dictionary<string, SongVo>();
var result = new Dictionary<string, ResultVo<SongVo>>();

foreach (var pair in datumResp)
{
var songId = pair.Key;
var datum = pair.Value;

if (!songResp.TryGetValue(songId, out var song))
if (songResp.TryGetValue(songId, out var song))
{
errorMsgDict[songId] = ErrorMsg.SONG_NOT_EXIST;
continue;
var datum = pair.Value;

result[songId] = new ResultVo<SongVo>(new SongVo
{
Links = datum.Url,
Pics = song.Al.PicUrl,
Name = song.Name,
Singer = ContractSinger(song.Ar),
Album = song.Al.Name,
Duration = song.Dt
});
}

result[songId] = new SongVo
else
{
Links = datum.Url,
Pics = song.Al.PicUrl,
Name = song.Name,
Singer = ContractSinger(song.Ar),
Album = song.Al.Name,
Duration = song.Dt
};
errorMsgDict[songId] = ErrorMsg.SUCCESS;
result[songId] = new ResultVo<SongVo>(null, ErrorMsg.SONG_NOT_EXIST);
}
}

return result;
Expand Down
15 changes: 6 additions & 9 deletions MusicLyricApp/Api/QQMusicApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,33 @@ protected override IEnumerable<string> GetSongIdsFromAlbum0(string albumId)
throw new MusicLyricException(ErrorMsg.ALBUM_NOT_EXIST);
}

protected override Dictionary<string, SongVo> GetSongVo0(string[] songIds, out Dictionary<string, string> errorMsgDict)
protected override Dictionary<string, ResultVo<SongVo>> GetSongVo0(string[] songIds)
{
errorMsgDict = new Dictionary<string, string>();

var result = new Dictionary<string, SongVo>();
var result = new Dictionary<string, ResultVo<SongVo>>();

foreach (var songId in songIds)
{
var resp = _api.GetSong(songId);

if (resp.Code != 0 || resp.Data.Length == 0)
{
errorMsgDict[songId] = ErrorMsg.SONG_NOT_EXIST;
result[songId] = new ResultVo<SongVo>(null, ErrorMsg.SONG_NOT_EXIST);
continue;
}

var song = resp.Data[0];

var links = _api.GetSongLink(songId);
var links = _api.GetSongLink(songId);

result[songId] = new SongVo
result[songId] = new ResultVo<SongVo>(new SongVo
{
Links = links,
Pics = BuildPicUrl(song.Album),
Name = song.Name,
Singer = ContractSinger(song.Singer),
Album = song.Album.Name,
Duration = song.Interval * 1000
};
errorMsgDict[songId] = ErrorMsg.SUCCESS;
});
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion MusicLyricApp/Bean/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MusicLyricApp.Bean
{
public static class Constants
{
public const string Version = "v5.0";
public const string Version = "v5.1";

public static readonly string SettingPath = Environment.CurrentDirectory + "\\MusicLyricAppSetting.json";

Expand Down
24 changes: 24 additions & 0 deletions MusicLyricApp/Bean/MusicLyricsVO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,30 @@ public class SearchInfo
public SettingBean SettingBean { get; set; }
}

public class ResultVo<T>
{
public T Data { get; }

public string ErrorMsg { get; }

public ResultVo(T data, string errorMsg)
{
Data = data;
ErrorMsg = errorMsg;
}

public ResultVo(T data)
{
Data = data;
ErrorMsg = Bean.ErrorMsg.SUCCESS;
}

public bool IsSuccess()
{
return ErrorMsg == Bean.ErrorMsg.SUCCESS;
}
}

public static class EnumHelper
{
public static string ToDescription(this Enum val)
Expand Down
12 changes: 9 additions & 3 deletions MusicLyricApp/Bean/QQMusicBean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ public class LyricResult

public LyricResult Decode()
{
Lyric = Encoding.UTF8.GetString(Convert.FromBase64String(Lyric));

Trans = Encoding.UTF8.GetString(Convert.FromBase64String(Trans));
if (Lyric != null)
{
Lyric = Encoding.UTF8.GetString(Convert.FromBase64String(Lyric));
}

if (Trans != null)
{
Trans = Encoding.UTF8.GetString(Convert.FromBase64String(Trans));
}

return this;
}
Expand Down
94 changes: 51 additions & 43 deletions MusicLyricApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ private void ReloadInputIdText()
/// <summary>
/// 根据歌曲ID查询
/// </summary>
private void SearchBySongId(IEnumerable<string> songIds, out Dictionary<string, string> errorMsgDict)
private Dictionary<string, string> SearchBySongId(IEnumerable<string> songIds)
{
errorMsgDict = new Dictionary<string, string>();
var errorMsgDict = new Dictionary<string, string>();

// 1、优先加载缓存
var requestId = new List<string>();
Expand All @@ -216,44 +216,52 @@ private void SearchBySongId(IEnumerable<string> songIds, out Dictionary<string,

if (requestId.Count == 0)
{
return;
return errorMsgDict;
}

// 2、API 请求
var songResp = _api.GetSongVo(requestId.ToArray(), out var songVoErrorMsg);
foreach (var errorPair in songVoErrorMsg)
var songResp = _api.GetSongVo(requestId.ToArray());
foreach (var pair in songResp)
{
var songId = errorPair.Key;
var errorMsg = errorPair.Value;
var songId = pair.Key;
var resultVo = pair.Value;

if (errorMsg != ErrorMsg.SUCCESS)
string msg;
if (resultVo.IsSuccess())
{
errorMsgDict.Add(songId, errorMsg);
continue;
}

try
{
var songVo = songResp[songId];
var lyricVo = _api.GetLyricVo(songId);
try
{
var songVo = resultVo.Data;
var lyricVo = _api.GetLyricVo(songId);

lyricVo.Duration = songVo.Duration;
lyricVo.Duration = songVo.Duration;

GlobalCache.PutSaveVo(songId, new SaveVo(songId, songVo, lyricVo));
}
catch (WebException ex)
{
_logger.Error(ex, "SearchBySongId network error, delay: {Delay}", NetworkUtils.GetWebRoundtripTime(50));
errorMsg = ErrorMsg.NETWORK_ERROR;
GlobalCache.PutSaveVo(songId, new SaveVo(songId, songVo, lyricVo));

msg = ErrorMsg.SUCCESS;
}
catch (WebException ex)
{
_logger.Error(ex, "SearchBySongId network error, delay: {Delay}", NetworkUtils.GetWebRoundtripTime(50));

msg = ErrorMsg.NETWORK_ERROR;
}
catch (System.Exception ex)
{
msg = ex.Message;

_logger.Error(ex, "SearchBySongId error, songId: {SongId}, message: {ErrorMsg}", songId, msg);
}
}
catch (System.Exception ex)
else
{
_logger.Error(ex, "SearchBySongId error, songId: {SongId}, message: {ErrorMsg}", songId, errorMsg);
errorMsg = ex.Message;
msg = resultVo.ErrorMsg;
}

errorMsgDict.Add(songId, errorMsg);
errorMsgDict.Add(songId, msg);
}

return errorMsgDict;
}

/// <summary>
Expand Down Expand Up @@ -297,27 +305,27 @@ private void InitInputSongIds()
/// <param name="songId">歌曲ID</param>
/// <param name="errorMsg">错误信息</param>
/// <exception cref="WebException"></exception>
private void SingleSearch(string songId, out string errorMsg)
private string SingleSearch(string songId)
{
SearchBySongId(new[] { songId }, out var resultMaps);
var resultMaps = SearchBySongId(new[] { songId });

var message = resultMaps[songId];
errorMsg = message;
if (message != ErrorMsg.SUCCESS)

if (message == ErrorMsg.SUCCESS)
{
return;
}
var result = GlobalCache.GetSaveVo(songId);

var result = GlobalCache.GetSaveVo(songId);
// 3、加入结果集
_globalSaveVoMap.Add(songId, result);

// 3、加入结果集
_globalSaveVoMap.Add(songId, result);
// 4、前端设置
SongName_TextBox.Text = result.SongVo.Name;
Singer_TextBox.Text = result.SongVo.Singer;
Album_TextBox.Text = result.SongVo.Album;
UpdateLrcTextBox(string.Empty);
}

// 4、前端设置
SongName_TextBox.Text = result.SongVo.Name;
Singer_TextBox.Text = result.SongVo.Singer;
Album_TextBox.Text = result.SongVo.Album;
UpdateLrcTextBox(string.Empty);
return message;
}

/// <summary>
Expand All @@ -327,7 +335,7 @@ private void SingleSearch(string songId, out string errorMsg)
/// <exception cref="WebException"></exception>
private void BatchSearch(IEnumerable<string> ids)
{
SearchBySongId(ids, out var resultMaps);
var resultMaps = SearchBySongId(ids);

// 输出日志
var log = new StringBuilder();
Expand Down Expand Up @@ -374,7 +382,7 @@ private async void Search_Btn_Click(object sender, EventArgs e)
// just loop once
foreach (var songId in songIds)
{
SingleSearch(songId, out var errorMsg);
var errorMsg = SingleSearch(songId);
if (errorMsg != ErrorMsg.SUCCESS)
{
MessageBox.Show(errorMsg, "提示");
Expand Down

0 comments on commit 7c3131d

Please sign in to comment.