novel.d 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * pixiv_down - CLI-based downloading tool for https://www.pixiv.net.
  3. * Copyright (C) 2024 Mio
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. module app.cmds.novel;
  18. import std.experimental.logger;
  19. import std.stdio;
  20. import pd.configuration;
  21. import pd.pixiv;
  22. import app.util;
  23. int novelHandle(string[] args, in Config config)
  24. {
  25. import std.algorithm.searching : all, countUntil;
  26. import std.algorithm.mutation : remove;
  27. import std.ascii : isDigit;
  28. import std.getopt : getopt, GetOptOption = config;
  29. // id => error_message
  30. string[string] errors;
  31. bool groupErrors;
  32. // novel <id...>
  33. if (args.length < 2) {
  34. displayNovelHelp();
  35. return 1;
  36. }
  37. auto helpInformation = getopt(args,
  38. GetOptOption.passThrough,
  39. "group-errors", &groupErrors);
  40. if (helpInformation.helpWanted) {
  41. displayNovelHelp();
  42. return 0;
  43. }
  44. infof("grouping errors = %s", groupErrors);
  45. foreach(arg; args[2..$]) {
  46. if (false == all!isDigit(arg)) {
  47. errorf("invalid ID: %s", arg);
  48. if (groupErrors) {
  49. errors[arg] = "Invalid ID. Must be all numbers.";
  50. continue;
  51. } else {
  52. stderr.writefln("Error: Invalid ID: %s", arg);
  53. continue;
  54. }
  55. }
  56. try {
  57. fetchAndDownloadNovel(arg, config);
  58. } catch (Exception e) {
  59. errors[arg] = e.msg;
  60. }
  61. }
  62. return 0;
  63. }
  64. public void displayNovelHelp()
  65. {
  66. stderr.writefln(
  67. "pixiv_down novel - Download specific novel(s)\n" ~
  68. "\nUsage:\tpixiv_down novel [options] <id> [<additional ids...>]\n" ~
  69. "\nYou can download one or more novels via this command. Any\n" ~
  70. "novels that fail to download are mentioned after attempting to\n" ~
  71. "download, you can change this to only print at the end by using the\n" ~
  72. "--group-errors option.\n" ~
  73. "\nOptions:\n" ~
  74. " --group-errors\tDisplay errors at the end instead.\n" ~
  75. "\nExamples:\n" ~
  76. "\n Download a single novel:\n" ~
  77. " pixiv_down artwork id\n" ~
  78. "\n Download multiple novels and display failed IDs at the end:\n" ~
  79. " pixiv_down artwork --group-errors id1 id2\n");
  80. }
  81. private:
  82. void fetchAndDownloadNovel(string id, in Config config)
  83. {
  84. import pd.pixiv_downloader : downloadNovel;
  85. tracef("fetching information for %s", id);
  86. const novel = fetchNovelInfo(id, config);
  87. downloadNovel(novel, config);
  88. }