Skip to content

Commit

Permalink
增加登录功能
Browse files Browse the repository at this point in the history
  • Loading branch information
wwh1004 committed Aug 22, 2019
1 parent 87ea40e commit 75298de
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
33 changes: 33 additions & 0 deletions NLyric/NLyricImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ internal static class NLyricImpl {
private static AllCaches _allCaches;

public static async Task ExecuteAsync(Arguments arguments) {
Logger.Instance.LogInfo("登录可避免出现大部分API错误!!!", ConsoleColor.Green);
Logger.Instance.LogInfo("程序会自动过滤相似度为0的结果与歌词未被收集的结果!!!", ConsoleColor.Green);
Logger.Instance.LogNewLine();
_allCachesPath = Path.Combine(arguments.Directory, ".nlyric");
await LoginIfNeedAsync();
LoadLocalCaches();
foreach (string audioPath in Directory.EnumerateFiles(arguments.Directory, "*", SearchOption.AllDirectories)) {
string lrcPath;
Expand All @@ -51,6 +53,37 @@ public static async Task ExecuteAsync(Arguments arguments) {
SaveLocalCaches();
}

private static async Task LoginIfNeedAsync() {
do {
string userInput;

Logger.Instance.LogInfo("如果需要登录,请输入Y,反之输入N");
userInput = Console.ReadLine().Trim().ToUpperInvariant();
if (userInput == "Y") {
string account;
string password;

Console.WriteLine("请输入账号");
account = Console.ReadLine();
Console.WriteLine("请输入密码");
password = Console.ReadLine();
if (await CloudMusic.LoginAsync(account, password)) {
Logger.Instance.LogInfo("登录成功", ConsoleColor.Green);
Logger.Instance.LogNewLine();
break;
}
else {
Logger.Instance.LogError("登录失败,请重试");
Logger.Instance.LogNewLine();
}
}
else if (userInput == "N")
break;
else
Logger.Instance.LogWarning("输入有误,请重新输入!");
} while (true);
}

private static bool CanSkip(string audioPath, string lrcPath) {
string extension;

Expand Down
18 changes: 16 additions & 2 deletions NLyric/Ncm/CloudMusic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NeteaseCloudMusicApi;
using Newtonsoft.Json.Linq;
Expand All @@ -11,6 +12,19 @@ namespace NLyric.Ncm {
public static class CloudMusic {
private static readonly CloudMusicApi _api = new CloudMusicApi();

public static async Task<bool> LoginAsync(string account, string password) {
Dictionary<string, string> queries;
bool isPhone;
bool isOk;

queries = new Dictionary<string, string>();
isPhone = Regex.Match(account, "^[0-9]+$").Success;
queries[isPhone ? "phone" : "email"] = account;
queries["password"] = password;
(isOk, _) = await _api.RequestAsync(isPhone ? CloudMusicApiProviders.LoginCellphone : CloudMusicApiProviders.Login, queries);
return isOk;
}

public static async Task<NcmTrack[]> SearchTrackAsync(Track track, int limit, bool withArtists) {
List<string> keywords;
bool isOk;
Expand All @@ -34,7 +48,7 @@ public static async Task<NcmTrack[]> SearchTrackAsync(Track track, int limit, bo
throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误");
json = (JObject)json["result"];
if (json is null)
throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误");
throw new ArgumentException($"\"{string.Join(" ", keywords)}\" 中有关键词被屏蔽");
if ((int)json["songCount"] == 0)
return Array.Empty<NcmTrack>();
return ((JArray)json["songs"]).Select(t => ParseTrack(t, false)).ToArray();
Expand Down Expand Up @@ -63,7 +77,7 @@ public static async Task<NcmAlbum[]> SearchAlbumAsync(Album album, int limit, bo
throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误");
json = (JObject)json["result"];
if (json is null)
throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误");
throw new ArgumentException($"\"{string.Join(" ", keywords)}\" 中有关键词被屏蔽");
if ((int)json["albumCount"] == 0)
return Array.Empty<NcmAlbum>();
return ((JArray)json["albums"]).Select(t => ParseAlbum(t)).ToArray();
Expand Down
3 changes: 2 additions & 1 deletion NLyric/Settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"Separators": "|;,/\\&:", // 分隔符,用于分割歌手名
"WholeWordReplace": {
"sh0ut": "shØut",
"あやぽんず*": "あやぽんず*"
"あやぽんず*": "あやぽんず",
"あやぽんず*": "あやぽんず"
}, // 前面是被替换的词,后面是要替换成的词,比如歌名"sh0ut",搜索的时候会被替换成"shØut"来搜索,网易云音乐部分歌曲歌手名收录有问题,只能这么办
"Limit": 15 // 搜索结果数量
},
Expand Down

0 comments on commit 75298de

Please sign in to comment.