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.

example01_dump_json.cpp 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* This file is part of mastodon-cpp.
  2. * This example dumps the raw JSON of your last toot with media attached
  3. * and your last 2 followers.
  4. */
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdint>
  9. #ifdef MASTODON_CPP
  10. #include "mastodon-cpp.hpp"
  11. #else
  12. #include <mastodon-cpp/mastodon-cpp.hpp>
  13. #endif
  14. using Mastodon::API;
  15. int main(int argc, char *argv[])
  16. {
  17. if (argc < 3)
  18. {
  19. std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
  20. return 1;
  21. }
  22. Mastodon::API masto(argv[1], argv[2]);
  23. masto.set_useragent("mastodon-cpp-example/1.3.3.7");
  24. std::string answer;
  25. std::uint16_t ret;
  26. ret = masto.get(API::v1::accounts_verify_credentials, answer);
  27. if (ret == 0)
  28. {
  29. std::cout << "Your last toot with media attached:\n";
  30. std::string uid = answer.substr(7, answer.find("\"", 7) - 7);
  31. API::parametermap parameters =
  32. {
  33. { "id", { uid } },
  34. { "limit", { "1" } },
  35. { "only_media", { "1" } }
  36. };
  37. ret = masto.get(API::v1::accounts_id_statuses, parameters, answer);
  38. if (ret == 0)
  39. {
  40. std::cout << answer << '\n';
  41. }
  42. else
  43. {
  44. std::cerr << "Error code: " << ret << '\n';
  45. return ret;
  46. }
  47. std::cout << "\nYour last 2 followers:\n";
  48. parameters =
  49. {
  50. {
  51. "limit", { "2" }
  52. },
  53. {
  54. "exclude_types", { "favourite", "reblog", "mention" }
  55. }
  56. };
  57. ret = masto.get(API::v1::notifications, parameters, answer);
  58. if (ret == 0)
  59. {
  60. std::cout << answer << '\n';
  61. return 0;
  62. }
  63. else
  64. {
  65. std::cerr << "Error code: " << ret << '\n';
  66. return ret;
  67. }
  68. }
  69. else if (ret == 13)
  70. {
  71. std::cerr << "The URL has permanently changed.\n" <<
  72. "New URL: " << answer << '\n';
  73. return ret;
  74. }
  75. else
  76. {
  77. std::cerr << "Error code: " << ret << '\n';
  78. return ret;
  79. }
  80. return 0;
  81. }