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.

example05_follow_unfollow.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* This file is part of mastodon-cpp.
  2. * Follow someone, then unfollow.
  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 id;
  28. std::cout << "Follow [ID or username@domain]: ";
  29. std::cin >> id;
  30. // If no @ is found, it is presumably an ID
  31. if (id.find('@') == std::string::npos)
  32. {
  33. ret = masto.post(API::v1::accounts_id_follow, {{"id", {id}}}, answer);
  34. if (ret == 0)
  35. {
  36. // std::cout << answer << '\n';
  37. }
  38. else if (ret == 13)
  39. {
  40. std::cerr << "The URL has permanently changed.\n" <<
  41. "New URL: " << answer << '\n';
  42. return ret;
  43. }
  44. else
  45. {
  46. std::cerr << "Error code: " << ret << '\n';
  47. return ret;
  48. }
  49. std::cout << "Unfollowing in 30 seconds...\n";
  50. std::this_thread::sleep_for(std::chrono::seconds(30));
  51. ret = masto.post(API::v1::accounts_id_unfollow, {{"id", {id}}}, answer);
  52. if (ret == 0)
  53. {
  54. // std::cout << answer << '\n';
  55. }
  56. else
  57. {
  58. std::cerr << "Error code: " << ret << '\n';
  59. return ret;
  60. }
  61. }
  62. else
  63. {
  64. API::parametermap params =
  65. {
  66. { "uri", { id } }
  67. };
  68. ret = masto.post(API::v1::follows, params, answer);
  69. if (ret == 0)
  70. {
  71. // std::cout << answer << '\n';
  72. id = answer.substr(7, answer.find("\"", 7) - 7);
  73. }
  74. else
  75. {
  76. std::cerr << "Error code: " << ret << '\n';
  77. return ret;
  78. }
  79. std::cout << "Unfollowing " << id << " in 30 seconds...\n";
  80. std::this_thread::sleep_for(std::chrono::seconds(30));
  81. ret = masto.post(API::v1::accounts_id_unfollow, {{"id", {id}}}, answer);
  82. if (ret == 0)
  83. {
  84. // std::cout << answer << '\n';
  85. }
  86. else
  87. {
  88. std::cerr << "Error code: " << ret << '\n';
  89. return ret;
  90. }
  91. }
  92. return 0;
  93. }