forked from mo10/TS3NeteaseCloudMusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIHelper.cs
More file actions
288 lines (264 loc) · 11.2 KB
/
APIHelper.cs
File metadata and controls
288 lines (264 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TS3AudioBot;
using TS3AudioBot.Helper;
namespace ts3ncm
{
public static class APIHelper
{
private static long GetTimestamp() => DateTimeOffset.Now.ToUnixTimeSeconds();
#region 登入API
/// <summary>
/// 二维码 key 生成接口
/// </summary>
/// <param name="server"></param>
/// <returns></returns>
public static async Task<string> GetLoginQRKeyAsync(string server)
{
var obj = await WebWrapper.Request($"{server}/login/qr/key?timestamp={GetTimestamp()}").AsJson<RespQRKey>();
if (obj == null || obj.code != 200)
return "";
return obj.data.unikey;
}
/// <summary>
/// 二维码生成接口
/// </summary>
/// <param name="server"></param>
/// <param name="unikey">由第一个接口生成</param>
/// <returns>二维码图片Stream</returns>
public static async Task<Stream> GetLoginQRImageAsync(string server, string unikey)
{
var obj = await WebWrapper.Request($"{server}/login/qr/create?key={unikey}&qrimg=true×tamp={GetTimestamp()}").AsJson<RespQRCreate>();
if (obj == null || obj.code != 200 || obj.data.qrimg.Length == 0)
return null;
byte[] imgBytes = Convert.FromBase64String(obj.data.qrimg.Split(',')[1]);
return new MemoryStream(imgBytes);
}
/// <summary>
/// 二维码检测扫码状态接口
/// </summary>
/// <param name="server"></param>
/// <param name="unikey"></param>
/// <returns>800 为二维码过期.801 为等待扫码,802 为待确认,803 为授权登录成功(803 状态码下会返回 cookies)</returns>
public static async Task<RespQRCheck> GetLoginQRStatusAsync(string server, string unikey)
{
var obj = await WebWrapper.Request($"{server}/login/qr/check?key={unikey}×tamp={GetTimestamp()}").AsJson<RespQRCheck>();
return obj;
}
/// <summary>
/// 登录状态
/// </summary>
/// <param name="server"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static async Task<RespStatus> GetLoginStatusAasync(string server, string cookie)
{
return await WebWrapper.Request($"{server}/login/status?timestamp={GetTimestamp()}")
.WithHeader("Cookie", cookie)
.AsJson<RespStatus>();
}
#endregion
#region 歌曲信息
private static IEnumerable<SongDetail> ParseToSongDetails(IEnumerable<JsonSongDetailSong> songs)
{
List<SongDetail> details = new List<SongDetail>();
foreach (var song in songs)
{
if (song.noCopyrightRcmd != null)
continue;
var detail = new SongDetail()
{
id = $"{song.id}",
title = song.name,
author = "",
program_id = "",
};
if (song.al != null)
detail.picUrl = song.al.picUrl;
if (song.ar != null && song.ar.Length > 0)
detail.author = string.Join('/', song.ar.Select(ar => ar.name));
details.Add(detail);
}
return details;
}
public static async Task<IEnumerable<SongDetail>> GetAlbumDetailAsync(string server, string cookie, string albumId)
{
JsonSongDetail result;
try
{
var request = WebWrapper.Request($"{server}/album?id={albumId}×tamp={GetTimestamp()}");
if(!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
result = await request.AsJson<JsonSongDetail>();
}
catch (Exception ex)
{
throw Error.Str($"获取专辑信息失败: {ex}");
}
if (result == null || result.code != 200)
throw Error.Str($"专辑接口查询失败");
if (result.songs.Length == 0)
throw Error.Str($"专辑是空的或不存在");
return ParseToSongDetails(result.songs);
}
public static async Task<IEnumerable<SongDetail>> GetPlaylistDetailAsync(string server, string cookie, string listId)
{
JsonSongDetail result;
try
{
var request = WebWrapper.Request($"{server}/playlist/track/all?id={listId}&limit=100×tamp={GetTimestamp()}");
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
result = await request.AsJson<JsonSongDetail>();
}
catch (Exception ex)
{
throw Error.Str($"获取专辑信息失败: {ex}");
}
if (result == null || result.code != 200)
throw Error.Str($"歌单接口查询失败");
if (result.songs.Length == 0)
throw Error.Str($"歌单是空的或不存在");
return ParseToSongDetails(result.songs);
}
public static async Task<IEnumerable<SongDetail>> GetSongDetailAsync(string server, string cookie, string songId)
{
JsonSongDetail result;
try
{
var request = WebWrapper.Request($"{server}/song/detail?ids={songId}×tamp={GetTimestamp()}");
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
result = await request.AsJson<JsonSongDetail>();
}
catch (Exception ex)
{
throw Error.Str($"获取歌曲信息失败: {ex}");
}
if (result == null || result.code != 200)
throw Error.Str($"歌曲接口查询失败");
if (result.songs.Length == 0)
throw Error.Str($"找不到这个歌曲");
return ParseToSongDetails(result.songs);
}
public static async Task<IEnumerable<SongDetail>> GetSearchDetailAsync(string server, string cookie, string keyword)
{
JsonSearch result;
try
{
var request = WebWrapper.Request($"{server}/cloudsearch?keywords={Uri.EscapeDataString(keyword)}×tamp={GetTimestamp()}");
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
result = await request.AsJson<JsonSearch>();
}
catch (Exception ex)
{
throw Error.Str($"搜索失败: {ex}");
}
if (result == null || result.code != 200)
throw Error.Str($"搜索接口查询失败");
if (result.result == null || result.result.songs == null || result.result.songs.Length == 0)
throw Error.Str($"什么都没有搜索到");
return ParseToSongDetails(result.result.songs);
}
public static async Task<IEnumerable<SongDetail>> GetDjRadioDetailAsync(string server, string cookie, string radioId)
{
RespDjRadio result;
try
{
var request = WebWrapper.Request($"{server}/dj/program?rid={radioId}&limit=100×tamp={GetTimestamp()}");
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
result = await request.AsJson<RespDjRadio>();
}
catch (Exception ex)
{
throw Error.Str($"获取电台信息失败: {radioId} {ex}");
}
if (result == null || result.code != 200)
throw Error.Str($"电台接口查询失败");
if (result.programs.Length == 0)
throw Error.Str($"电台是空的或不存在");
List<SongDetail> details = new List<SongDetail>();
foreach (var song in result.programs)
{
var detail = new SongDetail()
{
id = $"{song.mainTrackId}",
program_id = $"{song.id}",
title = song.name,
author = "电台节目",
picUrl = song.coverUrl
};
details.Add(detail);
}
return details;
}
public static async Task<IEnumerable<SongDetail>> GetProgramDetailAsync(string server, string cookie, string programId)
{
RespProgramDetail result;
try
{
var request = WebWrapper.Request($"{server}/dj/program/detail?id={programId}×tamp={GetTimestamp()}");
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
result = await request.AsJson<RespProgramDetail>();
}
catch (Exception ex)
{
throw Error.Str($"获取节目信息失败: {programId} {ex}");
}
if (result == null || result.code != 200)
throw Error.Str($"节目接口查询失败");
List<SongDetail> details = new List<SongDetail>
{
new SongDetail()
{
id = $"{result.program.mainTrackId}",
program_id = $"{result.program.id}",
title = result.program.name,
author = "电台节目",
picUrl = result.program.coverUrl
}
};
return details;
}
public static async Task<bool> CheckSongCanPlay(string server, string cookie, string songId)
{
var request = WebWrapper.Request($"{server}/check/music?id={songId}");
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
var result = await request.AsJson<JsonCheckMusic>();
return result.success;
}
public static async Task<string> GetAudioUrlAsync(string server, string cookie, string songId)
{
JsonSongUrlInfo result;
try
{
var request = WebWrapper.Request($"{server}/song/url?id={songId}&br=320000×tamp={GetTimestamp()}");
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add("Cookie", cookie);
result = await request.AsJson<JsonSongUrlInfo>();
}
catch (Exception ex)
{
throw Error.Str($"获取播放链接失败: {ex}");
}
if (result == null || result.code != 200)
throw Error.Str($"播放链接接口查询失败");
if (result.data == null || result.data.Length == 0 || result.data[0].url == null)
{
throw Error.Str($"没有播放路径: {songId}");
}
return result.data[0].url;
}
#endregion
}
}