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.

example12_easy_laststatus.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* This file is part of mastodon-cpp.
  2. * Prints some information about your last status.
  3. */
  4. // Don't compile this if the Easy-interface is turned off
  5. #ifndef WITHOUT_EASY
  6. #include <iostream>
  7. #include <string>
  8. #include <cstdint>
  9. // If we are compiling mastodon-cpp, use another include path
  10. #ifdef MASTODON_CPP
  11. #include "mastodon-cpp.hpp"
  12. #include "easy/all.hpp"
  13. #else
  14. #include <mastodon-cpp/mastodon-cpp.hpp>
  15. // Include all headers in mastodon-cpp/easy/
  16. #include <mastodon-cpp/easy/all.hpp>
  17. #endif
  18. using Mastodon::API;
  19. using Mastodon::Easy;
  20. using std::cout;
  21. int main(int argc, char *argv[])
  22. {
  23. if (argc < 3)
  24. {
  25. std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
  26. return 1;
  27. }
  28. Easy masto(argv[1], argv[2]);
  29. std::string answer;
  30. std::uint16_t ret;
  31. // Get own account in order to obtain account ID
  32. ret = masto.get(API::v1::accounts_verify_credentials, answer);
  33. if (ret == 0)
  34. {
  35. // Construct an Account object using the JSON string from the server
  36. Easy::Account acc(answer);
  37. // Get last status
  38. ret = masto.get(API::v1::accounts_id_statuses,
  39. {
  40. { "id", { std::to_string(acc.id()) } },
  41. { "limit", { "1" } }
  42. },
  43. answer);
  44. if (ret == 0)
  45. {
  46. // answer contains an array with a single object. This works because
  47. // Easy::Status will turn that into an object, but an array with
  48. // more than 1 object will not work.
  49. Easy::Status status(answer);
  50. // An Entitiy is valid if the JSON was not empty and contained no
  51. // "error":-key
  52. if (status.valid())
  53. {
  54. if (!status.language().empty())
  55. cout << "Language: " << status.language() << '\n';
  56. if (!status.content().empty())
  57. cout << "Content: "
  58. << status.content().substr(0, 200) << "…\n";
  59. if (!status.application().name().empty())
  60. cout << "Application used: "
  61. << status.application().name() << '\n';
  62. cout << "ID: " << status.id() << '\n';
  63. string acct;
  64. string url;
  65. std::vector<Easy::Attachment> attachments;
  66. std::vector<Easy::Tag> tags;
  67. // If the status is a reblog, print the original author
  68. if (status.reblog().valid())
  69. {
  70. // status.reblog() is an Easy::Status
  71. // status.reblog().account() is an Easy::Account
  72. cout << "Original ID: " << status.reblog().id() << '\n';
  73. acct = status.reblog().account().acct();
  74. url = status.reblog().account().url();
  75. attachments = status.reblog().media_attachments();
  76. tags = status.reblog().tags();
  77. }
  78. else
  79. {
  80. acct = status.account().acct();
  81. url = status.account().url();
  82. attachments = status.media_attachments();
  83. tags = status.tags();
  84. }
  85. cout << "From: " << acct << " ";
  86. cout << "(" << url << ")\n";
  87. // List attachments, if any
  88. for (const Easy::Attachment &attachment : attachments)
  89. {
  90. cout << "Attachment: " << attachment.text_url()
  91. << " (" << attachment.size() << ")\n";
  92. }
  93. // List hashtags, if any
  94. for (const Easy::Tag &tag : tags)
  95. {
  96. cout << "Hashtag: #" << tag.name()
  97. << " (" << tag.url() << ")\n";
  98. }
  99. }
  100. return 0;
  101. }
  102. }
  103. std::cout << answer << '\n';
  104. std::cerr << "Error code: " << ret << '\n';
  105. return ret;
  106. }
  107. #else
  108. #include <cstdio>
  109. int main()
  110. {
  111. printf("mastodon-cpp was compiled without Easy support.\n");
  112. return 255;
  113. }
  114. #endif // WITHOUT_EASY