A tool for adding anime to your anidb list.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.2KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include "cmd.h"
  6. #include "error.h"
  7. #include "uio.h"
  8. #include "api.h"
  9. #include "config.h"
  10. #include "cache.h"
  11. #include "util.h"
  12. bool did_cache_init = false;
  13. uint64_t cmd_modify_arg_parse(const char *str, uint64_t *out_wdate)
  14. {
  15. uint64_t val;
  16. struct cache_entry ce;
  17. enum error err;
  18. const char *fn_st = strrchr(str, '/');
  19. const char *size_st = strchr(str, '/');
  20. *out_wdate = 0;
  21. /* Skip the '/' */
  22. if (fn_st)
  23. fn_st++;
  24. if (size_st)
  25. size_st++;
  26. if (fn_st == size_st) {
  27. /* size/filename format */
  28. size_st = str;
  29. } else {
  30. size_t timelen = size_st - str - 1; /* -1 for the skipped '/' */
  31. char timstr[timelen + 1];
  32. memcpy(timstr, str, timelen);
  33. timstr[timelen] = '\0';
  34. *out_wdate = util_iso2unix(timstr);
  35. }
  36. if (sscanf(size_st, "%lu", &val) != 1)
  37. return 0;
  38. if (!fn_st && !size_st) /* Only lid format */
  39. return val;
  40. if (!cache_is_init()) {
  41. if (cache_init() != NOERR)
  42. return 0;
  43. did_cache_init = true;
  44. }
  45. err = cache_get(fn_st, val, CACHE_S_LID, &ce);
  46. if (err != NOERR)
  47. return 0;
  48. return ce.lid;
  49. }
  50. enum error cmd_modify(void *data)
  51. {
  52. struct api_mylistadd_opts mopt = {0};
  53. bool *watched;
  54. const char **wdate_str;
  55. enum error err = NOERR;
  56. int fcount;
  57. fcount = config_get_nonopt_count();
  58. if (fcount == 0) {
  59. uio_error("No mylist ids specified");
  60. return ERR_CMD_ARG;
  61. }
  62. if (config_get("watched", (void**)&watched) == NOERR && *watched) {
  63. mopt.watched = *watched;
  64. mopt.watched_set = true;
  65. if (config_get("wdate", (void**)&wdate_str) == NOERR) {
  66. uint64_t wdate = util_iso2unix(*wdate_str);
  67. if (wdate == 0) {
  68. uio_error("Invalid time value: '%s'", *wdate_str);
  69. return ERR_CMD_ARG;
  70. }
  71. mopt.wdate = wdate;
  72. mopt.wdate_set = true;
  73. }
  74. }
  75. for (int i = 0; i < fcount; i++) {
  76. struct api_result res;
  77. struct api_mylistadd_opts l_opts = mopt;
  78. const char *arg = config_get_nonopt(i);
  79. uint64_t wdate;
  80. uint64_t lid = cmd_modify_arg_parse(arg, &wdate);
  81. if (lid == 0) {
  82. uio_error("Argument '%s' is not valid. Skipping", arg);
  83. continue;
  84. }
  85. if (wdate != 0) {
  86. l_opts.watched = true;
  87. l_opts.watched_set = true;
  88. l_opts.wdate = wdate;
  89. l_opts.wdate_set = true;
  90. }
  91. err = api_cmd_mylistmod(lid, &l_opts, &res);
  92. if (err != NOERR)
  93. break;
  94. if (res.code == APICODE_NO_SUCH_MYLIST_ENTRY) {
  95. uio_error("No mylist entry with id: '%lu'", lid);
  96. continue;
  97. }
  98. if (!cache_is_init()) {
  99. if ((err = cache_init()) != NOERR)
  100. return err;
  101. did_cache_init = true;
  102. }
  103. err = cache_update(lid, &l_opts);
  104. if (err != NOERR)
  105. break;
  106. }
  107. if (did_cache_init) {
  108. did_cache_init = false;
  109. cache_free();
  110. }
  111. return err;
  112. }