Better youtube searches (no errors)

This commit is contained in:
Master Kwoth 2016-05-03 05:57:15 +02:00
parent 5e4d1ebcf5
commit 081cb44dac
5 changed files with 107 additions and 9 deletions

View File

@ -159,8 +159,17 @@ namespace NadekoBot.Classes
$"part=snippet&maxResults=1" + $"part=snippet&maxResults=1" +
$"&q={Uri.EscapeDataString(keywords)}" + $"&q={Uri.EscapeDataString(keywords)}" +
$"&key={NadekoBot.Creds.GoogleAPIKey}").ConfigureAwait(false); $"&key={NadekoBot.Creds.GoogleAPIKey}").ConfigureAwait(false);
dynamic obj = JObject.Parse(response); JObject obj = JObject.Parse(response);
return "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString();
var data = JsonConvert.DeserializeObject<YoutubeVideoSearch>(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<string> GetPlaylistIdByKeyword(string query) public static async Task<string> GetPlaylistIdByKeyword(string query)
@ -174,9 +183,10 @@ namespace NadekoBot.Classes
$"&key={NadekoBot.Creds.GoogleAPIKey}"; $"&key={NadekoBot.Creds.GoogleAPIKey}";
var response = await GetResponseStringAsync(link).ConfigureAwait(false); var response = await GetResponseStringAsync(link).ConfigureAwait(false);
dynamic obj = JObject.Parse(response); var data = JsonConvert.DeserializeObject<YoutubePlaylistSearch>(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<IEnumerable<string>> GetVideoIDs(string playlist, int number = 50) public static async Task<IEnumerable<string>> GetVideoIDs(string playlist, int number = 50)

View File

@ -249,7 +249,7 @@ namespace NadekoBot.Modules.Music.Classes
}); });
} }
var link = await SearchHelper.FindYoutubeUrlByKeywords(query).ConfigureAwait(false); var link = await SearchHelper.FindYoutubeUrlByKeywords(query).ConfigureAwait(false);
if (link == String.Empty) if (string.IsNullOrWhiteSpace(link))
throw new OperationCanceledException("Not a valid youtube query."); 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 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); var videos = allVideos.Where(v => v.AdaptiveKind == AdaptiveKind.Audio);

View File

@ -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); 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; 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 //todo TEMPORARY SOLUTION, USE RESOLVE QUEUE IN THE FUTURE
var idArray = ids as string[] ?? ids.ToArray(); var idArray = ids as string[] ?? ids.ToArray();
var count = idArray.Count(); var count = idArray.Count();

View File

@ -67,8 +67,13 @@ $@"🌍 **Weather for** 【{obj["target"]}】
.Do(async e => .Do(async e =>
{ {
if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")).ConfigureAwait(false))) return; if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")).ConfigureAwait(false))) return;
var link = await SearchHelper.FindYoutubeUrlByKeywords(e.GetArg("query")).ConfigureAwait(false);
var shortUrl = await SearchHelper.ShortenUrl(await SearchHelper.FindYoutubeUrlByKeywords(e.GetArg("query")).ConfigureAwait(false)).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); await e.Channel.SendMessage(shortUrl).ConfigureAwait(false);
}); });

View File

@ -1,4 +1,7 @@
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
using System.Diagnostics;
namespace NadekoBot.Classes.JSONModels namespace NadekoBot.Classes.JSONModels
{ {
public class Credentials public class Credentials
@ -15,4 +18,78 @@ namespace NadekoBot.Classes.JSONModels
public string LOLAPIKey = ""; public string LOLAPIKey = "";
public string CarbonKey = ""; public string CarbonKey = "";
} }
[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"
// }
// }
// ]
//}