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.

124 lines
3.4KB

  1. #include <sys/stat.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include "cmd.h"
  6. #include "error.h"
  7. #include "uio.h"
  8. #include "api.h"
  9. #include "config.h"
  10. #include "ed2k_util.h"
  11. #include "cache.h"
  12. #include "util.h"
  13. enum error cmd_add_cachecheck(const char *path, const struct stat *st,
  14. void *data)
  15. {
  16. const char *bname = util_basename(path);
  17. enum error err;
  18. err = cache_get(bname, st->st_size, NULL);
  19. if (err == NOERR) {
  20. /* We could get the entry, so it exists already */
  21. uio_user("This file (%s) with size (%lu) already exists in cache."
  22. " Skipping", bname, st->st_size);
  23. return ED2KUTIL_DONTHASH;
  24. } else if (err != ERR_CACHE_NO_EXISTS) {
  25. uio_error("Some error when trying to get from cache: %s",
  26. error_to_string(err));
  27. return ED2KUTIL_DONTHASH;
  28. }
  29. uio_user("Hashing %s", path);
  30. return NOERR;
  31. }
  32. enum error cmd_add_apisend(const char *path, const uint8_t *hash,
  33. const struct stat *st, void *data)
  34. {
  35. struct api_result r;
  36. struct api_mylistadd_opts *mopt = (struct api_mylistadd_opts *)data;
  37. if (api_cmd_mylistadd(st->st_size, hash, mopt, &r)
  38. != NOERR)
  39. return ERR_CMD_FAILED;
  40. if (r.code == 310) {
  41. struct api_mylistadd_result *x = &r.mylistadd;
  42. uio_warning("File already added! Adding it to cache");
  43. uio_debug("File info: lid: %ld, fid: %ld, eid: %ld, aid: %ld,"
  44. " gid: %ld, date: %ld, viewdate: %ld, state: %d,"
  45. " filestate: %d\nstorage: %s\nsource: %s\nother: %s",
  46. x->lid, x->fid, x->eid, x->aid, x->gid, x->date, x->viewdate,
  47. x->state, x->filestate, x->storage, x->source, x->other);
  48. cache_add(x->lid, util_basename(path), st->st_size, hash);
  49. if (x->storage)
  50. free(x->storage);
  51. if (x->source)
  52. free(x->source);
  53. if (x->other)
  54. free(x->other);
  55. return NOERR;
  56. }
  57. if (r.code != 210) {
  58. uio_error("Mylistadd failure: %hu", r.code);
  59. return ERR_CMD_FAILED;
  60. }
  61. uio_user("Succesfully added!");
  62. uio_debug("New mylist id is: %ld", r.mylistadd.new_id);
  63. cache_add(r.mylistadd.new_id, util_basename(path), st->st_size, hash);
  64. return NOERR;
  65. }
  66. enum error cmd_add(void *data)
  67. {
  68. struct api_mylistadd_opts mopt = {0};
  69. struct ed2k_util_opts ed2k_opts = {
  70. .pre_hash_fn = cmd_add_cachecheck,
  71. .post_hash_fn = cmd_add_apisend,
  72. .data = &mopt,
  73. };
  74. bool *watched;
  75. const char **wdate_str;
  76. enum error err = NOERR;
  77. int fcount;
  78. fcount = config_get_nonopt_count();
  79. if (fcount == 0) {
  80. uio_error("No files specified");
  81. return ERR_CMD_ARG;
  82. }
  83. if (config_get("watched", (void**)&watched) == NOERR && *watched) {
  84. mopt.watched = *watched;
  85. mopt.watched_set = true;
  86. if (config_get("wdate", (void**)&wdate_str) == NOERR) {
  87. uint64_t wdate = util_iso2unix(*wdate_str);
  88. if (wdate == 0) {
  89. uio_error("Invalid time value: '%s'", *wdate_str);
  90. return ERR_CMD_ARG;
  91. }
  92. mopt.wdate = wdate;
  93. mopt.wdate_set = true;
  94. }
  95. }
  96. mopt.state = MYLIST_STATE_INTERNAL;
  97. mopt.state_set = true;
  98. for (int i = 0; i < fcount; i++) {
  99. err = ed2k_util_iterpath(config_get_nonopt(i), &ed2k_opts);
  100. if (err != NOERR)
  101. break;
  102. }
  103. return err;
  104. }