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/example02_parse_account.cpp

76 lines
1.8 KiB
C++
Raw Permalink Normal View History

2018-01-15 00:08:14 +01:00
/* This file is part of mastodon-cpp.
* This example parses your account data and prints it out in a readable way.
*/
#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-01-15 00:08:14 +01:00
using Mastodon::API;
using std::cout;
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;
2018-01-15 00:08:14 +01:00
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
ss.str(answer);
Json::Value json;
ss >> json;
std::string uid = json["id"].asString();
2018-01-15 00:08:14 +01:00
cout << "Your ID is: " << uid << '\n';
cout << "Your whole acount tree:\n";
for (auto it = json.begin(); it != json.end(); ++it)
2018-01-15 00:08:14 +01:00
{
if (it.name().compare("source") == 0)
2018-01-15 00:08:14 +01:00
{
cout << it.name() << '\n';
for (auto it_s = (*it).begin(); it_s != (*it).end(); ++it_s)
2018-01-15 00:08:14 +01:00
{
cout << '\t' << it_s.name() << ": ";
cout << *it_s << '\n';
2018-01-15 00:08:14 +01:00
}
}
else
{
cout << it.name() << ": ";
cout << *it << '\n';
2018-01-15 00:08:14 +01:00
}
}
}
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-15 00:08:14 +01:00
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}