Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

解决歌曲API无法使用的问题 #27

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions DGJv3/DGJv3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DGJv3</RootNamespace>
<AssemblyName>DGJv3</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -69,15 +70,28 @@
<HintPath>..\packages\NAudio.1.8.4\lib\net35\NAudio.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="Scriban, Version=1.2.5.0, Culture=neutral, PublicKeyToken=5675fb69b15f2433, processorArchitecture=MSIL">
<HintPath>..\packages\Scriban.Signed.1.2.5\lib\net40\Scriban.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
Expand Down
52 changes: 11 additions & 41 deletions DGJv3/InternalModule/LwlApiBaseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal class LwlApiBaseModule : SearchModule
protected void SetServiceName(string name) => ServiceName = name;

private const string API_PROTOCOL = "https://";
private const string API_HOST = "api.lwl12.com";
private const string API_PATH = "/music/";
private const string API_HOST = "v1.itooi.cn";
private const string API_PATH = "/";

protected const string INFO_PREFIX = "";
protected const string INFO_AUTHOR = "Genteure & LWL12";
Expand All @@ -44,15 +44,7 @@ protected override string GetDownloadUrl(SongItem songInfo)

if (dlurlobj["code"].ToString() == "200")
{
if (dlurlobj["result"] is JObject)
{
dlurlobj = (JObject)dlurlobj["result"];
}
else
{
dlurlobj = JObject.Parse(dlurlobj["result"].Value<string>());
}
return dlurlobj["url"].ToString();
return $"{API_PROTOCOL}{API_HOST}{API_PATH}{ServiceName}/url?id={songInfo.SongId}";
}
else
{
Expand All @@ -71,29 +63,7 @@ protected override string GetLyricById(string Id)
{
try
{
JObject lobj = JObject.Parse(Fetch(API_PROTOCOL, API_HOST, API_PATH + ServiceName + $"/lyric?id={Id}"));
if (lobj["result"] is JObject)
{
lobj = (JObject)lobj["result"];
}
else
{
lobj = JObject.Parse(lobj["result"].Value<string>());
}
if (lobj["lwlyric"] != null)
{
return lobj["lwlyric"].ToString();
}
else if (lobj["tlyric"] != null)
{
return lobj["tlyric"].ToString();
}
else if (lobj["lyric"] != null)
{
return lobj["lyric"].ToString();
}
else
{ Log("歌词获取错误(id:" + Id + ")"); }
return Fetch(API_PROTOCOL, API_HOST, API_PATH + ServiceName + $"/lrc?id={Id}");

}
catch (Exception ex)
Expand All @@ -108,11 +78,11 @@ protected override List<SongInfo> GetPlaylist(string keyword)
{
List<SongInfo> songInfos = new List<SongInfo>();

JObject playlist = JObject.Parse(Fetch(API_PROTOCOL, API_HOST, API_PATH + ServiceName + $"/playlist?id={HttpUtility.UrlEncode(keyword)}"));
JObject playlist = JObject.Parse(Fetch(API_PROTOCOL, API_HOST, API_PATH + ServiceName + $"/search?id={HttpUtility.UrlEncode(keyword)}&type=songList&pageSize=5&page=0"));

if (playlist["code"]?.ToObject<int>() == 200)
{
List<JToken> result = (playlist["result"] as JArray).ToList();
List<JToken> result = (playlist["data"]["playlists"] as JArray).ToList();

//if (result.Count() > 50)
// result = result.Take(50).ToList();
Expand All @@ -124,7 +94,7 @@ protected override List<SongInfo> GetPlaylist(string keyword)
var songInfo = new SongInfo(this,
song["id"].ToString(),
song["name"].ToString(),
(song["artist"] as JArray).Select(x => x.ToString()).ToArray());
(song["ar"] as JArray).Select(x => x["name"].ToString()).ToArray());

songInfo.Lyric = null;//在之后再获取Lyric

Expand Down Expand Up @@ -153,7 +123,7 @@ protected override SongInfo Search(string keyword)
string result_str;
try
{
result_str = Fetch(API_PROTOCOL, API_HOST, API_PATH + ServiceName + $"/search?keyword={HttpUtility.UrlEncode(keyword)}");
result_str = Fetch(API_PROTOCOL, API_HOST, API_PATH + ServiceName + $"/search?keyword={HttpUtility.UrlEncode(keyword)}&type=song&pageSize=5&page=0");
}
catch (Exception ex)
{
Expand All @@ -167,7 +137,7 @@ protected override SongInfo Search(string keyword)
JObject info = JObject.Parse(result_str);
if (info["code"].ToString() == "200")
{
song = (info["result"] as JArray)?[0] as JObject;
song = (info["data"]["songs"] as JArray)?[0] as JObject;
}
}
catch (Exception ex)
Expand All @@ -185,7 +155,7 @@ protected override SongInfo Search(string keyword)
this,
song["id"].ToString(),
song["name"].ToString(),
(song["artist"] as JArray).Select(x => x.ToString()).ToArray()
(song["ar"] as JArray).Select(x => x["name"].ToString()).ToArray()
);
}
catch (Exception ex)
Expand All @@ -196,7 +166,7 @@ protected override SongInfo Search(string keyword)
return songInfo;
}

