Better argument handling, make output-dir optional
This commit is contained in:
46
Program.cs
46
Program.cs
@@ -7,9 +7,9 @@ namespace WSiPBookDownloader;
|
|||||||
|
|
||||||
internal class BookMissingException : Exception
|
internal class BookMissingException : Exception
|
||||||
{
|
{
|
||||||
public int BookId { get; }
|
public uint BookId { get; }
|
||||||
|
|
||||||
public BookMissingException(int bookId)
|
public BookMissingException(uint bookId)
|
||||||
: base($"Book with ID {bookId} does not exist.")
|
: base($"Book with ID {bookId} does not exist.")
|
||||||
{
|
{
|
||||||
BookId = bookId;
|
BookId = bookId;
|
||||||
@@ -45,28 +45,46 @@ internal class Program
|
|||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
if (args.Length < 2 || args.Length > 4)
|
var onlyCovers = args.Any(arg => arg.Equals("--only-covers", StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
var outputDirectory = Directory.GetCurrentDirectory();
|
||||||
|
var outputDirectoryIndex = Array.FindIndex(args, arg => arg.Equals("--output-dir", StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (outputDirectoryIndex > -1)
|
||||||
{
|
{
|
||||||
Log("Usage: [outputDirectory] [startBookId] [endBookId] [--only-covers]", ConsoleColor.DarkRed);
|
if (outputDirectoryIndex + 1 >= args.Length)
|
||||||
|
{
|
||||||
|
Log("--output-dir requires a value.", ConsoleColor.DarkRed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var outputDirectory = args[0];
|
outputDirectory = args[outputDirectoryIndex + 1];
|
||||||
if (!Path.Exists(outputDirectory))
|
|
||||||
|
if (!Directory.Exists(outputDirectory))
|
||||||
{
|
{
|
||||||
Log($"Output directory \"{outputDirectory}\" does not exist.", ConsoleColor.DarkRed);
|
Log($"Output directory \"{outputDirectory}\" does not exist.", ConsoleColor.DarkRed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var startBookId = int.Parse(args[1]);
|
var positionalArgs = args
|
||||||
var endBookId = (args.Length >= 3 && int.TryParse(args[2], out var parsedEndBookId))
|
.Except(
|
||||||
? parsedEndBookId
|
[
|
||||||
|
"--only-covers",
|
||||||
|
"--output-dir",
|
||||||
|
outputDirectory
|
||||||
|
], StringComparer.OrdinalIgnoreCase)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
var startBookId = int.Parse(positionalArgs[0]);
|
||||||
|
var endBookId = positionalArgs.Length > 1
|
||||||
|
? int.Parse(positionalArgs[1])
|
||||||
: startBookId;
|
: startBookId;
|
||||||
var onlyCovers = args.FirstOrDefault(arg => arg.Equals("--only-covers", StringComparison.OrdinalIgnoreCase)) != null;
|
|
||||||
|
|
||||||
var bookIds = Enumerable.Range(startBookId, endBookId - startBookId + 1);
|
var bookIds = Enumerable.Range(startBookId, endBookId - startBookId + 1).Select(bookId => (uint)bookId);
|
||||||
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = onlyCovers ? 100 : 10 };
|
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = onlyCovers ? 100 : 10 };
|
||||||
|
|
||||||
|
Log($"Output directory: \"{outputDirectory}\"");
|
||||||
|
|
||||||
await Parallel.ForEachAsync(bookIds, parallelOptions, async (bookId, cancellationToken) =>
|
await Parallel.ForEachAsync(bookIds, parallelOptions, async (bookId, cancellationToken) =>
|
||||||
{
|
{
|
||||||
var outputPath = Path.Combine(outputDirectory, $"{bookId}.pdf");
|
var outputPath = Path.Combine(outputDirectory, $"{bookId}.pdf");
|
||||||
@@ -104,7 +122,7 @@ internal class Program
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task DownloadBookCoverJpg(int bookId, string outputDir, CancellationToken cancellationToken = default)
|
private static async Task DownloadBookCoverJpg(uint bookId, string outputDir, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var outputPath = Path.Combine(outputDir, $"{bookId}.jpg");
|
var outputPath = Path.Combine(outputDir, $"{bookId}.jpg");
|
||||||
if (File.Exists(outputPath))
|
if (File.Exists(outputPath))
|
||||||
@@ -126,7 +144,7 @@ internal class Program
|
|||||||
Debug.WriteLine($"Downloaded cover for book {bookId}");
|
Debug.WriteLine($"Downloaded cover for book {bookId}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task DownloadBookJpg(int bookId, string outputDir, CancellationToken cancellationToken = default)
|
private static async Task DownloadBookJpg(uint bookId, string outputDir, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var currentPage = 1;
|
var currentPage = 1;
|
||||||
while (true)
|
while (true)
|
||||||
@@ -163,7 +181,7 @@ internal class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void BuildPdf(int bookId, string jpgDir, string outputPath)
|
private static void BuildPdf(uint bookId, string jpgDir, string outputPath)
|
||||||
{
|
{
|
||||||
var pages = Directory.GetFiles(jpgDir, "*.jpg")
|
var pages = Directory.GetFiles(jpgDir, "*.jpg")
|
||||||
.OrderBy(file => int.Parse(Path.GetFileNameWithoutExtension(file)))
|
.OrderBy(file => int.Parse(Path.GetFileNameWithoutExtension(file)))
|
||||||
|
|||||||
Reference in New Issue
Block a user