Some fixes to yt search

This commit is contained in:
Kwoth 2016-12-13 05:19:33 +01:00
parent bef982616b
commit b97e49dccf

View File

@ -51,6 +51,8 @@ namespace NadekoBot.Services.Impl
return (await query.ExecuteAsync()).Items.Select(i => i.Id.PlaylistId); 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=)(?<id>[^#\\&\\?]*)", RegexOptions.Compiled);
public async Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1) public async Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1)
{ {
if (string.IsNullOrWhiteSpace(id)) if (string.IsNullOrWhiteSpace(id))
@ -59,7 +61,7 @@ namespace NadekoBot.Services.Impl
if (count <= 0) if (count <= 0)
throw new ArgumentOutOfRangeException(nameof(count)); throw new ArgumentOutOfRangeException(nameof(count));
var match = new Regex("(?:youtu\\.be\\/|v=)(?<id>[\\da-zA-Z\\-_]*)").Match(id); var match = YtVideoIdRegex.Match(id);
if (match.Length > 1) if (match.Length > 1)
{ {
id = match.Groups["id"].Value; id = match.Groups["id"].Value;
@ -79,6 +81,16 @@ namespace NadekoBot.Services.Impl
if (count <= 0) if (count <= 0)
throw new ArgumentOutOfRangeException(nameof(count)); 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"); var query = yt.Search.List("snippet");
query.MaxResults = count; query.MaxResults = count;
query.Q = keywords; query.Q = keywords;