private static string Fetch(string prot, string host, string path, string data = null, string referer = null)
protected static string Fetch(string prot, string host, string path, string data = null, string referer = null)
{
for (int retryCount = 0; retryCount < 4; retryCount++)
{
Expand Down
133 changes: 132 additions & 1 deletion DGJv3/InternalModule/LwlApiNetease.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,142 @@
namespace DGJv3.InternalModule
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace DGJv3.InternalModule
{
sealed class LwlApiNetease : LwlApiBaseModule
{
private const string API_PROTOCOL = "http://";
// private const string API_HOST = "v1.itooi.cn";
private const string API_HOST = "music.163.com";
private const string API_PATH = "/api";

internal LwlApiNetease()
{
SetServiceName("netease");
SetInfo("网易云音乐", INFO_AUTHOR, INFO_EMAIL, INFO_VERSION, "搜索网易云音乐的歌曲");
}

protected override string GetDownloadUrl(SongItem songInfo)
{
try
{
// api.Request(CloudMusicApiProviders.SongUrl,
// new Dictionary<string, string> { { "id", songInfo.SongId } }, out var response);
return $"https://music.163.com/song/media/outer/url?id={songInfo.SongId}.mp3";
}
catch (Exception ex)
{
Log($"歌曲 {songInfo.SongName} 疑似版权不能下载(ex:{ex.Message})");
return null;
}
}

protected override string GetLyricById(string Id)
{
try
{
var response = Fetch(API_PROTOCOL, API_HOST,
API_PATH +
$"/song/lyric?id={Id}&lv=1&kv=1&tv=-1");
var json = JObject.Parse(response);

return json["lrc"]["lyric"].ToString();
}
catch (Exception ex)
{
Log($"歌曲 {Id} 歌词下载错误(ex:{ex.Message})");
return null;
}
}

protected override List<SongInfo> GetPlaylist(string keyword)
{
var Id = 0;
try
{
var response = Fetch(API_PROTOCOL, API_HOST,
API_PATH +
$"/search/get/web?csrf_token=hlpretag=&hlposttag=&s={keyword}&type=1000&offset=0&total=true&limit=3");
var json = JObject.Parse(response);
var playlist = (json["result"]["playlists"] as JArray)?[0] as JObject;
Id = playlist.Value<int>("id");
}
catch (Exception ex)
{
Log($"歌单下载错误(ex:{ex.Message})");
return null;
}

try
{
var response = Fetch(API_PROTOCOL, API_HOST,
API_PATH +
$"/search/get/web?csrf_token=hlpretag=&hlposttag=&s={keyword}&type=1&offset=0&total=true&limit=3");
var json = JObject.Parse(response);
return (json["result"]["tracks"] as JArray)?.Select(song =>
{
SongInfo songInfo;

try
{
songInfo = new SongInfo(
this,
song["id"].ToString(),
song["name"].ToString(),
(song["artists"] as JArray).Select(x => x["name"].ToString()).ToArray()
);
}
catch (Exception ex)
{ Log("歌曲信息获取结果错误:" + ex.Message); return null; }

songInfo.Lyric = GetLyricById(songInfo.Id);
return songInfo;
}).ToList();

}
catch (Exception ex)
{
Log($"歌单下载错误(ex:{ex.Message})");
return null;
}
}

protected override SongInfo Search(string keyword)
{
try
{
var response = Fetch(API_PROTOCOL, API_HOST,
API_PATH +
$"/search/get/web?csrf_token=hlpretag=&hlposttag=&s={keyword}&type=1&offset=0&total=true&limit=3");
var json = JObject.Parse(response);
var song = (json["result"]["songs"] as JArray)?[0] as JObject;

SongInfo songInfo;

try
{
songInfo = new SongInfo(
this,
song["id"].ToString(),
song["name"].ToString(),
(song["artists"] as JArray).Select(x => x["name"].ToString()).ToArray()
);
}
catch (Exception ex)
{ Log("歌曲信息获取结果错误:" + ex.Message); return null; }

songInfo.Lyric = GetLyricById(songInfo.Id);

return songInfo;
}
catch (Exception ex)
{
Log(ex.Message);
return null;
}
}

}
}
6 changes: 5 additions & 1 deletion DGJv3/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<package id="MaterialDesignColors" version="1.1.3" targetFramework="net462" />
<package id="MaterialDesignThemes" version="2.4.1.1101" targetFramework="net462" />
<package id="NAudio" version="1.8.4" targetFramework="net462" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net462" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
<package id="Scriban.Signed" version="1.2.5" targetFramework="net462" />
<package id="System.Buffers" version="4.4.0" targetFramework="net461" />
<package id="System.Memory" version="4.5.3" targetFramework="net461" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net461" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net461" />
</packages>