Skip to content

Commit

Permalink
Improve handling of hidden videos (#70)
Browse files Browse the repository at this point in the history
* Initial working version

* Minor cleanup

* Add `TrimTerminalLineBreak()` extension method

* Trim errors separately

* Create new method for exit code check

* Add comment about errors on success
  • Loading branch information
codeconscious authored Oct 25, 2024
1 parent d7ce2b9 commit eebbb61
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
34 changes: 29 additions & 5 deletions src/CCVTAC.Console/Downloading/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,36 @@ internal static Result<MediaTypeWithUrls> WrapUrlInMediaType(string url)

if (!mediaType.IsVideo && !mediaType.IsPlaylistVideo)
{
printer.Info("Please wait for the multiple videos to be downloaded...");
printer.Info("Please wait for multiple videos to be downloaded...");
}

var rawUrls = FSharp.Downloading.ExtractDownloadUrls(mediaType);
var urls = new Urls(rawUrls[0], rawUrls.Length == 2 ? rawUrls[1] : null);

Result downloadResult = new();
var downloadResult = new Result<(int, string)> ();
string? successfulFormat = null;

foreach (string format in settings.AudioFormats)
{
string args = GenerateDownloadArgs(format, settings, mediaType, urls.Primary);
var downloadSettings = new ToolSettings(ExternalTool, args, settings.WorkingDirectory!);

downloadResult = Runner.Run(downloadSettings, printer);
downloadResult = Runner.Run(downloadSettings, otherSuccessExitCodes: [1], printer);

if (downloadResult.IsSuccess)
{
successfulFormat = format;

var (exitCode, warnings) = downloadResult.Value;
if (exitCode != 0)
{
printer.Warning($"Downloading completed with minor issues.");
if (warnings.HasText())
{
printer.Warning(warnings);
}
}

break;
}

Expand All @@ -60,7 +71,16 @@ internal static Result<MediaTypeWithUrls> WrapUrlInMediaType(string url)

var errors = downloadResult.Errors.Select(e => e.Message).ToList();

if (downloadResult.IsFailed)
int audioFileCount = IoUtilties.Directories.AudioFileCount(settings.WorkingDirectory);
if (audioFileCount == 0)
{
return Result.Fail(string.Join(
Environment.NewLine,
["No audio files were downloaded.", ..errors])
);
}

if (errors.Count != 0)
{
downloadResult.Errors.ForEach(e => printer.Error(e.Message));
printer.Info("Post-processing will still be attempted."); // For any partial downloads
Expand All @@ -75,7 +95,11 @@ internal static Result<MediaTypeWithUrls> WrapUrlInMediaType(string url)
supplementaryArgs,
settings.WorkingDirectory!);

var supplementaryDownloadResult = Runner.Run(supplementaryDownloadSettings, printer);
var supplementaryDownloadResult =
Runner.Run(
supplementaryDownloadSettings,
otherSuccessExitCodes: [1],
printer);

if (supplementaryDownloadResult.IsSuccess)
{
Expand Down
4 changes: 4 additions & 0 deletions src/CCVTAC.Console/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ public static string ReplaceInvalidPathChars(
);
}

public static string TrimTerminalLineBreak(this string text) =>
text.HasText()
? text.TrimEnd(Environment.NewLine.ToCharArray())
: text;
}
32 changes: 21 additions & 11 deletions src/CCVTAC.Console/ExternalTools/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@ namespace CCVTAC.Console.ExternalTools;

internal static class Runner
{
internal static Result Run(ToolSettings settings, Printer printer)
private const int AuthenticSuccessExitCode = 0;

private static bool IsSuccessExitCode(HashSet<int> otherSuccessExitCodes, int exitCode) =>
otherSuccessExitCodes.Append(AuthenticSuccessExitCode).Contains(exitCode);

/// <summary>
/// Calls an external application.
/// </summary>
/// <param name="settings"></param>
/// <param name="otherSuccessExitCodes">Additional exit codes, other than 0, that can be treated as non-failures.</param>
/// <param name="printer"></param>
/// <returns>A `Result` containing the exit code, if successful, or else an error message.</returns>
internal static Result<(int SuccessExitCode, string Warnings)> Run(
ToolSettings settings,
HashSet<int> otherSuccessExitCodes,
Printer printer)
{
Watch watch = new();

Expand All @@ -29,18 +44,13 @@ internal static Result Run(ToolSettings settings, Printer printer)
return Result.Fail($"Could not locate {settings.Program.Name}. If it's not installed, please install from {settings.Program.Url}.");
}


string error = process.StandardError.ReadToEnd(); // Must precede `WaitForExit()`.
string errors = process.StandardError.ReadToEnd(); // Must precede `WaitForExit()`
process.WaitForExit();

printer.Info($"Completed {settings.Program.Purpose} in {watch.ElapsedFriendly}.");

int exitCode = process.ExitCode;
if (exitCode == 0)
{
return Result.Ok();
}

return Result.Fail($"[{settings.Program.Name}] {error.Replace(Environment.NewLine, string.Empty)}.");
var trimmedErrors = errors.TrimTerminalLineBreak();
return IsSuccessExitCode(otherSuccessExitCodes ?? [], process.ExitCode)
? Result.Ok((process.ExitCode, trimmedErrors)) // Errors will be considered warnings.
: Result.Fail($"[{settings.Program.Name}] Exit code {process.ExitCode}: {trimmedErrors}.");
}
}
8 changes: 8 additions & 0 deletions src/CCVTAC.Console/IoUtilties/Directories.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Text;
using CCVTAC.Console.PostProcessing;

namespace CCVTAC.Console.IoUtilties;

Expand All @@ -8,6 +9,13 @@ internal static class Directories
private static readonly string AllFilesSearchPattern = "*";
private static readonly EnumerationOptions EnumerationOptions = new();

internal static int AudioFileCount(string directory)
{
return new DirectoryInfo(directory)
.EnumerateFiles()
.Count(f => PostProcessor.AudioExtensions.CaseInsensitiveContains(f.Extension));
}

internal static Result WarnIfAnyFiles(string directory, int showMax)
{
var fileNames = GetDirectoryFileNames(directory);
Expand Down
2 changes: 1 addition & 1 deletion src/CCVTAC.Console/PostProcessing/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ internal static void Run(string workingDirectory, Printer printer)
workingDirectory
);

Runner.Run(imageEditToolSettings, printer);
Runner.Run(imageEditToolSettings, [], printer);
}
}

0 comments on commit eebbb61

Please sign in to comment.