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.

example11_post_media.cpp 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* This file is part of mastodon-cpp.
  2. * Post the same media 2 times, with URLs
  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. int main(int argc, char *argv[])
  17. {
  18. if (argc < 3)
  19. {
  20. std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
  21. return 1;
  22. }
  23. Mastodon::API masto(argv[1], argv[2]);
  24. std::string answer;
  25. std::stringstream ss;
  26. Json::Value json;
  27. std::uint16_t ret;
  28. std::string filepath;
  29. std::cout << "Absolute filename: ";
  30. std::cin >> filepath;
  31. API::parametermap parameters =
  32. {
  33. { "file", { filepath } }
  34. };
  35. ret = masto.post(API::v1::media, parameters, answer);
  36. if (ret == 0)
  37. {
  38. ss.str(answer);
  39. ss >> json;
  40. std::string image1_id = json["id"].asString();
  41. std::string image1_url = json["url"].asString();
  42. parameters =
  43. {
  44. { "file", { filepath } }
  45. };
  46. ret = masto.post(API::v1::media, parameters, answer);
  47. if (ret == 0)
  48. {
  49. ss.str(answer);
  50. ss >> json;
  51. std::string image2_id = json["id"].asString();
  52. std::string image2_url = json["url"].asString();
  53. parameters =
  54. {
  55. { "status", { image1_url + " \n" + image2_url } },
  56. { "media_ids", { image1_id, image2_id } },
  57. { "visibility", { "direct" } }
  58. };
  59. ret = masto.post(API::v1::statuses, parameters, answer);
  60. if (ret == 0)
  61. {
  62. return 0;
  63. }
  64. }
  65. }
  66. else if (ret == 13)
  67. {
  68. std::cerr << "The URL has permanently changed.\n" <<
  69. "New URL: " << answer << '\n';
  70. return ret;
  71. }
  72. std::cout << answer << '\n';
  73. std::cerr << "Error code: " << ret << '\n';
  74. return ret;
  75. }