diff --git a/NadekoBot/Classes/SearchHelper.cs b/NadekoBot/Classes/SearchHelper.cs index ad78466f..2b3ba0d8 100644 --- a/NadekoBot/Classes/SearchHelper.cs +++ b/NadekoBot/Classes/SearchHelper.cs @@ -159,8 +159,17 @@ namespace NadekoBot.Classes $"part=snippet&maxResults=1" + $"&q={Uri.EscapeDataString(keywords)}" + $"&key={NadekoBot.Creds.GoogleAPIKey}").ConfigureAwait(false); - dynamic obj = JObject.Parse(response); - return "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString(); + JObject obj = JObject.Parse(response); + + var data = JsonConvert.DeserializeObject(response); + + if (data.items.Length > 0) + { + var toReturn = "http://www.youtube.com/watch?v=" + data.items[0].id.videoId.ToString(); + return toReturn; + } + else + return null; } public static async Task GetPlaylistIdByKeyword(string query) @@ -174,9 +183,10 @@ namespace NadekoBot.Classes $"&key={NadekoBot.Creds.GoogleAPIKey}"; var response = await GetResponseStringAsync(link).ConfigureAwait(false); - dynamic obj = JObject.Parse(response); + var data = JsonConvert.DeserializeObject(response); + JObject obj = JObject.Parse(response); - return obj.items[0].id.playlistId.ToString(); + return data.items.Length > 0 ? data.items[0].id.playlistId.ToString() : null; } public static async Task> GetVideoIDs(string playlist, int number = 50) diff --git a/NadekoBot/Modules/Music/Classes/Song.cs b/NadekoBot/Modules/Music/Classes/Song.cs index 4cf1d01d..976867e0 100644 --- a/NadekoBot/Modules/Music/Classes/Song.cs +++ b/NadekoBot/Modules/Music/Classes/Song.cs @@ -249,7 +249,7 @@ namespace NadekoBot.Modules.Music.Classes }); } var link = await SearchHelper.FindYoutubeUrlByKeywords(query).ConfigureAwait(false); - if (link == String.Empty) + if (string.IsNullOrWhiteSpace(link)) throw new OperationCanceledException("Not a valid youtube query."); var allVideos = await Task.Factory.StartNew(async () => await YouTube.Default.GetAllVideosAsync(link).ConfigureAwait(false)).Unwrap().ConfigureAwait(false); var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio); diff --git a/NadekoBot/Modules/Music/MusicModule.cs b/NadekoBot/Modules/Music/MusicModule.cs index 502410bb..2950bc1b 100644 --- a/NadekoBot/Modules/Music/MusicModule.cs +++ b/NadekoBot/Modules/Music/MusicModule.cs @@ -276,7 +276,13 @@ namespace NadekoBot.Modules.Music await e.Channel.SendMessage("💢 You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining it.").ConfigureAwait(false); return; } - var ids = await SearchHelper.GetVideoIDs(await SearchHelper.GetPlaylistIdByKeyword(arg).ConfigureAwait(false), 50).ConfigureAwait(false); + var plId = await SearchHelper.GetPlaylistIdByKeyword(arg).ConfigureAwait(false); + if (plId == null) + { + await e.Channel.SendMessage("No search results for that query."); + return; + } + var ids = await SearchHelper.GetVideoIDs(plId, 50).ConfigureAwait(false); //todo TEMPORARY SOLUTION, USE RESOLVE QUEUE IN THE FUTURE var idArray = ids as string[] ?? ids.ToArray(); var count = idArray.Count(); diff --git a/NadekoBot/Modules/Searches/SearchesModule.cs b/NadekoBot/Modules/Searches/SearchesModule.cs index 0d0429d1..82c5721c 100644 --- a/NadekoBot/Modules/Searches/SearchesModule.cs +++ b/NadekoBot/Modules/Searches/SearchesModule.cs @@ -67,8 +67,13 @@ $@"🌍 **Weather for** 【{obj["target"]}】 .Do(async e => { if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")).ConfigureAwait(false))) return; - - var shortUrl = await SearchHelper.ShortenUrl(await SearchHelper.FindYoutubeUrlByKeywords(e.GetArg("query")).ConfigureAwait(false)).ConfigureAwait(false); + var link = await SearchHelper.FindYoutubeUrlByKeywords(e.GetArg("query")).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(link)) + { + await e.Channel.SendMessage("No results found for that query."); + return; + } + var shortUrl = await SearchHelper.ShortenUrl(link).ConfigureAwait(false); await e.Channel.SendMessage(shortUrl).ConfigureAwait(false); }); diff --git a/NadekoBot/_Models/JSONModels/_JSONModels.cs b/NadekoBot/_Models/JSONModels/_JSONModels.cs index b8f9b086..8a78993a 100644 --- a/NadekoBot/_Models/JSONModels/_JSONModels.cs +++ b/NadekoBot/_Models/JSONModels/_JSONModels.cs @@ -1,4 +1,7 @@ // ReSharper disable InconsistentNaming + +using System.Diagnostics; + namespace NadekoBot.Classes.JSONModels { public class Credentials @@ -15,4 +18,78 @@ namespace NadekoBot.Classes.JSONModels public string LOLAPIKey = ""; public string CarbonKey = ""; } -} \ No newline at end of file + [DebuggerDisplay("{items[0].id.playlistId}")] + public class YoutubePlaylistSearch + { + public YtPlaylistItem[] items { get; set; } + } + public class YtPlaylistItem + { + public YtPlaylistId id { get; set; } + } + public class YtPlaylistId + { + public string kind { get; set; } + public string playlistId { get; set; } + } + [DebuggerDisplay("{items[0].id.videoId}")] + public class YoutubeVideoSearch + { + public YtVideoItem[] items { get; set; } + } + public class YtVideoItem + { + public YtVideoId id { get; set; } + } + public class YtVideoId + { + public string kind { get; set; } + public string videoId { get; set; } + } +} + +//{ +// "kind": "youtube#searchListResponse", +// "etag": "\"kiOs9cZLH2FUp6r6KJ8eyq_LIOk/hCJTmyH_v57mh_MvnUFSTHfjzBs\"", +// "nextPageToken": "CAEQAA", +// "regionCode": "RS", +// "pageInfo": { +// "totalResults": 4603, +// "resultsPerPage": 1 +// }, +// "items": [ +// { +// "kind": "youtube#searchResult", +// "etag": "\"kiOs9cZLH2FUp6r6KJ8eyq_LIOk/iD1S35mk0xOfwTB_8lpPZ9u-Vzc\"", +// "id": { +// "kind": "youtube#playlist", +// "playlistId": "PLs_KC2CCxJVMfOBnIyW5Kbu_GciNiYNAI" +// }, +// "snippet": { +// "publishedAt": "2016-04-14T11:35:29.000Z", +// "channelId": "UCMLwm18Qa20L2L-HGpgC3jQ", +// "title": "Popular Videos - Otorimonogatari & mousou express", +// "description": "", +// "thumbnails": { +// "default": { +// "url": "https://i.ytimg.com/vi/2FeptLky2mU/default.jpg", +// "width": 120, +// "height": 90 +// }, +// "medium": { +// "url": "https://i.ytimg.com/vi/2FeptLky2mU/mqdefault.jpg", +// "width": 320, +// "height": 180 +// }, +// "high": { +// "url": "https://i.ytimg.com/vi/2FeptLky2mU/hqdefault.jpg", +// "width": 480, +// "height": 360 +// } +// }, +// "channelTitle": "Otorimonogatari - Topic", +// "liveBroadcastContent": "none" +// } +// } +// ] +//} \ No newline at end of file