mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Query youtube-dl version
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Podsync.Services.Resolver;
|
||||
using Podsync.Services.Storage;
|
||||
|
||||
namespace Podsync.Controllers
|
||||
@@ -7,10 +8,12 @@ namespace Podsync.Controllers
|
||||
public class StatusController : Controller
|
||||
{
|
||||
private readonly IStorageService _storageService;
|
||||
private readonly IResolverService _resolverService;
|
||||
|
||||
public StatusController(IStorageService storageService)
|
||||
public StatusController(IStorageService storageService, IResolverService resolverService)
|
||||
{
|
||||
_storageService = storageService;
|
||||
_resolverService = resolverService;
|
||||
}
|
||||
|
||||
public async Task<string> Index()
|
||||
@@ -18,7 +21,8 @@ namespace Podsync.Controllers
|
||||
var time = await _storageService.Ping();
|
||||
|
||||
return $"Path: {Request.Path}\r\n" +
|
||||
$"Redis: {time}";
|
||||
$"Redis: {time}\r\n" +
|
||||
$"Resolve: {_resolverService.Version}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ namespace Podsync.Services.Resolver
|
||||
{
|
||||
public interface IResolverService
|
||||
{
|
||||
string Version { get; }
|
||||
|
||||
Task<Uri> Resolve(Uri videoUrl, ResolveType resolveType = ResolveType.VideoHigh);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Podsync.Services.Resolver
|
||||
@@ -8,22 +9,37 @@ namespace Podsync.Services.Resolver
|
||||
{
|
||||
private static readonly int WaitTimeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
|
||||
|
||||
public YtdlWrapper()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var proc = new Process())
|
||||
{
|
||||
FillStartInfo(proc.StartInfo, "--version");
|
||||
|
||||
proc.Start();
|
||||
proc.WaitForExit(WaitTimeout);
|
||||
|
||||
var stdout = proc.StandardOutput.ReadToEndAsync().GetAwaiter().GetResult();
|
||||
Version = stdout;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new FileNotFoundException("Failed to execute youtube-dl executable", "youtube-dl", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public string Version { get; }
|
||||
|
||||
|
||||
public async Task<Uri> Resolve(Uri videoUrl, ResolveType resolveType)
|
||||
{
|
||||
var format = SelectFormat(resolveType);
|
||||
|
||||
using (var proc = new Process())
|
||||
{
|
||||
var startInfo = proc.StartInfo;
|
||||
|
||||
startInfo.FileName = "youtube-dl";
|
||||
startInfo.Arguments = $"-f {format} -g {videoUrl}";
|
||||
|
||||
startInfo.UseShellExecute = false;
|
||||
startInfo.CreateNoWindow = true;
|
||||
|
||||
startInfo.RedirectStandardOutput = true;
|
||||
startInfo.RedirectStandardError = true;
|
||||
FillStartInfo(proc.StartInfo, $"-f {format} -g {videoUrl}");
|
||||
|
||||
proc.Start();
|
||||
|
||||
@@ -45,6 +61,18 @@ namespace Podsync.Services.Resolver
|
||||
}
|
||||
}
|
||||
|
||||
private static void FillStartInfo(ProcessStartInfo startInfo, string arguments)
|
||||
{
|
||||
startInfo.FileName = "youtube-dl";
|
||||
startInfo.Arguments = arguments;
|
||||
|
||||
startInfo.UseShellExecute = false;
|
||||
startInfo.CreateNoWindow = true;
|
||||
|
||||
startInfo.RedirectStandardOutput = true;
|
||||
startInfo.RedirectStandardError = true;
|
||||
}
|
||||
|
||||
private static string SelectFormat(ResolveType resolveType)
|
||||
{
|
||||
switch (resolveType)
|
||||
|
||||
@@ -27,5 +27,11 @@ namespace Podsync.Tests.Services.Resolver
|
||||
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => await _resolver.Resolve(new Uri(url)));
|
||||
Assert.NotEmpty(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VersionTest()
|
||||
{
|
||||
Assert.NotNull(_resolver.Version);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user