Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jitwxs/163MusicLyrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jitwxs committed Dec 24, 2022
2 parents 3e14375 + dcf3b30 commit 10f9624
Show file tree
Hide file tree
Showing 22 changed files with 884 additions and 324 deletions.
28 changes: 24 additions & 4 deletions MusicLyricApp/Api/BaseNativeApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Text;
Expand All @@ -11,9 +12,16 @@ public abstract class BaseNativeApi
public const string Useragent =
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";

public const string Cookie =
public const string DefaultCookie =
"os=pc;osver=Microsoft-Windows-10-Professional-build-16299.125-64bit;appver=2.0.3.131777;channel=netease;__remember_me=true";

private Func<string> _cookieFunc;

protected BaseNativeApi(Func<string> cookieFunc)
{
_cookieFunc = cookieFunc;
}

protected abstract string HttpRefer();

/// <summary>
Expand All @@ -29,10 +37,16 @@ protected string SendPost(string url, Dictionary<string, string> paramDict)
string result;
using (var wc = new WebClient())
{
var cookie = _cookieFunc.Invoke();
if (string.IsNullOrWhiteSpace(cookie))
{
cookie = DefaultCookie;
}

wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
wc.Headers.Add(HttpRequestHeader.Referer, HttpRefer());
wc.Headers.Add(HttpRequestHeader.UserAgent, Useragent);
wc.Headers.Add(HttpRequestHeader.Cookie, Cookie);
wc.Headers.Add(HttpRequestHeader.Cookie, cookie);

var request = new NameValueCollection();
foreach (var keyPair in @paramDict)
Expand All @@ -51,11 +65,17 @@ protected string SendJsonPost(string url, Dictionary<string, object> paramDict)
{
using (var wc = new WebClient())
{
var cookie = _cookieFunc.Invoke();
if (string.IsNullOrWhiteSpace(cookie))
{
cookie = DefaultCookie;
}

wc.Encoding = Encoding.UTF8;
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
wc.Headers.Add(HttpRequestHeader.Referer, HttpRefer());
wc.Headers.Add(HttpRequestHeader.UserAgent, Useragent);
wc.Headers.Add(HttpRequestHeader.Cookie, Cookie);
wc.Headers.Add(HttpRequestHeader.Cookie, cookie);

return wc.UploadString(url, paramDict.ToJson());
}
Expand Down
15 changes: 10 additions & 5 deletions MusicLyricApp/Api/NetEaseMusicApiV2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using MusicLyricApp.Bean;
using MusicLyricApp.Cache;
Expand All @@ -13,9 +14,9 @@ public class NetEaseMusicApiV2 : MusicApiV2Cacheable

private readonly NetEaseMusicNativeApi _api;

public NetEaseMusicApiV2()
public NetEaseMusicApiV2(Func<string> cookieFunc)
{
_api = new NetEaseMusicNativeApi();
_api = new NetEaseMusicNativeApi(cookieFunc);
}

protected override SearchSourceEnum Source0()
Expand All @@ -33,6 +34,10 @@ protected override ResultVo<PlaylistVo> GetPlaylistVo0(string playlistId)
GlobalCache.DoCache(CacheType.NET_EASE_SONG, value => value.Id, resp.Playlist.Tracks);

return new ResultVo<PlaylistVo>(resp.Convert());
}
else if (resp.Code == 20001)
{
return ResultVo<PlaylistVo>.Failure(ErrorMsg.NEED_LOGIN);
}
else
{
Expand Down Expand Up @@ -141,13 +146,13 @@ protected override ResultVo<SearchResultVo> Search0(string keyword, SearchTypeEn
{
var resp = _api.Search(keyword, searchType);

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

return new ResultVo<SearchResultVo>(resp.Result.Convert(searchType));
return new ResultVo<SearchResultVo>(resp.Convert(searchType));
}
}
}
20 changes: 18 additions & 2 deletions MusicLyricApp/Api/NetEaseMusicNativeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.Text;
using MusicLyricApp.Bean;
using MusicLyricApp.Exception;
using MusicLyricApp.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MusicLyricApp.Api
{
Expand All @@ -25,7 +27,7 @@ public class NetEaseMusicNativeApi : BaseNativeApi
private readonly string _secretKey;
private readonly string _encSecKey;

public NetEaseMusicNativeApi()
public NetEaseMusicNativeApi(Func<string> cookieFunc) : base(cookieFunc)
{
_secretKey = CreateSecretKey(16);
_encSecKey = RSAEncode(_secretKey);
Expand Down Expand Up @@ -68,7 +70,21 @@ public SearchResult Search(string keyword, SearchTypeEnum searchType)

var res = SendPost(url, Prepare(JsonConvert.SerializeObject(data)));

return JsonConvert.DeserializeObject<SearchResult>(res);
var obj = (JObject)JsonConvert.DeserializeObject(res);

if (obj["code"].ToString() != "200")
{
return null;
}

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

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

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

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions MusicLyricApp/Api/QQMusicApiV2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using MusicLyricApp.Bean;
using MusicLyricApp.Cache;
Expand All @@ -9,9 +10,9 @@ public class QQMusicApiV2 : MusicApiV2Cacheable
{
private readonly QQMusicNativeApi _api;

public QQMusicApiV2()
public QQMusicApiV2(Func<string> cookieAction)
{
_api = new QQMusicNativeApi();
_api = new QQMusicNativeApi(cookieAction);
}

protected override SearchSourceEnum Source0()
Expand Down
4 changes: 4 additions & 0 deletions MusicLyricApp/Api/QQMusicNativeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class QQMusicNativeApi : BaseNativeApi
{ "Lyric_1", "lyric" }, // 解压后的内容
};

public QQMusicNativeApi(Func<string> cookieFunc) : base(cookieFunc)
{
}

protected override string HttpRefer()
{
return "https://c.y.qq.com/";
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.5";
public const string Version = "v5.6";

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

Expand Down
5 changes: 4 additions & 1 deletion MusicLyricApp/Bean/MusicLyricsVO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public static class ErrorMsg
public const string SONG_PIC_GET_FAILED = "歌曲封面,获取失败";
public const string DEPENDENCY_LOSS = "缺少必须依赖,请前往项目主页下载 {0} 插件";
public const string SAVE_COMPLETE = "保存完毕,成功 {0} 跳过 {1}";
public const string NEED_LOGIN = "该搜索请求需要登陆,请填写 Cookie 后重试";

public const string GET_LATEST_VERSION_FAILED = "获取最新版本失败";
public const string THIS_IS_LATEST_VERSION = "当前版本已经是最新版本";
Expand Down Expand Up @@ -150,6 +151,8 @@ public SaveVo(string songId, SongVo songVo, LyricVo lyricVo)
public class SearchResultVo
{
public SearchTypeEnum SearchType { get; set; }

public SearchSourceEnum SearchSource { get; set; }

public readonly List<SongSearchResultVo> SongVos = new List<SongSearchResultVo>();

Expand Down Expand Up @@ -663,7 +666,7 @@ public class SearchInfo
/// <summary>
/// 实际处理的歌曲 ID 列表
/// </summary>
public readonly HashSet<string> SongIds = new HashSet<string>();
public readonly Dictionary<string, SearchSourceEnum> SongIds = new Dictionary<string, SearchSourceEnum>();

public SettingBean SettingBeanBackup { get; set; }

Expand Down
138 changes: 65 additions & 73 deletions MusicLyricApp/Bean/NetEaseMusicBean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,87 @@ namespace MusicLyricApp.Bean
/// </summary>
public class SearchResult
{
public bool NeedLogin { get; set; }
/* SearchType = SONG */
public Song[] Songs { get; set; }

public SearchResultData Result { get; set; }
public long SongCount { get; set; }

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

public Album[] Albums { get; set; }

public long AlbumCount { get; set; }

/* SearchType = PLAYLIST */

public SimplePlaylist[] Playlists { get; set; }

public class SearchResultData
public long PlaylistCount { get; set; }

/* ----- */

public SearchResultVo Convert(SearchTypeEnum searchType)
{
/* SearchType = SONG */
public Song[] Songs { get; set; }

public long SongCount { get; set; }

/* SearchType = ALBUM */

public Album[] Albums { get; set; }

public long AlbumCount { get; set; }

/* SearchType = PLAYLIST */

public SimplePlaylist[] Playlists { get; set; }

public long PlaylistCount { get; set; }

/* ----- */

public SearchResultVo Convert(SearchTypeEnum searchType)
var vo = new SearchResultVo
{
var vo = new SearchResultVo
{
SearchType = searchType
};
SearchType = searchType,
SearchSource = SearchSourceEnum.NET_EASE_MUSIC
};

switch (searchType)
{
case SearchTypeEnum.SONG_ID:
if (SongCount > 0)
switch (searchType)
{
case SearchTypeEnum.SONG_ID:
if (SongCount > 0)
{
foreach (var song in Songs)
{
foreach (var song in Songs)
vo.SongVos.Add(new SearchResultVo.SongSearchResultVo
{
vo.SongVos.Add(new SearchResultVo.SongSearchResultVo
{
DisplayId = song.Id,
Title = song.Name,
AuthorName = song.Ar.Select(e => e.Name).ToArray(),
AlbumName = song.Al.Name,
Duration = song.Dt
});
}
DisplayId = song.Id,
Title = song.Name,
AuthorName = song.Ar.Select(e => e.Name).ToArray(),
AlbumName = song.Al.Name,
Duration = song.Dt
});
}
break;
case SearchTypeEnum.ALBUM_ID:
if (AlbumCount > 0)
}
break;
case SearchTypeEnum.ALBUM_ID:
if (AlbumCount > 0)
{
foreach (var album in Albums)
{
foreach (var album in Albums)
vo.AlbumVos.Add(new SearchResultVo.AlbumSearchResultVo
{
vo.AlbumVos.Add(new SearchResultVo.AlbumSearchResultVo
{
DisplayId = album.Id.ToString(),
AlbumName = album.Name,
AuthorName = album.Artists.Select(e => e.Name).ToArray(),
SongCount = album.Size,
PublishTime = GlobalUtils.FormatDate(album.PublishTime)
});
}
DisplayId = album.Id.ToString(),
AlbumName = album.Name,
AuthorName = album.Artists.Select(e => e.Name).ToArray(),
SongCount = album.Size,
PublishTime = GlobalUtils.FormatDate(album.PublishTime)
});
}
break;
case SearchTypeEnum.PLAYLIST_ID:
if (PlaylistCount > 0)
}
break;
case SearchTypeEnum.PLAYLIST_ID:
if (PlaylistCount > 0)
{
foreach (var playlist in Playlists)
{
foreach (var playlist in Playlists)
vo.PlaylistVos.Add(new SearchResultVo.PlaylistResultVo()
{
vo.PlaylistVos.Add(new SearchResultVo.PlaylistResultVo()
{
DisplayId = playlist.Id,
PlaylistName = playlist.Name,
AuthorName = playlist.Creator.Nickname,
Description = playlist.Description,
PlayCount = playlist.PlayCount,
SongCount = playlist.TrackCount,
});
}
DisplayId = playlist.Id,
PlaylistName = playlist.Name,
AuthorName = playlist.Creator.Nickname,
Description = playlist.Description,
PlayCount = playlist.PlayCount,
SongCount = playlist.TrackCount,
});
}
break;
}

return vo;
}
break;
}

return vo;
}
}

Expand Down
3 changes: 2 additions & 1 deletion MusicLyricApp/Bean/QQMusicBean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public SearchResultVo Convert(SearchTypeEnum searchType)
{
var vo = new SearchResultVo
{
SearchType = searchType
SearchType = searchType,
SearchSource = SearchSourceEnum.QQ_MUSIC
};

switch (searchType)
Expand Down

0 comments on commit 10f9624

Please sign in to comment.