This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Track.cs
230 lines (188 loc) · 8.87 KB
/
Track.cs
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
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace YandexMusicApi
{
public class Track
{
private const string BaseUrl = "https://api.music.yandex.net:443";
public static JObject LikesTracks(List<string> likeTracks, string userId)
{
if (Token.token != "")
{
string urlToRequest = "/users/" + userId + "/likes/tracks/add-multiple";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
header.Add("Content-Type", "application/x-www-form-urlencoded");
header.Add("Authorization", "OAuth " + Token.token);
string likeTracksIdString = "";
int countTracksId = likeTracks.Count;
for (int i = 0; i < likeTracks.Count; i++)
if (countTracksId - 1 == i)
likeTracksIdString += likeTracks[i];
else
likeTracksIdString += likeTracks[i] + ",";
string dataRequest = "track-ids=" + likeTracksIdString;
string result = PostGet.PostDataAndHeaders(BaseUrl + urlToRequest, dataRequest, header);
JObject adResponse =
JsonConvert.DeserializeObject<JObject>(result);
return adResponse;
}
else
{
string result = "{\"error\": \"Not token\"}";
return JsonConvert.DeserializeObject<JObject>(result);
}
}
public static JObject RemoveLikesTracks(List<string> likeTracks, string userId)
{
if (Token.token != "")
{
string urlToRequest = "/users/" + userId + "/likes/tracks/remove";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
header.Add("Content-Type", "application/x-www-form-urlencoded");
header.Add("Authorization", "OAuth " + Token.token);
string removeTracksIdString = "";
int countTracksId = likeTracks.Count;
for (int i = 0; i < likeTracks.Count; i++)
if (countTracksId - 1 == i)
removeTracksIdString += likeTracks[i];
else
removeTracksIdString += likeTracks[i] + ",";
string dataRequest = "track-ids=" + removeTracksIdString;
string result = PostGet.PostDataAndHeaders(BaseUrl + urlToRequest, dataRequest, header);
JObject adResponse =
JsonConvert.DeserializeObject<JObject>(result);
return adResponse;
}
else
{
string result = "{\"error\": \"Not token\"}";
return JsonConvert.DeserializeObject<JObject>(result);
}
}
public static JObject GetLikesTrack(string userId)
{
string urlToRequest = "/users/" + userId + "/likes/tracks";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
string result = PostGet.GetWithHeaders(BaseUrl + urlToRequest, header);
JObject adResponse =
JsonConvert.DeserializeObject<JObject>(result);
return adResponse;
}
public static JObject GetInformTrack(List<string> idTracks)
{
string urlToRequest = "/tracks";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
header.Add("Content-Type", "application/x-www-form-urlencoded");
string tracksIdString = "";
int countTracksId = idTracks.Count;
for (int i = 0; i < idTracks.Count; i++)
if (countTracksId - 1 == i)
tracksIdString += idTracks[i];
else
tracksIdString += idTracks[i] + ",";
string dataRequest = "track-ids=" + tracksIdString + "&with-positions=false";
string result = PostGet.PostDataAndHeaders(BaseUrl + urlToRequest, dataRequest, header);
JObject adResponse =
JsonConvert.DeserializeObject<JObject>(result);
return adResponse;
}
public static JObject GetDownloadInfo(string trackId)
{
string urlToRequest = "/tracks/" + trackId + "/download-info";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
string result = PostGet.GetWithHeaders(BaseUrl + urlToRequest, header);
JObject adResponse =
JsonConvert.DeserializeObject<JObject>(result);
return adResponse;
}
public static JObject GetDownloadInfoWithToken(string trackId)
{
if (Token.token != "")
{
string urlToRequest = "/tracks/" + trackId + "/download-info";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
header.Add("Authorization", "OAuth " + Token.token);
string result = PostGet.GetWithHeaders(BaseUrl + urlToRequest, header);
dynamic adResponse = JsonConvert.DeserializeObject(result);
return adResponse;
}
else
{
string result = "{\"error\": \"Not token\"}";
return JsonConvert.DeserializeObject<JObject>(result);
}
}
private static Dictionary<string, string> GetXml(string vvod)
{
XDocument doc = XDocument.Parse(vvod);
IEnumerable<XElement> el = doc.Root.Elements();
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (XElement i in el) result.Add(i.Name.ToString(), i.Value);
return result;
}
public static string GetDirectLink(string downloadInfoUrl, string codec = "mp3")
{
string resultGetXml = PostGet.Get(downloadInfoUrl);
Console.WriteLine(resultGetXml);
Dictionary<string, string> xmlResult = GetXml(resultGetXml);
string host = xmlResult["host"];
string path = xmlResult["path"];
string ts = xmlResult["ts"];
string s = xmlResult["s"];
string secret = $"XGRlBW9FXlekgbPrRHuSiA{path.Substring(1, path.Length - 1)}{s}";
MD5 md5 = MD5.Create();
byte[] md5Hash = md5.ComputeHash(Encoding.UTF8.GetBytes(secret));
HMACSHA1 hmacsha1 = new HMACSHA1();
byte[] hmasha1Hash = hmacsha1.ComputeHash(md5Hash);
string sign = BitConverter.ToString(hmasha1Hash).Replace("-", "").ToLower();
return $"https://{host}/get-{codec}/{sign}/{ts}/{path}";
}
public static JObject GetTrackSimilar(string trackId)
{
string urlToRequest = "/tracks/" + trackId + "/similar";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
string result = PostGet.GetWithHeaders(BaseUrl + urlToRequest, header);
dynamic adResponse = JsonConvert.DeserializeObject(result);
return adResponse;
}
public static JObject GetDislikesTracks(string userId)
{
if (Token.token != "")
{
string urlToRequest = "/users/" + userId + "/dislikes/tracks";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
header.Add("Authorization", "OAuth " + Token.token);
string result = PostGet.GetWithHeaders(BaseUrl + urlToRequest, header);
dynamic adResponse = JsonConvert.DeserializeObject(result);
return adResponse;
}
else
{
string result = "{\"error\": \"Not token\"}";
return JsonConvert.DeserializeObject<JObject>(result);
}
}
public static JObject GetSupplement(string trackId)
{
string urlToRequest = "/tracks/" + trackId + "/supplement";
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("accept", "application/json");
string result = PostGet.GetWithHeaders(BaseUrl + urlToRequest, header);
dynamic adResponse = JsonConvert.DeserializeObject(result);
return adResponse;
}
}
}