From 7f007db197a858a5c034a53c0147ad71fa215216 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Sat, 10 Dec 2016 15:07:35 -0800 Subject: [PATCH] Query youtube-dl version --- src/Podsync/Controllers/StatusController.cs | 8 +++- .../Services/Resolver/IResolverService.cs | 2 + src/Podsync/Services/Resolver/YtdlWrapper.cs | 48 +++++++++++++++---- .../Services/Resolver/YtdlWrapperTests.cs | 6 +++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/Podsync/Controllers/StatusController.cs b/src/Podsync/Controllers/StatusController.cs index eb22e38..bf40a60 100644 --- a/src/Podsync/Controllers/StatusController.cs +++ b/src/Podsync/Controllers/StatusController.cs @@ -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 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}"; } } } \ No newline at end of file diff --git a/src/Podsync/Services/Resolver/IResolverService.cs b/src/Podsync/Services/Resolver/IResolverService.cs index 48e2dc8..ae5a5ce 100644 --- a/src/Podsync/Services/Resolver/IResolverService.cs +++ b/src/Podsync/Services/Resolver/IResolverService.cs @@ -5,6 +5,8 @@ namespace Podsync.Services.Resolver { public interface IResolverService { + string Version { get; } + Task Resolve(Uri videoUrl, ResolveType resolveType = ResolveType.VideoHigh); } } \ No newline at end of file diff --git a/src/Podsync/Services/Resolver/YtdlWrapper.cs b/src/Podsync/Services/Resolver/YtdlWrapper.cs index 716674e..d5c3b80 100644 --- a/src/Podsync/Services/Resolver/YtdlWrapper.cs +++ b/src/Podsync/Services/Resolver/YtdlWrapper.cs @@ -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 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) diff --git a/test/Podsync.Tests/Services/Resolver/YtdlWrapperTests.cs b/test/Podsync.Tests/Services/Resolver/YtdlWrapperTests.cs index e1734de..44da90b 100644 --- a/test/Podsync.Tests/Services/Resolver/YtdlWrapperTests.cs +++ b/test/Podsync.Tests/Services/Resolver/YtdlWrapperTests.cs @@ -27,5 +27,11 @@ namespace Podsync.Tests.Services.Resolver var ex = await Assert.ThrowsAsync(async () => await _resolver.Resolve(new Uri(url))); Assert.NotEmpty(ex.Message); } + + [Fact] + public void VersionTest() + { + Assert.NotNull(_resolver.Version); + } } } \ No newline at end of file