Files
nadekobot/src/NadekoBot/Services/Impl/YtdlOperation.cs
Kwoth d5fd6aae8e - More code cleanup and codestyle updates
- Fixed some possible nullref exceptions
- Methods signatures now have up to 3 parameters before breakaing down each parameter in a separate line
- Method invocations have the same rule, except the first parameter will be in the same line as the invocation to prevent some ugliness when passing lambas as arguments
- Applied many more codestyles
- Extensions folder fully reformatted
2021-12-26 17:28:39 +01:00

73 lines
2.2 KiB
C#

using System.ComponentModel;
using System.Diagnostics;
using System.Text;
namespace NadekoBot.Services;
public class YtdlOperation
{
private readonly string _baseArgString;
public YtdlOperation(string baseArgString)
=> _baseArgString = baseArgString;
private Process CreateProcess(string[] args)
{
args = args.Map(arg => arg.Replace("\"", ""));
return new()
{
StartInfo = new()
{
FileName = "youtube-dl",
Arguments = string.Format(_baseArgString, args),
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
CreateNoWindow = true,
},
};
}
public async Task<string> GetDataAsync(params string[] args)
{
try
{
using var process = CreateProcess(args);
Log.Debug($"Executing {process.StartInfo.FileName} {process.StartInfo.Arguments}");
process.Start();
var str = await process.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
var err = await process.StandardError.ReadToEndAsync().ConfigureAwait(false);
if (!string.IsNullOrEmpty(err))
Log.Warning("YTDL warning: {YtdlWarning}", err);
return str;
}
catch (Win32Exception)
{
Log.Error("youtube-dl is likely not installed. " +
"Please install it before running the command again");
return default;
}
catch (Exception ex)
{
Log.Error(ex , "Exception running youtube-dl: {ErrorMessage}", ex.Message);
return default;
}
}
public async IAsyncEnumerable<string> EnumerateDataAsync(params string[] args)
{
using var process = CreateProcess(args);
Log.Debug($"Executing {process.StartInfo.FileName} {process.StartInfo.Arguments}");
process.Start();
string line;
while((line = await process.StandardOutput.ReadLineAsync()) != null)
yield return line;
}
}