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;