forked from AlekseyMartynov/shazam-for-real
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShazamApi.cs
49 lines (40 loc) · 1.51 KB
/
ShazamApi.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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
static class ShazamApi {
static readonly HttpClient HTTP = new HttpClient();
static readonly string INSTALLATION_ID = Guid.NewGuid().ToString();
static ShazamApi() {
HTTP.DefaultRequestHeaders.UserAgent.ParseAdd("curl/7");
}
public static async Task<ShazamResult> SendRequest(string tagId, int samplems, byte[] sig) {
var payload = new {
signature = new {
uri = "data:audio/vnd.shazam.sig;base64," + Convert.ToBase64String(sig),
samplems
}
};
var url = "https://amp.shazam.com/discovery/v5/en/US/android/-/tag/" + INSTALLATION_ID + "/" + tagId;
var postData = new StringContent(
JsonConvert.SerializeObject(payload),
Encoding.UTF8,
"application/json"
);
var result = new ShazamResult();
var res = await HTTP.PostAsync(url, postData);
var obj = JsonConvert.DeserializeObject<JToken>(await res.Content.ReadAsStringAsync());
var track = obj.Value<JToken>("track");
if(track != null) {
result.Success = true;
result.Url = track.Value<string>("url");
result.Title = track.Value<string>("title");
result.Artist = track.Value<string>("subtitle");
} else {
result.RetryMs = obj.Value<int>("retryms");
}
return result;
}
}