Skip to content

Commit 92a220a

Browse files
committed
Add discs support next to quicksearch
1 parent 4b6fc09 commit 92a220a

4 files changed

Lines changed: 119 additions & 3 deletions

File tree

RedumpTool/Features/QueryFeature.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ internal sealed class QueryFeature : BaseFeature
2929
private const string _queryName = "query";
3030
internal readonly StringInput QueryInput = new(_queryName, ["-q", "--query"], "Redump-compatible query to run");
3131

32+
private const string _quickSearchName = "quicksearch";
33+
internal readonly FlagInput QuickSearchInput = new(_quickSearchName, ["-s", "--quick"], "Indicate a query is for the 'quicksearch' path, not 'discs'");
34+
3235
#endregion
3336

3437
public QueryFeature()
@@ -45,6 +48,7 @@ public QueryFeature()
4548

4649
// Specific
4750
Add(QueryInput);
51+
Add(QuickSearchInput);
4852
Add(ListInput);
4953
Add(NoSlashInput);
5054
}
@@ -57,6 +61,7 @@ public override bool Execute()
5761
string? outputDirectory = OutputInput.Value;
5862
string? queryString = QueryInput.Value;
5963
int? attemptCount = AttemptCountInput.Value;
64+
bool quick = QuickSearchInput.Value;
6065

6166
// Output directory validation
6267
if (!onlyList && !ValidateAndCreateOutputDirectory(outputDirectory))
@@ -80,10 +85,20 @@ public override bool Execute()
8085

8186
// Start the processing
8287
Task<List<int>> processingTask;
83-
if (onlyList)
84-
processingTask = _client.ListSearchResults(queryString, NoSlashInput.Value);
88+
if (quick)
89+
{
90+
if (onlyList)
91+
processingTask = _client.ListSearchResults(queryString, NoSlashInput.Value);
92+
else
93+
processingTask = _client.DownloadSearchResults(queryString, outputDirectory, NoSlashInput.Value);
94+
}
8595
else
86-
processingTask = _client.DownloadSearchResults(queryString, outputDirectory, NoSlashInput.Value);
96+
{
97+
if (onlyList)
98+
processingTask = _client.ListDiscsResults(queryString, NoSlashInput.Value);
99+
else
100+
processingTask = _client.DownloadDiscsResults(queryString, outputDirectory, NoSlashInput.Value);
101+
}
87102

88103
// Retrieve the result
89104
processingTask.Wait();

RedumpTool/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private static void ShowHelp()
9494
Console.WriteLine();
9595
Console.WriteLine("query - Download pages and related files from a Redump-compatible query");
9696
Console.WriteLine(" -q, --query - Redump-compatible query to run");
97+
Console.WriteLine(" -s, --quick - Indicate a query is for the 'quicksearch' path, not 'discs'");
9798
Console.WriteLine(" -l, --list - Only list the page IDs for that query");
9899
Console.WriteLine(" -ns, --noslash - Don't replace forward slashes with '-'");
99100
Console.WriteLine();

SabreTools.RedumpLib/Data/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public static class Constants
180180
/// </summary>
181181
public const string DiscPageUrl = @"http://redump.org/disc/{0}/";
182182

183+
/// <summary>
184+
/// Redump discs path URL template
185+
/// </summary>
186+
public const string DiscsUrl = @"http://redump.org/discs/{0}";
187+
183188
/// <summary>
184189
/// Redump last modified search URL template
185190
/// </summary>

SabreTools.RedumpLib/Web/Discs.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,101 @@ namespace SabreTools.RedumpLib.Web
1010
/// </summary>
1111
public static class Discs
1212
{
13+
/// <summary>
14+
/// List the disc IDs associated with a given discs query
15+
/// </summary>
16+
/// <param name="rc">RedumpClient for connectivity</param>
17+
/// <param name="query">Query string to attempt to search for</param>
18+
/// <param name="noSlash">Don't replace slashes with `-` in queries</param>
19+
/// <returns>All disc IDs for the given query, empty on error</returns>
20+
public static async Task<List<int>> ListDiscsResults(this RedumpClient rc, string? query, bool noSlash)
21+
{
22+
// If the query is invalid
23+
if (string.IsNullOrEmpty(query))
24+
return [];
25+
26+
List<int> ids = [];
27+
28+
// Strip quotes
29+
query = query!.Trim('"', '\'');
30+
31+
// Special characters become dashes
32+
query = query.Replace(' ', '-');
33+
query = query.Replace('\\', '-');
34+
if (!noSlash)
35+
query = query.Replace('/', '-');
36+
37+
// Lowercase is defined per language
38+
query = query.ToLowerInvariant();
39+
40+
// Keep getting quicksearch pages until there are none left
41+
try
42+
{
43+
int pageNumber = 1;
44+
while (true)
45+
{
46+
var pageIds = await rc.CheckSingleSitePage(string.Format(Constants.DiscsUrl, query, pageNumber++));
47+
if (pageIds is null)
48+
return [];
49+
50+
ids.AddRange(pageIds);
51+
if (pageIds.Count <= 1)
52+
break;
53+
}
54+
}
55+
catch (Exception ex)
56+
{
57+
Console.WriteLine($"An exception occurred while trying to log in: {ex}");
58+
return [];
59+
}
60+
61+
return ids;
62+
}
63+
64+
/// <summary>
65+
/// Download the disc pages associated with a given discs query
66+
/// </summary>
67+
/// <param name="rc">RedumpClient for connectivity</param>
68+
/// <param name="query">Query string to attempt to search for</param>
69+
/// <param name="outDir">Output directory to save data to</param>
70+
/// <param name="noSlash">Don't replace slashes with `-` in queries</param>
71+
/// <returns>All disc IDs for the given query, empty on error</returns>
72+
public static async Task<List<int>> DownloadDiscsResults(this RedumpClient rc, string? query, string? outDir, bool noSlash)
73+
{
74+
List<int> ids = [];
75+
76+
// If the query is invalid
77+
if (string.IsNullOrEmpty(query))
78+
return ids;
79+
80+
// Strip quotes
81+
query = query!.Trim('"', '\'');
82+
83+
// Special characters become dashes
84+
query = query.Replace(' ', '-');
85+
query = query.Replace('\\', '-');
86+
if (!noSlash)
87+
query = query.Replace('/', '-');
88+
89+
// Lowercase is defined per language
90+
query = query.ToLowerInvariant();
91+
92+
// Keep getting quicksearch pages until there are none left
93+
int pageNumber = 1;
94+
while (true)
95+
{
96+
var pageIds = await rc.CheckSingleSitePage(string.Format(Constants.DiscsUrl, query, pageNumber++), outDir, false);
97+
if (pageIds is null)
98+
return [];
99+
100+
ids.AddRange(pageIds);
101+
if (pageIds.Count == 0)
102+
break;
103+
}
104+
105+
return ids;
106+
}
107+
13108
/// <summary>
14109
/// Download the last modified disc pages, until first failure
15110
/// </summary>

0 commit comments

Comments
 (0)