From 3b66f8106ab0dcb7f13dcd1ee77bd6c83e23f520 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 4 Feb 2016 18:24:06 +0100 Subject: [PATCH] SoundCloud track streaming support, trivia win = 10, bye is not a mention --- NadekoBot/Classes/Music/SoundCloud.cs | 105 +++++++++++++++++++++++ NadekoBot/Classes/Music/StreamRequest.cs | 36 +++++--- NadekoBot/Classes/Trivia/TriviaGame.cs | 2 +- NadekoBot/Classes/_JSONModels.cs | 1 + NadekoBot/Commands/ServerGreetCommand.cs | 2 +- NadekoBot/NadekoBot.cs | 4 + NadekoBot/NadekoBot.csproj | 1 + 7 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 NadekoBot/Classes/Music/SoundCloud.cs diff --git a/NadekoBot/Classes/Music/SoundCloud.cs b/NadekoBot/Classes/Music/SoundCloud.cs new file mode 100644 index 00000000..b8a95690 --- /dev/null +++ b/NadekoBot/Classes/Music/SoundCloud.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace NadekoBot.Classes.Music { + public class SoundCloud { + private static readonly SoundCloud _instance = new SoundCloud(); + public static SoundCloud Default => _instance; + + static SoundCloud() { } + public SoundCloud() { } + + public async Task GetVideoAsync(string url) { + if (string.IsNullOrWhiteSpace(url)) + throw new ArgumentNullException(nameof(url)); + if (string.IsNullOrWhiteSpace(NadekoBot.creds.SoundCloudClientID)) + throw new ArgumentNullException(nameof(NadekoBot.creds.SoundCloudClientID)); + + var response = await Modules.Searches.GetResponseAsync($"http://api.soundcloud.com/resolve?url={url}&client_id={NadekoBot.creds.SoundCloudClientID}"); + + var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject(response); + 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)(.*)"); + } + + public class SoundCloudVideo { + public string Kind =""; + public long Id = 0; + public SoundCloudUser User = new SoundCloudUser(); + public string Title = ""; + public string FullName => User.Name + " - " + Title; + public bool Streamable = false; + public string StreamLink => $"https://api.soundcloud.com/tracks/{Id}/stream?client_id={NadekoBot.creds.SoundCloudClientID}"; + } + public class SoundCloudUser { + [Newtonsoft.Json.JsonProperty("username")] + public string Name; + } + /* + {"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"} + + */ + +} diff --git a/NadekoBot/Classes/Music/StreamRequest.cs b/NadekoBot/Classes/Music/StreamRequest.cs index 6b1449cf..352d422e 100644 --- a/NadekoBot/Classes/Music/StreamRequest.cs +++ b/NadekoBot/Classes/Music/StreamRequest.cs @@ -53,21 +53,31 @@ namespace NadekoBot.Classes.Music { } private async void ResolveStreamLink() { - Video video = null; + string uri = null; try { - if (OnResolving != null) - OnResolving(); - var links = await Searches.FindYoutubeUrlByKeywords(Query); - var videos = await YouTube.Default.GetAllVideosAsync(links); - video = videos - .Where(v => v.AdaptiveKind == AdaptiveKind.Audio) - .OrderByDescending(v => v.AudioBitrate) - .FirstOrDefault(); + if (SoundCloud.Default.IsSoundCloudLink(Query)) { - if (video == null) // do something with this error - throw new Exception("Could not load any video elements based on the query."); + var svideo = await SoundCloud.Default.GetVideoAsync(Query); + Title = svideo.FullName + " - SoundCloud"; + uri = svideo.StreamLink; + Console.WriteLine(uri); + } else { - Title = video.Title; //.Substring(0,video.Title.Length-10); // removing trailing "- You Tube" + if (OnResolving != null) + OnResolving(); + var links = await Searches.FindYoutubeUrlByKeywords(Query); + var videos = await YouTube.Default.GetAllVideosAsync(links); + var video = videos + .Where(v => v.AdaptiveKind == AdaptiveKind.Audio) + .OrderByDescending(v => v.AudioBitrate) + .FirstOrDefault(); + + if (video == null) // do something with this error + throw new Exception("Could not load any video elements based on the query."); + + Title = video.Title; //.Substring(0,video.Title.Length-10); // removing trailing "- You Tube" + uri = video.Uri; + } } catch (Exception ex) { privateState = StreamState.Completed; if (OnResolvingFailed != null) @@ -76,7 +86,7 @@ namespace NadekoBot.Classes.Music { return; } - musicStreamer = new MusicStreamer(this, video.Uri); + musicStreamer = new MusicStreamer(this, uri); if (OnQueued != null) OnQueued(); } diff --git a/NadekoBot/Classes/Trivia/TriviaGame.cs b/NadekoBot/Classes/Trivia/TriviaGame.cs index 750240fa..0a87e85b 100644 --- a/NadekoBot/Classes/Trivia/TriviaGame.cs +++ b/NadekoBot/Classes/Trivia/TriviaGame.cs @@ -28,7 +28,7 @@ namespace NadekoBot.Classes.Trivia { public bool GameActive { get; private set; } = false; public bool ShouldStopGame { get; private set; } - public int WinRequirement { get; } = 3; + public int WinRequirement { get; } = 10; public TriviaGame(CommandEventArgs e) { _server = e.Server; diff --git a/NadekoBot/Classes/_JSONModels.cs b/NadekoBot/Classes/_JSONModels.cs index b86fab66..236e41a1 100644 --- a/NadekoBot/Classes/_JSONModels.cs +++ b/NadekoBot/Classes/_JSONModels.cs @@ -12,6 +12,7 @@ public string TrelloAppKey; public bool? ForwardMessages; public string OsuApiKey; + public string SoundCloudClientID; } public class AnimeResult { diff --git a/NadekoBot/Commands/ServerGreetCommand.cs b/NadekoBot/Commands/ServerGreetCommand.cs index f235f356..8f1e8c66 100644 --- a/NadekoBot/Commands/ServerGreetCommand.cs +++ b/NadekoBot/Commands/ServerGreetCommand.cs @@ -51,7 +51,7 @@ namespace NadekoBot.Commands { var controls = AnnouncementsDictionary[e.Server.Id]; var channel = NadekoBot.client.GetChannel(controls.ByeChannel); if (channel == null) return; - var msg = controls.ByeText.Replace("%user%", e.User.Mention).Trim(); + var msg = controls.ByeText.Replace("%user%", "**"+e.User.Name+"**").Trim(); if (string.IsNullOrEmpty(msg)) return; Greeted++; diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index a1be2c5b..50d5ea93 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -56,6 +56,10 @@ namespace NadekoBot { Console.WriteLine("No osu API key found. Osu functionality is disabled."); else Console.WriteLine("Osu enabled."); + if(string.IsNullOrWhiteSpace(creds.SoundCloudClientID)) + Console.WriteLine("No soundcloud Client ID found. Soundcloud streaming is disabled."); + else + Console.WriteLine("SoundCloud streaming enabled."); //init parse ParseClient.Initialize(creds.ParseID, creds.ParseKey); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 38cbb66d..318fd31d 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -136,6 +136,7 @@ +