2016-08-25 17:23:47 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2016-04-14 22:30:34 +00:00
|
|
|
|
using System;
|
2016-07-09 02:25:07 +00:00
|
|
|
|
using System.Linq;
|
2016-08-21 22:37:39 +00:00
|
|
|
|
using System.Net.Http;
|
2016-02-04 17:24:06 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-07-11 01:16:56 +00:00
|
|
|
|
namespace NadekoBot.Services.Impl
|
2016-04-14 22:30:34 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class SoundCloudApiService : INService
|
2016-04-14 22:30:34 +00:00
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
private readonly IBotCredentials _creds;
|
|
|
|
|
|
2017-05-24 04:43:00 +00:00
|
|
|
|
public SoundCloudApiService(IBotCredentials creds)
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
|
|
|
|
_creds = creds;
|
|
|
|
|
}
|
2016-02-04 17:24:06 +00:00
|
|
|
|
|
2016-07-09 02:25:07 +00:00
|
|
|
|
public async Task<SoundCloudVideo> ResolveVideoAsync(string url)
|
2016-04-14 22:30:34 +00:00
|
|
|
|
{
|
2016-02-04 17:24:06 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
|
|
|
throw new ArgumentNullException(nameof(url));
|
|
|
|
|
|
2016-08-21 22:37:39 +00:00
|
|
|
|
string response = "";
|
|
|
|
|
|
|
|
|
|
using (var http = new HttpClient())
|
|
|
|
|
{
|
2017-06-15 16:56:35 +00:00
|
|
|
|
response = await http.GetStringAsync($"https://scapi.nadekobot.me/resolve?url={url}").ConfigureAwait(false);
|
2016-08-21 22:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 17:24:06 +00:00
|
|
|
|
|
2017-05-24 04:43:00 +00:00
|
|
|
|
var responseObj = JsonConvert.DeserializeObject<SoundCloudVideo>(response);
|
2016-02-04 17:24:06 +00:00
|
|
|
|
if (responseObj?.Kind != "track")
|
|
|
|
|
throw new InvalidOperationException("Url is either not a track, or it doesn't exist.");
|
|
|
|
|
|
|
|
|
|
return responseObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsSoundCloudLink(string url) =>
|
|
|
|
|
System.Text.RegularExpressions.Regex.IsMatch(url, "(.*)(soundcloud.com|snd.sc)(.*)");
|
2016-07-09 02:25:07 +00:00
|
|
|
|
|
2016-09-01 01:12:08 +00:00
|
|
|
|
public async Task<SoundCloudVideo> GetVideoByQueryAsync(string query)
|
2016-07-09 02:25:07 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(query))
|
|
|
|
|
throw new ArgumentNullException(nameof(query));
|
|
|
|
|
|
2016-08-21 22:37:39 +00:00
|
|
|
|
var response = "";
|
|
|
|
|
using (var http = new HttpClient())
|
|
|
|
|
{
|
2017-06-15 16:56:35 +00:00
|
|
|
|
response = await http.GetStringAsync($"https://scapi.nadekobot.me/tracks?q={Uri.EscapeDataString(query)}").ConfigureAwait(false);
|
2016-08-21 22:37:39 +00:00
|
|
|
|
}
|
2016-07-09 02:25:07 +00:00
|
|
|
|
|
|
|
|
|
var responseObj = JsonConvert.DeserializeObject<SoundCloudVideo[]>(response).Where(s => s.Streamable).FirstOrDefault();
|
|
|
|
|
if (responseObj?.Kind != "track")
|
|
|
|
|
throw new InvalidOperationException("Query yielded no results.");
|
|
|
|
|
|
|
|
|
|
return responseObj;
|
|
|
|
|
}
|
2016-02-04 17:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 22:30:34 +00:00
|
|
|
|
public class SoundCloudVideo
|
|
|
|
|
{
|
2016-06-29 23:05:37 +00:00
|
|
|
|
public string Kind { get; set; } = "";
|
|
|
|
|
public long Id { get; set; } = 0;
|
|
|
|
|
public SoundCloudUser User { get; set; } = new SoundCloudUser();
|
|
|
|
|
public string Title { get; set; } = "";
|
|
|
|
|
[JsonIgnore]
|
2016-02-04 17:24:06 +00:00
|
|
|
|
public string FullName => User.Name + " - " + Title;
|
2016-06-29 23:05:37 +00:00
|
|
|
|
public bool Streamable { get; set; } = false;
|
2016-10-19 10:24:07 +00:00
|
|
|
|
public int Duration { get; set; }
|
2016-06-29 23:05:37 +00:00
|
|
|
|
[JsonProperty("permalink_url")]
|
|
|
|
|
public string TrackLink { get; set; } = "";
|
2016-12-14 19:50:37 +00:00
|
|
|
|
public string artwork_url { get; set; } = "";
|
2017-06-15 16:56:35 +00:00
|
|
|
|
public async Task<string> StreamLink()
|
|
|
|
|
{
|
|
|
|
|
using (var http = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
return await http.GetStringAsync($"http://scapi.nadekobot.me/stream/{Id}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-04 17:24:06 +00:00
|
|
|
|
}
|
2016-04-14 22:30:34 +00:00
|
|
|
|
public class SoundCloudUser
|
|
|
|
|
{
|
2017-05-24 04:43:00 +00:00
|
|
|
|
[JsonProperty("username")]
|
2016-06-29 23:05:37 +00:00
|
|
|
|
public string Name { get; set; }
|
2016-02-04 17:24:06 +00:00
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
{"kind":"track",
|
|
|
|
|
"id":238888167,
|
|
|
|
|
"created_at":"2015/12/24 01:04:52 +0000",
|
|
|
|
|
"user_id":43141975,
|
|
|
|
|
"duration":120852,
|
|
|
|
|
"commentable":true,
|
|
|
|
|
"state":"finished",
|
|
|
|
|
"original_content_size":4834829,
|
|
|
|
|
"last_modified":"2015/12/24 01:17:59 +0000",
|
|
|
|
|
"sharing":"public",
|
|
|
|
|
"tag_list":"Funky",
|
|
|
|
|
"permalink":"18-fd",
|
|
|
|
|
"streamable":true,
|
|
|
|
|
"embeddable_by":"all",
|
|
|
|
|
"downloadable":false,
|
|
|
|
|
"purchase_url":null,
|
|
|
|
|
"label_id":null,
|
|
|
|
|
"purchase_title":null,
|
|
|
|
|
"genre":"Disco",
|
|
|
|
|
"title":"18 Ж",
|
|
|
|
|
"description":"",
|
|
|
|
|
"label_name":null,
|
|
|
|
|
"release":null,
|
|
|
|
|
"track_type":null,
|
|
|
|
|
"key_signature":null,
|
|
|
|
|
"isrc":null,
|
|
|
|
|
"video_url":null,
|
|
|
|
|
"bpm":null,
|
|
|
|
|
"release_year":null,
|
|
|
|
|
"release_month":null,
|
|
|
|
|
"release_day":null,
|
|
|
|
|
"original_format":"mp3",
|
|
|
|
|
"license":"all-rights-reserved",
|
|
|
|
|
"uri":"https://api.soundcloud.com/tracks/238888167",
|
|
|
|
|
"user":{
|
|
|
|
|
"id":43141975,
|
|
|
|
|
"kind":"user",
|
|
|
|
|
"permalink":"mrb00gi",
|
|
|
|
|
"username":"Mrb00gi",
|
|
|
|
|
"last_modified":"2015/12/01 16:06:57 +0000",
|
|
|
|
|
"uri":"https://api.soundcloud.com/users/43141975",
|
|
|
|
|
"permalink_url":"http://soundcloud.com/mrb00gi",
|
|
|
|
|
"avatar_url":"https://a1.sndcdn.com/images/default_avatar_large.png"
|
|
|
|
|
},
|
|
|
|
|
"permalink_url":"http://soundcloud.com/mrb00gi/18-fd",
|
|
|
|
|
"artwork_url":null,
|
|
|
|
|
"waveform_url":"https://w1.sndcdn.com/gsdLfvEW1cUK_m.png",
|
|
|
|
|
"stream_url":"https://api.soundcloud.com/tracks/238888167/stream",
|
|
|
|
|
"playback_count":7,
|
|
|
|
|
"download_count":0,
|
|
|
|
|
"favoritings_count":1,
|
|
|
|
|
"comment_count":0,
|
|
|
|
|
"attachments_uri":"https://api.soundcloud.com/tracks/238888167/attachments"}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
}
|