This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/examples/example11_post_media.cpp

83 lines
2.0 KiB
C++
Raw Normal View History

2018-03-13 13:25:01 +01:00
/* This file is part of mastodon-cpp.
* Post the same media 2 times, with URLs
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#include <jsoncpp/json/json.h>
2018-03-21 20:12:45 +01:00
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
2018-03-13 13:25:01 +01:00
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
std::string answer;
std::stringstream ss;
Json::Value json;
2018-03-13 13:25:01 +01:00
std::uint16_t ret;
std::string filepath;
std::cout << "Absolute filename: ";
std::cin >> filepath;
API::parametermap parameters =
{
{ "file", { filepath } }
};
ret = masto.post(API::v1::media, parameters, answer);
if (ret == 0)
{
ss.str(answer);
ss >> json;
std::string image1_id = json["id"].asString();
std::string image1_url = json["url"].asString();
2018-03-13 13:25:01 +01:00
parameters =
{
{ "file", { filepath } }
};
ret = masto.post(API::v1::media, parameters, answer);
if (ret == 0)
{
ss.str(answer);
ss >> json;
std::string image2_id = json["id"].asString();
std::string image2_url = json["url"].asString();
2018-03-13 13:25:01 +01:00
parameters =
{
{ "status", { image1_url + " \n" + image2_url } },
{ "media_ids", { image1_id, image2_id } },
{ "visibility", { "direct" } }
};
ret = masto.post(API::v1::statuses, parameters, answer);
if (ret == 0)
{
return 0;
}
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
std::cout << answer << '\n';
std::cerr << "Error code: " << ret << '\n';
return ret;
}