A CLI tool for downloading from pixiv.net
Révision | e18345887020b56f1ffc52446d14c027e0554019 (tree) |
---|---|
l'heure | 2023-06-22 12:03:18 |
Auteur | mio <stigma@disr...> |
Commiter | mio |
Improve 'artwork' command
The main change is the --group-errors option which will only print the
errors just before the command has finished executing, rather than
immediately after the error occurrs.
This means you don't have to go searching for any errors after
downloading the artworks.
@@ -389,40 +389,76 @@ int artistHandle(string[] args, ref Config config) | ||
389 | 389 | |
390 | 390 | int artworkHandle(string[] args, ref Config config) |
391 | 391 | { |
392 | - if (3 > args.length) { | |
393 | - stderr.writefln("Usage: %s <id>...", args[0]); | |
394 | - return 1; | |
395 | - } | |
392 | + // When an exception occurrs. | |
393 | + string[] invalidArtworks; | |
394 | + // The error message for the invalidArtworks | |
395 | + // each index in the array matches the ID. | |
396 | + string[] errorMessages; | |
397 | + bool groupErrors = false; | |
398 | + int returnCode = 0; | |
396 | 399 | |
397 | - // Help | |
398 | - { | |
399 | - string arg = args[2]; | |
400 | - if ("help" == arg || "--help" == arg || "-h" == arg) { | |
401 | - writefln("Usage: %s <id>...", args[0]); | |
402 | - return 0; | |
403 | - } | |
404 | - } | |
400 | + if (3 > args.length) { | |
401 | + displayArtworkHelp(args[0]); | |
402 | + return 1; | |
403 | + } | |
405 | 404 | |
406 | - foreach (arg; args[2..$]) { | |
407 | - size_t id; | |
405 | + foreach (arg; args[2..$]) { | |
406 | + size_t id; | |
408 | 407 | |
409 | - try { | |
410 | - id = to!size_t(arg); | |
411 | - } catch (ConvException ce) { | |
412 | - log.criticalf("Could not convert \"%s\" to size_t.", arg); | |
408 | + if ("--help" == arg || "-h" == arg) { | |
409 | + displayArtworkHelp(args[0]); | |
410 | + return 0; | |
411 | + } | |
412 | + if ("--group-errors" == arg) { | |
413 | + groupErrors = true; | |
414 | + log.info("errors will be grouped"); | |
415 | + continue; | |
416 | + } | |
413 | 417 | |
414 | - stderr.writefln(`Failed to download artwork ID "%s".`, arg); | |
415 | - stderr.writeln("-- It could not be parsed as a number."); | |
416 | - return 1; | |
417 | - } | |
418 | + try { | |
419 | + id = to!size_t(arg); | |
420 | + } catch (ConvException ce) { | |
421 | + log.errorf("Could not convert %s to size_t", arg); | |
422 | + | |
423 | + if (groupErrors) { | |
424 | + invalidArtworks ~= arg; | |
425 | + errorMessages ~= "Not a valid number."; | |
426 | + } else { | |
427 | + stderr.writefln("%s: Could not convert '%s' to a valid ID.", | |
428 | + args[0], arg); | |
429 | + stderr.writeln("The ID must be a valid number."); | |
430 | + returnCode = 1; | |
431 | + } | |
432 | + continue; | |
433 | + } | |
418 | 434 | |
419 | - Illustration illust = config.client.fetchIllustration(id); | |
420 | - downloadIllust(illust, config); | |
435 | + try { | |
436 | + scope illust = config.client.fetchIllustration(id); | |
437 | + downloadIllust(illust, config); | |
438 | + } catch (PixivJSONException pje) { | |
439 | + | |
440 | + if (groupErrors) { | |
441 | + invalidArtworks ~= arg; | |
442 | + errorMessages ~= pje.msg; | |
443 | + } else { | |
444 | + stderr.writefln("%s: Failed to fetch and download ID '%d'", | |
445 | + id); | |
446 | + stderr.writeln(pje.msg); | |
447 | + } | |
448 | + continue; | |
449 | + } | |
421 | 450 | |
422 | - sleep(1, 5); | |
423 | - } | |
451 | + sleep(1, 5); | |
452 | + } | |
424 | 453 | |
425 | - return 0; | |
454 | + if (groupErrors && 0 < invalidArtworks.length) { | |
455 | + stderr.writeln("Failed to download the following IDs:"); | |
456 | + foreach(index, id; invalidArtworks) { | |
457 | + stderr.writefln(" %s - %s", id, errorMessages[index]); | |
458 | + } | |
459 | + } | |
460 | + | |
461 | + return returnCode; | |
426 | 462 | } |
427 | 463 | |
428 | 464 | int dailyHandle(string[] args, ref Config config) |
@@ -687,7 +723,7 @@ helpHandle(string[] args, const ref Config config) | ||
687 | 723 | displayArtistHelp(args[0]); |
688 | 724 | break; |
689 | 725 | case "artwork": |
690 | - writeln("Artwork help"); | |
726 | + displayArtworkHelp(args[0]); | |
691 | 727 | break; |
692 | 728 | case "daily": |
693 | 729 | displayDailyHelp(args[0]); |
@@ -781,6 +817,24 @@ displayArtistHelp(string programName) | ||
781 | 817 | " pixiv_down artist --type illust 10109777 4938312", |
782 | 818 | programName); |
783 | 819 | } |
820 | + | |
821 | +void | |
822 | +displayArtworkHelp(string programName) | |
823 | +{ | |
824 | + stderr.writefln( | |
825 | + "pixiv_down artwork - Download specific artwork(s)\n" ~ | |
826 | + "\nUsage:\t%s artwork <id> [<additional ids...>]\n" ~ | |
827 | + "\nYou can download one or more artworks via this command. Any\n" ~ | |
828 | + "artworks that fail to download are mentioned after attempting to\n" ~ | |
829 | + "download, you can change this to only print at the end by using the\n" ~ | |
830 | + "--group-errors option.\n" ~ | |
831 | + "\nOptions:\n" ~ | |
832 | + " --group-errors\tDisplay errors at the end instead.\n" ~ | |
833 | + "\nExamples:\n" ~ | |
834 | + "\n Download a single artwork:\n" ~ | |
835 | + " pixiv_down artwork id\n" ~ | |
836 | + "\n Download multiple artworks and display failed IDs at the end:\n" ~ | |
837 | + " pixiv_down artwork --group-errors id1 id2\n", | |
784 | 838 | programName); |
785 | 839 | } |
786 | 840 |