Skip to content

Commit

Permalink
[Add] Netease Eapi Search for abroad IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
WXRIW committed Aug 3, 2024
1 parent f84a649 commit b69e0e5
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Lyricify.Lyrics.Helper/Decrypter/Qrc/Decrypter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Decrypter
}

Span<byte> unzip = SharpZipLibDecompress(data);

// 移除字符串头部的 BOM 标识 (如果有)
var utf8Bom = Encoding.UTF8.GetPreamble();
if (unzip[..utf8Bom.Length].SequenceEqual(utf8Bom))
Expand Down
2 changes: 1 addition & 1 deletion Lyricify.Lyrics.Helper/Providers/Web/BaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class BaseApi

protected abstract string? HttpRefer { get; }

protected abstract Dictionary<string,string>? AdditionalHeaders { get; }
protected abstract Dictionary<string, string>? AdditionalHeaders { get; }

protected async Task<string> GetAsync(string url)
{
Expand Down
1 change: 0 additions & 1 deletion Lyricify.Lyrics.Helper/Providers/Web/MusixMatch/Api.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using static Lyricify.Lyrics.Providers.Web.Musixmatch.GetTrackResponse;

namespace Lyricify.Lyrics.Providers.Web.Musixmatch
{
Expand Down
43 changes: 42 additions & 1 deletion Lyricify.Lyrics.Helper/Providers/Web/Netease/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,49 @@ public enum SearchTypeEnum
return JsonConvert.DeserializeObject<SearchResult>(res);
}

public async Task<SearchResult?> SearchNew(string keyword)
{
const string url = "https://interface.music.163.com/eapi/cloudsearch/pc";

var data = new Dictionary<string, string>
{
{ "s", keyword },
{ "type", "1" },
{ "limit", "30" },
{ "offset", "0" },
{ "total", "true" }
};

var raw = await EapiHelper.PostAsync(url, HttpClient, data);

var eapiResult = JsonConvert.DeserializeObject<EapiSearchResult>(raw);
if (eapiResult is null) return null;

var result = new SearchResult();
result.Code = eapiResult.Code;
result.NeedLogin = eapiResult.NeedLogin;
result.Result = eapiResult.Result;
var list = new List<Song>();
foreach (var song in eapiResult.Result.Songs)
{
list.Add(new()
{
Album = song.Album,
Alias = song.Alias,
Artists = song.Artists,
Duration = song.Duration,
Id = song.Id,
Name = song.Name,
Privilege = song.Privilege,
PublishTime = song.PublishTime
});
}
result.Result.Songs = list.ToArray();
return result;
}

/// <summary>
///
///
/// </summary>
/// <param name="songId"></param>
/// <param name="bitrate"></param>
Expand Down
1 change: 1 addition & 0 deletions Lyricify.Lyrics.Helper/Providers/Web/Netease/EapiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private static ulong GetCurrentTotalMilliseconds()
public static Dictionary<string, string> EApi(string url, object @object)
{
url = url.Replace("https://interface3.music.163.com/e", "/");
url = url.Replace("https://interface.music.163.com/e", "/");
string text = JsonConvert.SerializeObject(@object);
string message = $"nobody{url}use{text}md5forencrypt";
string digest = message.ToByteArrayUtf8().ComputeMd5().ToHexStringLower();
Expand Down
65 changes: 53 additions & 12 deletions Lyricify.Lyrics.Helper/Providers/Web/Netease/Response.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable disable
using Newtonsoft.Json;

namespace Lyricify.Lyrics.Providers.Web.Netease
{
/// <summary>
Expand All @@ -11,27 +13,44 @@ public class SearchResult
public SearchResultData Result { get; set; }

public long Code { get; set; }
}

public class SearchResultData
{
/* SearchType = SONG */
public class SearchResultData
{
/* SearchType = SONG */

public Song[] Songs { get; set; }

public Song[] Songs { get; set; }
public long SongCount { get; set; }

public long SongCount { get; set; }
/* SearchType = ALBUM */

/* SearchType = ALBUM */
public Album[] Albums { get; set; }

public Album[] Albums { get; set; }
public long AlbumCount { get; set; }

public long AlbumCount { get; set; }
/* SearchType = PLAYLIST */

/* SearchType = PLAYLIST */
public SimplePlaylist[] Playlists { get; set; }

public SimplePlaylist[] Playlists { get; set; }
public long PlaylistCount { get; set; }
}

public class EapiSearchResultData : SearchResultData
{
public new EapiSong[] Songs { get; set; }
}

public long PlaylistCount { get; set; }
}
/// <summary>
/// 搜索接口结果
/// </summary>
public class EapiSearchResult
{
public bool NeedLogin { get; set; }

public EapiSearchResultData Result { get; set; }

public long Code { get; set; }
}

public class SongUrls
Expand Down Expand Up @@ -264,6 +283,28 @@ public class Song
public Privilege Privilege { get; set; }
}

public class EapiSong
{
public string Name { get; set; }
public string Id { get; set; }
[JsonProperty("ar")]
public List<Ar> Artists { get; set; }
[JsonProperty("alia")]
public List<object> Alias { get; set; }
[JsonProperty("al")]
public Al Album { get; set; }
/// <summary>
/// 时长,单位ms
/// </summary>
[JsonProperty("dt")]
public long Duration { get; set; }
/// <summary>
/// 时间戳,eg 1657900800000
/// </summary>
public long PublishTime { get; set; }
public Privilege Privilege { get; set; }
}

public class Info
{
public CommentThread CommentThread { get; set; }
Expand Down
26 changes: 25 additions & 1 deletion Lyricify.Lyrics.Helper/Searchers/NeteaseSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,37 @@ public class NeteaseSearcher : Searcher, ISearcher

public override string DisplayName => "Netease Cloud Music";

private bool useNewSearchFirst = false;

public override async Task<List<ISearchResult>?> SearchForResults(string searchString)
{
var search = new List<ISearchResult>();

SearchResult? result = null;
if (useNewSearchFirst)
{
try { result = await Providers.Web.Providers.NeteaseApi.SearchNew(searchString); }
catch
{
useNewSearchFirst = !useNewSearchFirst;
try { result = await Providers.Web.Providers.NeteaseApi.Search(searchString, Api.SearchTypeEnum.SONG_ID); }
catch { }
}
}
else
{
try { result = await Providers.Web.Providers.NeteaseApi.Search(searchString, Api.SearchTypeEnum.SONG_ID); }
catch
{
useNewSearchFirst = !useNewSearchFirst;
// 尝试新接口,可以在外网使用
try { result = await Providers.Web.Providers.NeteaseApi.SearchNew(searchString); }
catch { }
}
}

try
{
var result = await Providers.Web.Providers.NeteaseApi.Search(searchString, Api.SearchTypeEnum.SONG_ID);
var results = result?.Result.Songs;
if (results == null) return null;
foreach (var track in results)
Expand Down

0 comments on commit b69e0e5

Please sign in to comment.