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/src/examples/example01_dump_json.cpp

85 lines
2.0 KiB
C++
Raw Normal View History

2018-01-06 21:33:52 +01:00
/* This file is part of mastodon-cpp.
2018-01-15 00:08:14 +01:00
* This example dumps the raw JSON of your last toot with media attached
* and your last 2 followers.
2018-01-06 21:33:52 +01:00
*/
2018-01-09 22:12:11 +01:00
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
2018-01-06 21:33:52 +01:00
#include "../mastodon-cpp.hpp"
using Mastodon::API;
2018-01-06 21:33:52 +01:00
int main(int argc, char *argv[])
{
2018-01-09 22:12:11 +01:00
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Mastodon::API masto(argv[1], argv[2]);
2018-01-13 16:48:52 +01:00
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
2018-01-09 22:12:11 +01:00
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::cout << "Your last toot with media attached:\n";
std::string uid = answer.substr(7, answer.find("\"", 7) - 7);
API::parametermap parameters =
{
{ "limit", { "1" } },
{ "only_media", { "1" } }
};
2018-01-13 16:07:00 +01:00
ret = masto.get(API::v1::accounts_id_statuses, uid,parameters, answer);
if (ret == 0)
{
std::cout << answer << '\n';
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
std::cout << "\nYour last 2 followers:\n";
parameters =
{
{
"limit", { "2" }
},
{
"exclude_types", { "favourite", "reblog", "mention" }
}
};
ret = masto.get(API::v1::notifications, parameters, answer);
if (ret == 0)
{
std::cout << answer << '\n';
return 0;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
2018-02-28 22:37:30 +01:00
else if (ret == 13)
2018-02-17 20:01:51 +01:00
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
2018-01-13 16:07:00 +01:00
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
2018-01-06 21:33:52 +01:00
}