Files

132 lines
4.7 KiB
C#
Raw Permalink Normal View History

2026-02-26 22:54:53 -05:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SabreTools.CommandLine.Inputs;
2026-03-03 15:20:27 -05:00
using SabreTools.RedumpLib.Data;
2026-02-26 22:54:53 -05:00
using SabreTools.RedumpLib.Web;
namespace RedumpTool.Features
{
internal sealed class QueryFeature : BaseFeature
{
#region Feature Definition
public const string DisplayName = "query";
private static readonly string[] _flags = ["query"];
private const string _description = "Download pages and related files from a Redump-compatible quicksearch query";
2026-02-26 22:54:53 -05:00
#endregion
#region Inputs
private const string _limitName = "limit";
internal readonly Int32Input LimitInput = new(_limitName, ["--limit"], "Limit number of retrieved result pages");
2026-02-26 22:54:53 -05:00
private const string _listName = "list";
internal readonly FlagInput ListInput = new(_listName, ["-l", "--list"], "Only list the page IDs for that query");
2026-03-03 15:20:27 -05:00
private const string _onlyFilesName = "onlypages";
internal readonly FlagInput OnlyFilesInput = new(_onlyFilesName, ["--only-files"], "Only download disc file attachments (incompatible with --only-pages)");
private const string _onlyPagesName = "onlypages";
internal readonly FlagInput OnlyPagesInput = new(_onlyPagesName, ["--only-pages"], "Only download disc subpages (incompatible with --only-files)");
2026-02-26 22:54:53 -05:00
private const string _queryName = "query";
internal readonly StringInput QueryInput = new(_queryName, ["-q", "--query"], "Redump-compatible query to run");
#endregion
public QueryFeature()
: base(DisplayName, _flags, _description)
{
RequiresInputs = false;
// Common
Add(DebugInput);
Add(OutputInput);
Add(UsernameInput);
Add(PasswordInput);
Add(AttemptCountInput);
2026-02-27 09:58:05 -05:00
Add(TimeoutInput);
Add(ForceDownloadInput);
Add(ForceContinueInput);
2026-02-26 22:54:53 -05:00
// Specific
Add(QueryInput);
Add(ListInput);
Add(LimitInput);
2026-03-03 15:20:27 -05:00
Add(OnlyPagesInput);
Add(OnlyFilesInput);
2026-02-26 22:54:53 -05:00
}
/// <inheritdoc/>
public override bool Execute()
{
2026-02-27 11:14:05 -05:00
// Get common values
string? outDir = OutputInput.Value;
string? username = UsernameInput.Value;
string? password = PasswordInput.Value;
int? attemptCount = AttemptCountInput.Value;
2026-02-27 09:58:05 -05:00
int? timeout = TimeoutInput.Value;
bool forceDownload = ForceDownloadInput.Value;
bool forceContinue = ForceContinueInput.Value;
2026-02-27 11:14:05 -05:00
// Get specific values
bool onlyList = ListInput.Value;
string? query = QueryInput.Value;
int limit = LimitInput.Value ?? -1;
2026-03-03 15:20:27 -05:00
bool onlyPages = OnlyPagesInput.Value;
bool onlyFiles = OnlyFilesInput.Value;
// Build the disc subpaths
DiscSubpath[]? discSubpaths = Constants.AllDiscSubpaths;
if (onlyPages)
discSubpaths = Constants.DiscSubPagesOnly;
else if (onlyFiles)
discSubpaths = Constants.DiscFilesOnly;
2026-02-26 22:54:53 -05:00
// Output directory validation
2026-02-27 11:14:05 -05:00
if (!onlyList && !ValidateAndCreateOutputDirectory(outDir))
2026-02-26 22:54:53 -05:00
return false;
// Query verification (and cleanup)
if (string.IsNullOrEmpty(query))
2026-02-26 22:54:53 -05:00
{
Console.Error.WriteLine("Please enter a query for searching");
return false;
}
2026-02-27 08:56:13 -05:00
// Update client properties
2026-02-26 23:04:25 -05:00
_client.Debug = DebugInput.Value;
if (attemptCount != null && attemptCount > 0)
_client.AttemptCount = attemptCount.Value;
2026-02-27 09:58:05 -05:00
if (timeout != null && timeout > 0)
_client.Timeout = TimeSpan.FromSeconds(timeout.Value);
2026-03-01 11:56:08 -05:00
_client.Overwrite = forceDownload;
_client.IgnoreErrors = forceContinue;
2026-02-26 23:04:25 -05:00
2026-02-27 11:32:43 -05:00
// Login to Redump, if necessary
2026-02-28 11:06:08 -05:00
_client.Login(username, password).Wait();
2026-02-27 11:32:43 -05:00
2026-02-26 22:54:53 -05:00
// Start the processing
Task<List<int>> processingTask;
if (onlyList)
processingTask = _client.ListDiscsResults(quicksearch: query, limit: limit);
2026-02-26 22:54:53 -05:00
else
2026-03-03 15:20:27 -05:00
processingTask = _client.DownloadDiscsResults(outDir, quicksearch: query, limit: limit, discSubpaths: discSubpaths);
2026-02-26 22:54:53 -05:00
// Retrieve the result
processingTask.Wait();
var processedIds = processingTask.Result;
// Display the processed IDs
return PrintProcessedIds(processedIds);
}
/// <inheritdoc/>
public override bool VerifyInputs() => true;
}
}