Skip to content

Commit

Permalink
optimized net-ease search logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jitwxs committed Feb 19, 2024
1 parent 16a317d commit 294472e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
17 changes: 6 additions & 11 deletions MusicLyricApp/Api/Music/NetEaseMusicApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,15 @@ protected override ResultVo<LyricVo> GetLyricVo0(string id, string displayId, bo

protected override ResultVo<SearchResultVo> Search0(string keyword, SearchTypeEnum searchType)
{
var resp = _api.Search(keyword, searchType, out var code);
var resp = _api.Search(keyword, searchType);

if (code == "50000005")
if (resp.IsSuccess())
{
return ResultVo<SearchResultVo>.Failure(ErrorMsg.NEED_LOGIN);
return new ResultVo<SearchResultVo>(resp.Data.Convert(searchType));
}

if (code != "200")
{
_logger.Error("NetEaseMusicApiV2 Search0 failed, code: {Code}, resp: {Resp}", code, resp.ToJson());
return ResultVo<SearchResultVo>.Failure(ErrorMsg.NETWORK_ERROR);
}

return new ResultVo<SearchResultVo>(resp.Convert(searchType));

_logger.Error("NetEaseMusicApiV2 Search0 failed, resp: {Resp}", resp.ToJson());
return ResultVo<SearchResultVo>.Failure(resp.ErrorMsg);
}
}
}
23 changes: 17 additions & 6 deletions MusicLyricApp/Api/Music/NetEaseMusicNativeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override string HttpRefer()
return "https://music.163.com/";
}

public SearchResult Search(string keyword, SearchTypeEnum searchType, out string code)
public ResultVo<SearchResult> Search(string keyword, SearchTypeEnum searchType)
{
const string url = "https://music.163.com/weapi/cloudsearch/get/web";

Expand Down Expand Up @@ -71,21 +71,32 @@ public SearchResult Search(string keyword, SearchTypeEnum searchType, out string
var res = SendPost(url, Prepare(JsonConvert.SerializeObject(data)));

var obj = (JObject)JsonConvert.DeserializeObject(res);
if (obj == null)
{
return ResultVo<SearchResult>.Failure(ErrorMsg.SONG_NOT_EXIST);
}

var code = obj["code"].ToString();
var result = obj["result"];

if (code == "50000005")
{
return ResultVo<SearchResult>.Failure(ErrorMsg.NEED_LOGIN);
}

code = obj["code"].ToString();
if (code != "200")
if (result == null || code != "200")
{
return null;
return ResultVo<SearchResult>.Failure(ErrorMsg.SONG_NOT_EXIST);
}

var resultStr = obj["result"].ToString();
var resultStr = result.ToString();

if (obj["abroad"] != null && bool.Parse(obj["abroad"].ToString()))
{
resultStr = NetEaseMusicSearchUtils.Decode(resultStr);
}

return JsonConvert.DeserializeObject<SearchResult>(resultStr);
return new ResultVo<SearchResult>(JsonConvert.DeserializeObject<SearchResult>(resultStr));
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions MusicLyricAppTest/Api/Music/NetEaseMusicNativeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public void TestGetAlbum()
[Test]
public void TestSearch()
{
var res = _api.Search("慰问", SearchTypeEnum.PLAYLIST_ID, out var code);
var res = _api.Search("慰问", SearchTypeEnum.PLAYLIST_ID);

Assert.AreEqual("50000005", code);
Assert.IsNull(res);
Assert.AreEqual(ErrorMsg.NEED_LOGIN, res.ErrorMsg);
Assert.IsNull(res.Data);
}

[Test]
Expand Down

0 comments on commit 294472e

Please sign in to comment.