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.

example13_easy_stream.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* This file is part of mastodon-cpp.
  2. * Prints some information from the public timeline.
  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 <thread>
  9. #include <memory>
  10. #include <vector>
  11. #include <chrono>
  12. #include <mutex>
  13. // If we are compiling mastodon-cpp, use another include path
  14. #ifdef MASTODON_CPP
  15. #include "mastodon-cpp.hpp"
  16. #include "easy/all.hpp"
  17. #else
  18. #include <mastodon-cpp/mastodon-cpp.hpp>
  19. // Include all headers in mastodon-cpp/easy/
  20. #include <mastodon-cpp/easy/all.hpp>
  21. #endif
  22. using Mastodon::API;
  23. using Mastodon::Easy;
  24. using std::cout;
  25. using std::chrono::system_clock;
  26. int main(int argc, char *argv[])
  27. {
  28. if (argc < 3)
  29. {
  30. std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
  31. return 1;
  32. }
  33. cout << "I'll show you the public timeline. Press CTRL-C to cancel\n";
  34. // These have to be static in order to use them in- and outside the thread
  35. static std::string stream;
  36. // You can cancel the stream with this pointer (ptr->cancel_stream())
  37. static std::unique_ptr<Mastodon::API::http> ptr;
  38. // Start a new thread for the stream
  39. std::thread pub_tl([=]
  40. {
  41. Easy masto(argv[1], argv[2]);
  42. masto.get_stream(Mastodon::API::v1::streaming_public, stream, ptr);
  43. });
  44. while (true)
  45. {
  46. std::this_thread::sleep_for(std::chrono::seconds(1));
  47. // Skip iteration if ptr points not to the Mastodon::API::http object
  48. if (ptr == nullptr)
  49. {
  50. continue;
  51. }
  52. // Acquire lock for the stream variable
  53. std::lock_guard<std::mutex> lock(ptr->get_mutex());
  54. // Parse event stream and clear it afterwards
  55. std::vector<Easy::stream_event> events = Easy::parse_stream(stream);
  56. stream.clear();
  57. // The contents of the stream are now in a vector of pairs
  58. // { Easy::event_type, std::string }
  59. for (const Easy::stream_event &event : events)
  60. {
  61. Easy::Status status;
  62. Easy::Notification notification;
  63. // Print out some information about the events
  64. switch (event.first)
  65. {
  66. case Easy::event_type::Update:
  67. status.from_string(event.second);
  68. cout << "[" <<
  69. Easy::strtime_local(status.created_at(), "%T") << "] ";
  70. cout << "Status from: " << status.account().acct()
  71. << " (" << status.url() << ")\n";
  72. break;
  73. case Easy::event_type::Notification:
  74. notification.from_string(event.second);
  75. cout << "Notification involving: "
  76. << notification.account().acct()
  77. << " (" << notification.id() << ")\n";
  78. break;
  79. case Easy::event_type::Delete:
  80. cout << "Deleted: " << event.second << '\n';
  81. break;
  82. default:
  83. cout << "Something undefined happened. 😱\n";
  84. }
  85. }
  86. }
  87. pub_tl.join();
  88. }
  89. #else
  90. #include <cstdio>
  91. int main()
  92. {
  93. printf("mastodon-cpp was compiled without Easy support.\n");
  94. return 255;
  95. }
  96. #endif // WITHOUT_EASY