.q now uses youtube-dl

This commit is contained in:
Master Kwoth 2017-07-06 19:30:22 +02:00
parent 62326da5b5
commit 5d6b0f44ae
5 changed files with 49 additions and 53 deletions

View File

@ -9,7 +9,7 @@ namespace NadekoBot.DataStructures
// readpos == writepos means empty
// writepos == readpos - 1 means full
private readonly byte[] buffer;
private byte[] buffer;
public int Capacity { get; }
private int _readPos = 0;
@ -110,7 +110,7 @@ namespace NadekoBot.DataStructures
public void Dispose()
{
buffer = null;
}
}
}

View File

@ -74,6 +74,7 @@
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="NLog" Version="5.0.0-beta03" />
<PackageReference Include="NYoutubeDL" Version="0.4.4" />
<PackageReference Include="System.ValueTuple" Version="4.4.0-preview1-25305-02" />
<PackageReference Include="System.Xml.XPath" Version="4.3.0" />
</ItemGroup>

View File

@ -42,16 +42,17 @@ namespace NadekoBot.Services.Impl
sh = new UrlshortenerService(bcs);
cs = new CustomsearchService(bcs);
}
private static readonly Regex plRegex = new Regex("(?:youtu\\.be\\/|list=)(?<id>[\\da-zA-Z\\-_]*)", RegexOptions.Compiled);
public async Task<IEnumerable<string>> GetPlaylistIdsByKeywordsAsync(string keywords, int count = 1)
{
await Task.Yield();
if (string.IsNullOrWhiteSpace(keywords))
throw new ArgumentNullException(nameof(keywords));
if (count <= 0)
throw new ArgumentOutOfRangeException(nameof(count));
var match = new Regex("(?:youtu\\.be\\/|list=)(?<id>[\\da-zA-Z\\-_]*)").Match(keywords);
var match = plRegex.Match(keywords);
if (match.Length > 1)
{
return new[] { match.Groups["id"].Value.ToString() };
@ -64,22 +65,17 @@ namespace NadekoBot.Services.Impl
return (await query.ExecuteAsync()).Items.Select(i => i.Id.PlaylistId);
}
private readonly Regex YtVideoIdRegex = new Regex(@"(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|youtu\.be\/)(?<id>[a-zA-Z0-9_-]{6,11})", RegexOptions.Compiled);
//private readonly Regex YtVideoIdRegex = new Regex(@"(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|youtu\.be\/)(?<id>[a-zA-Z0-9_-]{6,11})", RegexOptions.Compiled);
private readonly IBotCredentials _creds;
public async Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1)
{
await Task.Yield();
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
if (count <= 0)
throw new ArgumentOutOfRangeException(nameof(count));
var match = YtVideoIdRegex.Match(id);
if (match.Length > 1)
{
id = match.Groups["id"].Value;
}
var query = yt.Search.List("snippet");
query.MaxResults = count;
query.RelatedToVideoId = id;
@ -89,22 +85,13 @@ namespace NadekoBot.Services.Impl
public async Task<IEnumerable<string>> GetVideoLinksByKeywordAsync(string keywords, int count = 1)
{
await Task.Yield();
if (string.IsNullOrWhiteSpace(keywords))
throw new ArgumentNullException(nameof(keywords));
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;
@ -114,6 +101,7 @@ namespace NadekoBot.Services.Impl
public async Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync(string keywords, int count = 1)
{
await Task.Yield();
if (string.IsNullOrWhiteSpace(keywords))
throw new ArgumentNullException(nameof(keywords));
@ -129,6 +117,7 @@ namespace NadekoBot.Services.Impl
public async Task<string> ShortenUrl(string url)
{
await Task.Yield();
if (string.IsNullOrWhiteSpace(url))
throw new ArgumentNullException(nameof(url));
@ -149,6 +138,7 @@ namespace NadekoBot.Services.Impl
public async Task<IEnumerable<string>> GetPlaylistTracksAsync(string playlistId, int count = 50)
{
await Task.Yield();
if (string.IsNullOrWhiteSpace(playlistId))
throw new ArgumentNullException(nameof(playlistId));
@ -181,6 +171,7 @@ namespace NadekoBot.Services.Impl
public async Task<IReadOnlyDictionary<string, TimeSpan>> GetVideoDurationsAsync(IEnumerable<string> videoIds)
{
await Task.Yield();
var videoIdsList = videoIds as List<string> ?? videoIds.ToList();
Dictionary<string, TimeSpan> toReturn = new Dictionary<string, TimeSpan>();
@ -211,6 +202,7 @@ namespace NadekoBot.Services.Impl
public async Task<ImageResult> GetImageAsync(string query, int start = 1)
{
await Task.Yield();
if (string.IsNullOrWhiteSpace(query))
throw new ArgumentNullException(nameof(query));
@ -362,6 +354,7 @@ namespace NadekoBot.Services.Impl
public async Task<string> Translate(string sourceText, string sourceLanguage, string targetLanguage)
{
await Task.Yield();
string text;
if (!_languageDictionary.ContainsKey(sourceLanguage) ||

View File

@ -161,7 +161,7 @@ namespace NadekoBot.Services.Music
continue;
_log.Info("Starting");
using (var b = new SongBuffer(await data.Song.Uri(), ""))
using (var b = new SongBuffer(await data.Song.Uri(), "", data.Song.ProviderType == Database.Models.MusicType.Local))
{
_log.Info("Created buffer, buffering...");
AudioOutStream pcm = null;

View File

@ -73,36 +73,38 @@ namespace NadekoBot
{
_log.Error(ex);
}
await Task.Run(() =>
{
string input;
while ((input = Console.ReadLine()?.ToLowerInvariant()) != "quit")
{
try
{
switch (input)
{
case "ls":
var groupStr = string.Join(",", Statuses
.ToArray()
.Where(x => x != null)
.GroupBy(x => x.ConnectionState)
.Select(x => x.Count() + " " + x.Key));
_log.Info(string.Join("\n", Statuses
.ToArray()
.Where(x => x != null)
.Select(x => $"Shard {x.ShardId} is in {x.ConnectionState.ToString()} state with {x.Guilds} servers. {(DateTime.UtcNow - x.Time).ToString(@"hh\:mm\:ss")} ago")) + "\n" + groupStr);
break;
default:
break;
}
}
catch (Exception ex)
{
_log.Warn(ex);
}
}
});
//await Task.Run(() =>
//{
// string input;
// while ((input = Console.ReadLine()?.ToLowerInvariant()) != "quit")
// {
// try
// {
// switch (input)
// {
// case "ls":
// var groupStr = string.Join(",", Statuses
// .ToArray()
// .Where(x => x != null)
// .GroupBy(x => x.ConnectionState)
// .Select(x => x.Count() + " " + x.Key));
// _log.Info(string.Join("\n", Statuses
// .ToArray()
// .Where(x => x != null)
// .Select(x => $"Shard {x.ShardId} is in {x.ConnectionState.ToString()} state with {x.Guilds} servers. {(DateTime.UtcNow - x.Time).ToString(@"hh\:mm\:ss")} ago")) + "\n" + groupStr);
// break;
// default:
// break;
// }
// }
// catch (Exception ex)
// {
// _log.Warn(ex);
// }
// }
//});
await Task.Delay(-1);
foreach (var p in ShardProcesses)
{
try { p.Kill(); } catch { }