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.

example14_easy_treeview.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* This file is part of mastodon-cpp.
  2. * Prints a Mastodon-conversation in a tree.
  3. * Argument has to be in the form: https://<domain>/<user>/<status id>
  4. *
  5. * This example uses a rather wasteful approach in order to use less code and
  6. * therefore be more clear. Please don't use it on long conversations.
  7. */
  8. // Don't compile this if the Easy-interface is turned off
  9. #ifndef WITHOUT_EASY
  10. #include <iostream>
  11. #include <string>
  12. #include <thread>
  13. #include <chrono>
  14. #include <ctime>
  15. #include <regex>
  16. // If we are compiling mastodon-cpp, use another include path
  17. #ifdef MASTODON_CPP
  18. #include "mastodon-cpp.hpp"
  19. #include "easy/all.hpp"
  20. #else
  21. #include <mastodon-cpp/mastodon-cpp.hpp>
  22. // Include all headers in mastodon-cpp/easy/
  23. #include <mastodon-cpp/easy/all.hpp>
  24. #endif
  25. using Mastodon::API;
  26. using Mastodon::Easy;
  27. using std::cout;
  28. using std::chrono::system_clock;
  29. // Print a status to stdout, nicely formatted
  30. void format_status(const Easy::Status &status, const std::uint8_t &level)
  31. {
  32. std::string space(level, ' ');
  33. // Remove HTML-tags
  34. std::regex restrip("<[^>]*>");
  35. std::string content = std::regex_replace(status.content(), restrip, "");
  36. cout << space << "+-----------------------------------------\n";
  37. cout << space << "| " << status.account().acct() << ": \n";
  38. while (!content.empty())
  39. {
  40. cout << space << "| " << content.substr(0, 40) << '\n';
  41. if (content.length() > 40)
  42. {
  43. content = content.substr(40);
  44. }
  45. else
  46. {
  47. content.clear();
  48. }
  49. }
  50. cout << space << "| "
  51. << Easy::strtime_local(status.created_at(), "%T") << '\n';
  52. cout << space << "+-----------------------------------------" << std::endl;
  53. }
  54. // Fetch status and its descendants, output them
  55. std::uint16_t print_status(Easy &masto, const std::string &id,
  56. const std::uint8_t level)
  57. {
  58. std::uint16_t ret;
  59. std::string answer;
  60. API::parametermap parameters =
  61. {
  62. { "id", { id }}
  63. };
  64. ret = masto.get(Mastodon::API::v1::statuses_id, parameters, answer);
  65. if (ret == 0)
  66. {
  67. format_status(Easy::Status(answer), level);
  68. ret = masto.get(Mastodon::API::v1::statuses_id_context,
  69. parameters, answer);
  70. if (ret == 0)
  71. {
  72. Easy::Context context(answer);
  73. for (const Easy::Status &s : context.descendants())
  74. {
  75. // Only print descendant if it is a direct reply
  76. // NOTE: This is a wasteful approach, I use it only in the
  77. // interest of more clarity. Don't do this at home. :-)
  78. if (s.in_reply_to_id() == std::stoull(id))
  79. {
  80. print_status(masto, std::to_string(s.id()), level + 4);
  81. }
  82. }
  83. }
  84. }
  85. return ret;
  86. }
  87. int main(int argc, char *argv[])
  88. {
  89. if (argc < 2)
  90. {
  91. std::cerr << "usage: " << argv[0] << " <url>\n";
  92. return 1;
  93. }
  94. // Extract domain name
  95. std::string instance = std::string(argv[1]);
  96. std::size_t pos = instance.find('/') + 2;
  97. instance = instance.substr(pos, instance.find('/', pos) - pos);
  98. // Extract status ID
  99. std::string id = std::string(argv[1]);
  100. id = id.substr(id.rfind('/') + 1);
  101. cout << " Instance: " << instance << "\nStatus ID: " << id << "\n\n";
  102. Easy masto(instance, "");
  103. return print_status(masto, id, 0);
  104. }
  105. #else
  106. #include <cstdio>
  107. int main()
  108. {
  109. printf("mastodon-cpp was compiled without Easy support.\n");
  110. return 255;
  111. }
  112. #endif // WITHOUT_EASY