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

73 lines
1.8 KiB
C++
Raw 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 <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "../mastodon-cpp.hpp"
using Mastodon::API;
namespace pt = boost::property_tree;
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::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::istringstream iss(answer);
pt::ptree tree;
pt::read_json(iss, tree);
std::string uid = tree.get<std::string>("id");
cout << "Your ID is: " << uid << '\n';
cout << "Your whole acount tree:\n";
for (const pt::ptree::value_type &v : tree.get_child(""))
{
cout << " ";
if (v.second.size() > 0)
{
cout << v.first.data() << ": \n";
for (const pt::ptree::value_type &vc : v.second.get_child(""))
{
cout << " ";
cout << vc.first.data() << ": " << vc.second.data() << '\n';
}
}
else
{
cout << v.first.data() << ": " << v.second.data() << '\n';
}
}
}
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;
}