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.

example02_parse_account.cpp 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* This file is part of mastodon-cpp.
  2. * This example parses your account data and prints it out in a readable way.
  3. */
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <cstdint>
  8. #include <sstream>
  9. #include <jsoncpp/json/json.h>
  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. using std::cout;
  17. int main(int argc, char *argv[])
  18. {
  19. if (argc < 3)
  20. {
  21. std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
  22. return 1;
  23. }
  24. Mastodon::API masto(argv[1], argv[2]);
  25. std::string answer;
  26. std::stringstream ss;
  27. std::uint16_t ret;
  28. ret = masto.get(API::v1::accounts_verify_credentials, answer);
  29. if (ret == 0)
  30. {
  31. ss.str(answer);
  32. Json::Value json;
  33. ss >> json;
  34. std::string uid = json["id"].asString();
  35. cout << "Your ID is: " << uid << '\n';
  36. cout << "Your whole acount tree:\n";
  37. for (auto it = json.begin(); it != json.end(); ++it)
  38. {
  39. if (it.name().compare("source") == 0)
  40. {
  41. cout << it.name() << '\n';
  42. for (auto it_s = (*it).begin(); it_s != (*it).end(); ++it_s)
  43. {
  44. cout << '\t' << it_s.name() << ": ";
  45. cout << *it_s << '\n';
  46. }
  47. }
  48. else
  49. {
  50. cout << it.name() << ": ";
  51. cout << *it << '\n';
  52. }
  53. }
  54. }
  55. else if (ret == 13)
  56. {
  57. std::cerr << "The URL has permanently changed.\n" <<
  58. "New URL: " << answer << '\n';
  59. return ret;
  60. }
  61. else
  62. {
  63. std::cerr << "Error code: " << ret << '\n';
  64. return ret;
  65. }
  66. return 0;
  67. }