C++ wrapper for the Mastodon API
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.

example06_toot_delete-toot.cpp 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* This file is part of mastodon-cpp.
  2. * Post a status, then delete it.
  3. */
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <cstdint>
  8. #include <chrono>
  9. #include <thread>
  10. #ifdef MASTODON_CPP
  11. #include "mastodon-cpp.hpp"
  12. #else
  13. #include <mastodon-cpp/mastodon-cpp.hpp>
  14. #endif
  15. using Mastodon::API;
  16. int main(int argc, char *argv[])
  17. {
  18. if (argc < 3)
  19. {
  20. std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
  21. return 1;
  22. }
  23. Mastodon::API masto(argv[1], argv[2]);
  24. masto.set_useragent("mastodon-cpp-example/1.3.3.7");
  25. std::string answer;
  26. std::uint16_t ret;
  27. std::string toot;
  28. std::string id;
  29. std::cout << "Toot: ";
  30. std::cin >> toot;
  31. API::parametermap parameters =
  32. {
  33. { "status", { toot } },
  34. { "visibility", { "unlisted" } },
  35. { "spoiler_text", { "test" } }
  36. };
  37. ret = masto.post(API::v1::statuses, parameters, answer);
  38. if (ret == 0)
  39. {
  40. id = answer.substr(7, answer.find("\"", 7) - 7);
  41. std::cout << "Deleting in 30 seconds...\n";
  42. std::this_thread::sleep_for(std::chrono::seconds(30));
  43. ret = masto.del(API::v1::statuses_id, {{"id", {id}}});
  44. if (ret == 0)
  45. {
  46. std::cout << "Status " << id << " deleted.\n";
  47. }
  48. else
  49. {
  50. std::cerr << "Error code: " << ret << '\n';
  51. return ret;
  52. }
  53. }
  54. else if (ret == 13)
  55. {
  56. std::cerr << "The URL has permanently changed.\n" <<
  57. "New URL: " << answer << '\n';
  58. return ret;
  59. }
  60. else
  61. {
  62. std::cerr << "Error code: " << ret << '\n';
  63. return ret;
  64. }
  65. return 0;
  66. }