Ejemplo n.º 1
0
        public void GetValid(string url, Action <UrlValidationResponse, Exception> callback)
        {
            try
            {
                var startInfo = new ProcessStartInfo(YoutubeDlFilename)
                {
                    Arguments              = $"--ignore-errors --get-url {url}",
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true
                };

                UrlValidationResult validationResult = null;
                using (var ydlProcess = Process.Start(startInfo))
                {
                    if (ydlProcess == null)
                    {
                        callback(null, new Exception("Cannot start validation process."));
                        return;
                    }
                    var output = ydlProcess.StandardOutput;
                    while (!output.EndOfStream)
                    {
                        // Read first line
                        var outputLine = output.ReadLine();
                        validationResult = new UrlValidationResult
                        {
                            IsValid = outputLine.IsValidUrl(),
                            Url     = url
                        };
                        // First line should tell everything, so let's close the process.
                        ydlProcess.Close();
                    }
                }

                if (validationResult == null)
                {
                    validationResult = new UrlValidationResult
                    {
                        IsValid = false,
                        Url     = url
                    }
                }
                ;

                var response = new UrlValidationResponse
                {
                    ValidationResults = new[] { validationResult }
                };
                callback(response, null);
            }
            catch (Exception ex)
            {
                callback(null, ex);
            }
        }
Ejemplo n.º 2
0
     private static void ValidateValidationResults(UrlValidationResponse response, string url)
     {
         if ((response.ValidationResults == null) || (response.ValidationResults.Length == 0))
         {
             response.ValidationResults = new[]
             {
                 new UrlValidationResult
                 {
                     Url     = url,
                     IsValid = false
                 }
             }
         }
         ;
     }
 }