From b97e49dccf410dbe9d0f1ec66965f9c74d7446d4 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 13 Dec 2016 05:19:33 +0100 Subject: [PATCH] Some fixes to yt search --- src/NadekoBot/Services/Impl/GoogleApiService.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/NadekoBot/Services/Impl/GoogleApiService.cs b/src/NadekoBot/Services/Impl/GoogleApiService.cs index cb401476..6a18c686 100644 --- a/src/NadekoBot/Services/Impl/GoogleApiService.cs +++ b/src/NadekoBot/Services/Impl/GoogleApiService.cs @@ -51,6 +51,8 @@ namespace NadekoBot.Services.Impl return (await query.ExecuteAsync()).Items.Select(i => i.Id.PlaylistId); } + private readonly Regex YtVideoIdRegex = new Regex("(?:youtu\\.be\\/|v\\/|u\\/\\w\\/|embed\\/|watch\\?v=|\\&v=)(?[^#\\&\\?]*)", RegexOptions.Compiled); + public async Task> GetRelatedVideosAsync(string id, int count = 1) { if (string.IsNullOrWhiteSpace(id)) @@ -59,7 +61,7 @@ namespace NadekoBot.Services.Impl if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); - var match = new Regex("(?:youtu\\.be\\/|v=)(?[\\da-zA-Z\\-_]*)").Match(id); + var match = YtVideoIdRegex.Match(id); if (match.Length > 1) { id = match.Groups["id"].Value; @@ -79,6 +81,16 @@ namespace NadekoBot.Services.Impl if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); + string id = ""; + var match = YtVideoIdRegex.Match(keywords); + if (match.Length > 1) + { + id = match.Groups["id"].Value; + } + if (!string.IsNullOrWhiteSpace(id)) + { + return new[] { "http://www.youtube.com/watch?v=" + id }; + } var query = yt.Search.List("snippet"); query.MaxResults = count; query.Q = keywords;