A tool for adding anime to your anidb list.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

115 lignes
3.1KB

  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. struct add_opts {
  14. enum mylist_state ao_state;
  15. bool ao_watched;
  16. };
  17. enum error cmd_add_cachecheck(const char *path, const struct stat *st,
  18. void *data)
  19. {
  20. const char *bname = util_basename(path);
  21. enum error err;
  22. err = cache_get(bname, st->st_size, NULL);
  23. if (err == NOERR) {
  24. /* We could get the entry, so it exists already */
  25. uio_user("This file (%s) with size (%lu) already exists in cache."
  26. " Skipping", bname, st->st_size);
  27. return ED2KUTIL_DONTHASH;
  28. } else if (err != ERR_CACHE_NO_EXISTS) {
  29. uio_error("Some error when trying to get from cache: %s",
  30. error_to_string(err));
  31. return ED2KUTIL_DONTHASH;
  32. }
  33. uio_user("Hashing %s", path);
  34. return NOERR;
  35. }
  36. enum error cmd_add_apisend(const char *path, const uint8_t *hash,
  37. const struct stat *st, void *data)
  38. {
  39. struct api_result r;
  40. struct add_opts* ao = (struct add_opts*)data;
  41. if (api_cmd_mylistadd(st->st_size, hash, ao->ao_state, ao->ao_watched, &r)
  42. != NOERR)
  43. return ERR_CMD_FAILED;
  44. if (r.code == 310) {
  45. struct api_mylistadd_result *x = &r.mylistadd;
  46. uio_warning("File already added! Adding it to cache");
  47. uio_debug("File info: lid: %ld, fid: %ld, eid: %ld, aid: %ld,"
  48. " gid: %ld, date: %ld, viewdate: %ld, state: %d,"
  49. " filestate: %d\nstorage: %s\nsource: %s\nother: %s",
  50. x->lid, x->fid, x->eid, x->aid, x->gid, x->date, x->viewdate,
  51. x->state, x->filestate, x->storage, x->source, x->other);
  52. cache_add(x->lid, util_basename(path), st->st_size, hash);
  53. if (x->storage)
  54. free(x->storage);
  55. if (x->source)
  56. free(x->source);
  57. if (x->other)
  58. free(x->other);
  59. return NOERR;
  60. }
  61. if (r.code != 210) {
  62. uio_error("Mylistadd failure: %hu", r.code);
  63. return ERR_CMD_FAILED;
  64. }
  65. uio_user("Succesfully added!");
  66. uio_debug("New mylist id is: %ld", r.mylistadd.new_id);
  67. cache_add(r.mylistadd.new_id, util_basename(path), st->st_size, hash);
  68. return NOERR;
  69. }
  70. enum error cmd_add(void *data)
  71. {
  72. struct add_opts add_opts = {0};
  73. struct ed2k_util_opts ed2k_opts = {
  74. .pre_hash_fn = cmd_add_cachecheck,
  75. .post_hash_fn = cmd_add_apisend,
  76. .data = &add_opts,
  77. };
  78. bool *watched;
  79. enum error err = NOERR;
  80. int fcount;
  81. fcount = config_get_nonopt_count();
  82. if (fcount == 0) {
  83. uio_error("No files specified");
  84. return ERR_CMD_ARG;
  85. }
  86. if (config_get("watched", (void**)&watched) == NOERR) {
  87. add_opts.ao_watched = *watched;
  88. }
  89. add_opts.ao_state = MYLIST_STATE_INTERNAL;
  90. for (int i = 0; i < fcount; i++) {
  91. err = ed2k_util_iterpath(config_get_nonopt(i), &ed2k_opts);
  92. if (err != NOERR)
  93. break;
  94. }
  95. return err;
  96. }