Removed examples

This commit is contained in:
tastytea 2019-02-22 08:30:38 +01:00
parent 318221a2e6
commit 8c97e8f516
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
17 changed files with 0 additions and 1439 deletions

View File

@ -1,89 +0,0 @@
/* This file is part of mastodon-cpp.
* This example dumps the raw JSON of your last toot with media attached
* and your last 2 followers.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
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]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
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 =
{
{ "id", { uid } },
{ "limit", { "1" } },
{ "only_media", { "1" } }
};
ret = masto.get(API::v1::accounts_id_statuses, 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;
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,75 +0,0 @@
/* 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>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
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;
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();
cout << "Your ID is: " << uid << '\n';
cout << "Your whole acount tree:\n";
for (auto it = json.begin(); it != json.end(); ++it)
{
if (it.name().compare("source") == 0)
{
cout << it.name() << '\n';
for (auto it_s = (*it).begin(); it_s != (*it).end(); ++it_s)
{
cout << '\t' << it_s.name() << ": ";
cout << *it_s << '\n';
}
}
else
{
cout << it.name() << ": ";
cout << *it << '\n';
}
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,134 +0,0 @@
/* This file is part of mastodon-cpp.
* Prints the new toots under a hashtag, designed to be used in cronjobs
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <regex>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <jsoncpp/json/json.h>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
using std::cout;
using std::string;
int main(int argc, char *argv[])
{
string limit = "20";
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <hashtag> [limit]\n";
std::cerr << " Default limit is 20, max limit is 40.\n";
return 1;
}
else if (argc == 4)
{
limit = argv[3];
}
Mastodon::API masto(argv[1], "");
string hashtag = argv[2];
string answer;
std::stringstream ss;
std::uint16_t ret;
Json::Value config;
string lastid = "0";
string filename = string(getenv("HOME")) + "/.config/mastocron.json";
// Read config file, get last seen toot-id
try {
std::ifstream file(filename, std::ifstream::binary);
file >> config;
lastid = config.get(hashtag, "0").asString();
}
catch (std::exception &e)
{
// most likely no config file found, ignore
}
// Only get toots we haven't seen yet
API::parametermap params =
{
{ "hashtag", { hashtag } },
{ "limit", { limit } },
{ "since_id", { lastid } }
};
ret = masto.get(API::v1::timelines_tag_hashtag, params, answer);
if (ret == 0)
{
// If answer is empty, there are no new toots,
// if answer is "[]" there are none at all
if (answer != "" && answer != "[]")
{
string ornament = " +++++";
for (std::uint8_t i = hashtag.size(); i > 0; --i)
{
ornament += "+";
}
cout << ornament << '\n';
cout << " + " << hashtag << ": +\n";
cout << ornament << '\n';
Json::Value tree;
ss.str(answer);
ss >> tree;
for (const auto &toot : tree)
{
string content = toot["content"].asString();
std::regex reparagraph("</p><p>");
std::regex restrip("<[^>]*>");
cout << "++++++++\n";
content = std::regex_replace(content, reparagraph, "\n\n");
cout << std::regex_replace(content, restrip, "") << '\n';
cout << " ";
cout << toot["account"]["display_name"].asString()
<< " (" << toot["account"]["acct"] << ") at "
<< toot["created_at"].asString() << "\n";
cout << " " << toot["url"].asString() << '\n';
for (const auto &media : toot["media_attachments"])
{
cout << "Attachment: <" << media["url"].asString() << ">\n";
}
cout << "++++++++\n";
}
// Write the id of the newest toot in the config file
lastid = tree[0]["id"].asString();
config[hashtag] = lastid;
Json::StreamWriterBuilder wbuilder;
const string output = Json::writeString(wbuilder, config);
std::ofstream outfile(filename);
if (outfile.is_open())
{
outfile.write(output.c_str(), output.length());
outfile.close();
}
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,83 +0,0 @@
/* This file is part of mastodon-cpp.
* Updates your name, description and avatar.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
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]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
// This is the Gimp icon, 24x24px, GPL-3
const std::string avatar =
"data:image/png;base64,"
"iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz"
"AAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAAMdEVY"
"dFRpdGxlAEZvbGRlcuNZL58AAAAUdEVYdEF1dGhvcgBKYWt1YiBTdGVpbmVy5vv3LwAAACF0RVh0"
"U291cmNlAGh0dHA6Ly9qaW1tYWMubXVzaWNoYWxsLmN6aWbjXgAAAFJ0RVh0Q29weXJpZ2h0AEND"
"IEF0dHJpYnV0aW9uLVNoYXJlQWxpa2UgaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbGljZW5z"
"ZXMvYnktc2EvMy4wL16DWrwAAARFSURBVEiJtZXNbxtFGMafGe+ud9eO7bXbNBsSNx9GSaW2aQgK"
"UDiQUmQJiQsHJETFhVNBKuLAhQviT0Aq6g0heqjUS4UQqhYJhCrxoUZKQakIJTFuTGzXjmN7d+31"
"endnhwNNiEIKRWpeaTSjmdHzm/cZzbwkn8/jIIMeqDoA4VGIVCqVZ0DxUuAFn2Wz2dXda/tmUC6X"
"z21sbGQfRrxarb4Mig8BvEkpHdy7vi+AUvokpfSNUqmk/8fJXyeEvD8xNvpcTFUzlNLI3j37W0Rx"
"WKDC/OnTT72gaRoAIJ4YuJ3OaLpl2ZptWtRnjOsjQ1m3a49kUgmp1TJ5FyAPBwCk+bmZ6ZHRUWly"
"YjISi8WkWv3erOd78tkzL0paSiPVaoX//NMiGvUaOrZJ/qG8F2AYBs3n8yEApBKpiBKLq6dmTglj"
"Y+OQJJEoSjSZyRzG1NQxqKoKXdeJqkRx8+Z36PU6D7Rx5w6i0ehkvdH4qLRR/mpk9LFnj5+YEaem"
"j/Hx8Qly7drnxHF6GDoyBMYYlpaWcPHix2i1TRD85QslhP9rBkktee6t8+df5cDg2todDA8PE0VW"
"cOnSJVy5cgXvXHgbAOA4Dq5evYobN24gNzkBgIA80KBdAOazCcZYYkjX6fXrX3JVpFAVOTI39wRU"
"VUIyNYDVtV+RSKaxcGYBs7OzqNc2sFEqwrYsBCFzGWHhAwEAbl++fHkhlTmk3C3+TkK3jaDfhqYl"
"Ydk9tBoVCIIAKSpDllWwkGHxx+9BwdFxfe55QYsxFhqGIQAIt+9zB0AixFwv3Q3anS7cfh89x0Fz"
"8x563SZ6bgh1IA3f9yH1qoj5W2i4IhyzBzWlo+/58INADdxgAEAMQN8wDD+fz7MdAGf8iyAIT7S3"
"Gq9FBKoFYQjXC8A40Gya8Co1MMZAmYsMNbFwpIn+oQgKbhc9VyYkCFYty4oDyAAwAXQNwwgjuVwO"
"ALC8vNyhsnwnoShxRVGyw/qgeiiTJqIoggAIfB+EAJwIECQZHQ/QxD5mkm2kFBHlXkxumu6iZdtV"
"AP79FuwACoUC6TtOJx6PFwWBmv2ADRNCU4TSiBAREFNVpFIaUuk0lHgagXIEdTtEyQz5b20Z82df"
"ERgRp5LJ5POapvXDMCw6jtPdAeRyORQKBViW1REEsciBdcZ4xbS6TttywqbVxWbTJvWtNq1utru1"
"hrleavSKf1j4utzsf2K7XG+1WpOU0izn/HFZlr/Z3Nys7P0qQgBurVYrO45z3XXcHyRJ0kRRHGCM"
"xQMeKAAID7jnc26HnucxxqxOp7Nle7/cSiQGLoii9HQYhkc9zxsEECH7VTTDMHYe6P2eAJABSAA4"
"gOC+x9uvlwPg09PTR2VZfo9zftyyrA+KxeK3+wL2C8MwKP7+Wvguceyaw8mTJ8cIIe96nvfpysrK"
"rYcG/J+oVqvjAOq6rncfScncG7quF7fHB170DxzwJ8uH9gSkOVdYAAAAAElFTkSuQmCC";
API::parametermap params =
{
{ "display_name", { "Botty McBotface" } },
{ "note", { "Beep Bop." } },
{ "avatar", { avatar } }
};
ret = masto.patch(API::v1::accounts_update_credentials, params, answer);
if (ret == 0)
{
std::cout << answer << '\n';
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,102 +0,0 @@
/* This file is part of mastodon-cpp.
* Follow someone, then unfollow.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <chrono>
#include <thread>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
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]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
std::string id;
std::cout << "Follow [ID or username@domain]: ";
std::cin >> id;
// If no @ is found, it is presumably an ID
if (id.find('@') == std::string::npos)
{
ret = masto.post(API::v1::accounts_id_follow, {{"id", {id}}}, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
std::cout << "Unfollowing in 30 seconds...\n";
std::this_thread::sleep_for(std::chrono::seconds(30));
ret = masto.post(API::v1::accounts_id_unfollow, {{"id", {id}}}, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else
{
API::parametermap params =
{
{ "uri", { id } }
};
ret = masto.post(API::v1::follows, params, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
id = answer.substr(7, answer.find("\"", 7) - 7);
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
std::cout << "Unfollowing " << id << " in 30 seconds...\n";
std::this_thread::sleep_for(std::chrono::seconds(30));
ret = masto.post(API::v1::accounts_id_unfollow, {{"id", {id}}}, answer);
if (ret == 0)
{
// std::cout << answer << '\n';
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
return 0;
}

View File

@ -1,74 +0,0 @@
/* This file is part of mastodon-cpp.
* Post a status, then delete it.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <chrono>
#include <thread>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
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]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
std::string toot;
std::string id;
std::cout << "Toot: ";
std::cin >> toot;
API::parametermap parameters =
{
{ "status", { toot } },
{ "visibility", { "unlisted" } },
{ "spoiler_text", { "test" } }
};
ret = masto.post(API::v1::statuses, parameters, answer);
if (ret == 0)
{
id = answer.substr(7, answer.find("\"", 7) - 7);
std::cout << "Deleting in 30 seconds...\n";
std::this_thread::sleep_for(std::chrono::seconds(30));
ret = masto.del(API::v1::statuses_id, {{"id", {id}}});
if (ret == 0)
{
std::cout << "Status " << id << " deleted.\n";
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,88 +0,0 @@
/* This file is part of mastodon-cpp.
* Register as an application.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <instance>\n";
return 1;
}
Mastodon::API masto(argv[1], "");
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
std::string client_id, client_secret, url;
ret = masto.register_app1("test123",
"urn:ietf:wg:oauth:2.0:oob",
"read follow",
"",
client_id,
client_secret,
url);
if (ret == 0)
{
std::cout << "Visit " << url << " to get the code.\n";
std::string code;
std::cout << "Insert code: ";
std::cin >> code;
std::string access_token;
ret = masto.register_app2(client_id,
client_secret,
"urn:ietf:wg:oauth:2.0:oob",
code,
access_token);
if (ret == 0)
{
std::cout << "Success!\nAccess-token: " << access_token << '\n';
std::cout << "Testing access token...\n";
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::cout << answer;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << url << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,57 +0,0 @@
/* This file is part of mastodon-cpp.
* In this example we look at HTTP headers to determine how many calls we are
* allowed to make.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
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]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::string remaining = masto.get_header("X-RateLimit-Remaining");
std::string reset = masto.get_header("X-RateLimit-Reset");
std::cout << "You are allowed to make " << remaining
<< " calls until the counter is reset at " << reset << '\n';
if (std::stoi(remaining) <= 1)
{
std::cout << "🔥 Please let the server cool off a bit! 🔥\n";
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}

View File

@ -1,77 +0,0 @@
/* This file is part of mastodon-cpp.
* Streaming API example
*/
#include <iostream>
#include <string>
#include <cstdint>
#include <thread>
#include <chrono>
#include <memory>
#include <mutex>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access_token>\n";
return 1;
}
static std::string answer;
static std::unique_ptr<Mastodon::API::http> ptr;
std::cout << "Dumping public timeline...\n";
std::thread pub([=]
{
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
masto.get_stream(API::v1::streaming_public, answer, ptr);
});
std::uint8_t counter = 0;
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
if (ptr != nullptr)
{
std::lock_guard<std::mutex> lock(ptr->get_mutex());
++counter;
std::cout << answer;
answer.clear();
if (counter == 10)
{
std::cerr << "Cancelling...\n";
ptr->cancel_stream();
break;
}
}
}
pub.join();
std::cout << '\n';
std::cout << "Dumping the #np tag...\n";
answer = "";
std::thread tag([=]
{
Mastodon::API masto(argv[1], argv[2]);
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
masto.get_stream(API::v1::streaming_hashtag, {{ "hashtag", {"np"} }},
answer, ptr);
});
std::this_thread::sleep_for(std::chrono::seconds(20));
ptr->cancel_stream();
std::cout << answer;
std::cout << '\n';
tag.join();
return 0;
}

View File

@ -1,56 +0,0 @@
/* This file is part of mastodon-cpp.
* Simplify the interface
*/
#include <iostream>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
class EasyToot : public Mastodon::API
{
public:
explicit EasyToot(const std::string &instance, const std::string &access_token)
: API(instance, access_token)
{}
std::uint16_t toot(const std::string &text, const std::string &cw = "")
{
std::string answer;
API::parametermap parameters;
parameters.insert({ "status", { text } } );
if (!cw.empty())
{
parameters.insert({ "spoiler_text", { cw } } );
}
return post(API::v1::statuses, parameters, answer);
}
};
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access_token>\n";
return 1;
}
std::uint16_t ret = 0;
EasyToot masto(argv[1], argv[2]);
ret = masto.toot("Test");
if (ret != 0)
{
std::cerr << "Error: " << ret << '\n';
}
return ret;
}

View File

@ -1,82 +0,0 @@
/* 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>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
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;
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();
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();
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;
}

View File

@ -1,129 +0,0 @@
/* This file is part of mastodon-cpp.
* Prints some information about your last status.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <cstdint>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
// Include all headers in mastodon-cpp/easy/
#include <mastodon-cpp/easy/all.hpp>
#endif
using Mastodon::API;
using Mastodon::Easy;
using std::cout;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Easy masto(argv[1], argv[2]);
std::string answer;
std::uint16_t ret;
// Get own account in order to obtain account ID
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
// Construct an Account object using the JSON string from the server
Easy::Account acc(answer);
// Get last status
ret = masto.get(API::v1::accounts_id_statuses,
{
{ "id", { acc.id() } },
{ "limit", { "1" } }
},
answer);
if (ret == 0)
{
// answer contains an array with a single object. This works because
// Easy::Status will turn that into an object, but an array with
// more than 1 object will not work.
Easy::Status status(answer);
// An Entitiy is valid if the JSON was not empty and contained no
// "error":-key
if (status.valid())
{
if (!status.language().empty())
cout << "Language: " << status.language() << '\n';
if (!status.content().empty())
cout << "Content: "
<< status.content().substr(0, 200) << "\n";
if (!status.application().name().empty())
cout << "Application used: "
<< status.application().name() << '\n';
cout << "ID: " << status.id() << '\n';
string acct;
string url;
std::vector<Easy::Attachment> attachments;
std::vector<Easy::Tag> tags;
// If the status is a reblog, print the original author
if (status.reblog().valid())
{
// status.reblog() is an Easy::Status
// status.reblog().account() is an Easy::Account
cout << "Original ID: " << status.reblog().id() << '\n';
acct = status.reblog().account().acct();
url = status.reblog().account().url();
attachments = status.reblog().media_attachments();
tags = status.reblog().tags();
}
else
{
acct = status.account().acct();
url = status.account().url();
attachments = status.media_attachments();
tags = status.tags();
}
cout << "From: " << acct << " ";
cout << "(" << url << ")\n";
// List attachments, if any
for (const Easy::Attachment &attachment : attachments)
{
cout << "Attachment: " << attachment.text_url()
<< " (" << attachment.size() << ")\n";
}
// List hashtags, if any
for (const Easy::Tag &tag : tags)
{
cout << "Hashtag: #" << tag.name()
<< " (" << tag.url() << ")\n";
}
}
return 0;
}
}
std::cout << answer << '\n';
std::cerr << "Error code: " << ret << '\n';
return ret;
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -1,111 +0,0 @@
/* This file is part of mastodon-cpp.
* Prints some information from the public timeline.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <thread>
#include <memory>
#include <vector>
#include <chrono>
#include <mutex>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
// Include all headers in mastodon-cpp/easy/
#include <mastodon-cpp/easy/all.hpp>
#endif
using Mastodon::API;
using Mastodon::Easy;
using std::cout;
using std::chrono::system_clock;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
cout << "I'll show you the public timeline. Press CTRL-C to cancel\n";
// These have to be static in order to use them in- and outside the thread
static std::string stream;
// You can cancel the stream with this pointer (ptr->cancel_stream())
static std::unique_ptr<Mastodon::API::http> ptr;
// Start a new thread for the stream
std::thread pub_tl([=]
{
Easy masto(argv[1], argv[2]);
masto.get_stream(Mastodon::API::v1::streaming_public, stream, ptr);
});
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
// Skip iteration if ptr points not to the Mastodon::API::http object
if (ptr == nullptr)
{
continue;
}
// Acquire lock for the stream variable
std::lock_guard<std::mutex> lock(ptr->get_mutex());
// Parse event stream and clear it afterwards
std::vector<Easy::stream_event> events = Easy::parse_stream(stream);
stream.clear();
// The contents of the stream are now in a vector of pairs
// { Easy::event_type, std::string }
for (const Easy::stream_event &event : events)
{
Easy::Status status;
Easy::Notification notification;
// Print out some information about the events
switch (event.first)
{
case Easy::event_type::Update:
status.from_string(event.second);
cout << "[" <<
Easy::strtime_local(status.created_at(), "%T") << "] ";
cout << "Status from: " << status.account().acct()
<< " (" << status.url() << ")\n";
break;
case Easy::event_type::Notification:
notification.from_string(event.second);
cout << "Notification involving: "
<< notification.account().acct()
<< " (" << notification.id() << ")\n";
break;
case Easy::event_type::Delete:
cout << "Deleted: " << event.second << '\n';
break;
default:
cout << "Something undefined happened. 😱\n";
}
}
}
pub_tl.join();
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -1,130 +0,0 @@
/* This file is part of mastodon-cpp.
* Prints a Mastodon-conversation in a tree.
* Argument has to be in the form: https://<domain>/<user>/<status id>
*
* This example uses a rather wasteful approach in order to use less code and
* therefore be more clear. Please don't use it on long conversations.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <regex>
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
// Include all headers in mastodon-cpp/easy/
#include <mastodon-cpp/easy/all.hpp>
#endif
using Mastodon::API;
using Mastodon::Easy;
using std::cout;
using std::chrono::system_clock;
// Print a status to stdout, nicely formatted
void format_status(const Easy::Status &status, const std::uint8_t &level)
{
std::string space(level, ' ');
// Remove HTML-tags
std::regex restrip("<[^>]*>");
std::string content = std::regex_replace(status.content(), restrip, "");
cout << space << "+-----------------------------------------\n";
cout << space << "| " << status.account().acct() << ": \n";
while (!content.empty())
{
cout << space << "| " << content.substr(0, 40) << '\n';
if (content.length() > 40)
{
content = content.substr(40);
}
else
{
content.clear();
}
}
cout << space << "| "
<< Easy::strtime_local(status.created_at(), "%T") << '\n';
cout << space << "+-----------------------------------------" << std::endl;
}
// Fetch status and its descendants, output them
std::uint16_t print_status(Easy &masto, const std::string &id,
const std::uint8_t level)
{
std::uint16_t ret;
std::string answer;
API::parametermap parameters =
{
{ "id", { id }}
};
ret = masto.get(Mastodon::API::v1::statuses_id, parameters, answer);
if (ret == 0)
{
format_status(Easy::Status(answer), level);
ret = masto.get(Mastodon::API::v1::statuses_id_context,
parameters, answer);
if (ret == 0)
{
Easy::Context context(answer);
for (const Easy::Status &s : context.descendants())
{
// Only print descendant if it is a direct reply
// NOTE: This is a wasteful approach, I use it only in the
// interest of more clarity. Don't do this at home. :-)
if (s.in_reply_to_id() == id)
{
print_status(masto, s.id(), level + 4);
}
}
}
}
return ret;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <url>\n";
return 1;
}
// Extract domain name
std::string instance = std::string(argv[1]);
std::size_t pos = instance.find('/') + 2;
instance = instance.substr(pos, instance.find('/', pos) - pos);
// Extract status ID
std::string id = std::string(argv[1]);
id = id.substr(id.rfind('/') + 1);
cout << " Instance: " << instance << "\nStatus ID: " << id << "\n\n";
Easy masto(instance, "");
return print_status(masto, id, 0);
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -1,40 +0,0 @@
/* This file is part of mastodon-cpp.
* This example shows how to use a proxy.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using std::cout;
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]);
// SOCKS5 Proxy. Proxy resolves URL hostname.
masto.set_proxy("socks5h://[fd12::1a]:1080/");
// HTTPS proxy with username and password
masto.set_proxy("https://localhost:3128", "user5:supersecurepassword");
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
cout << "Return code: " << ret << '\n';
std::cout << answer << '\n';
return 0;
}

View File

@ -1,57 +0,0 @@
/* This file is part of mastodon-cpp.
* Get fields from own account.
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
#endif
using std::cout;
using Mastodon::API;
using Mastodon::Easy;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Easy masto(argv[1], argv[2]);
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::accounts_verify_credentials, answer);
cout << "Return code: " << ret << '\n';
Easy::Account account(answer);
std::vector<Easy::Account::fields_pair> fields(account.fields());
for (const auto &field : fields)
{
cout << "Name: " << field.first << "\nValue: " << field.second << "\n\n";
}
return 0;
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY

View File

@ -1,55 +0,0 @@
/* This file is part of mastodon-cpp.
* Print the first 20 characters from every bookmark (Glitch-Soc only).
*/
// Don't compile this if the Easy-interface is turned off
#ifndef WITHOUT_EASY
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy/all.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
#endif
using std::cout;
using Mastodon::API;
using Mastodon::Easy;
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
return 1;
}
Easy masto(argv[1], argv[2]);
std::string answer;
std::uint16_t ret;
ret = masto.get(API::v1::bookmarks, answer);
cout << "Return code: " << ret << '\n';
std::vector<string> statuses = Easy::json_array_to_vector(answer);
for (const string &json : statuses)
{
cout << Easy::Status(json).content().substr(0, 20) << '\n';
}
return 0;
}
#else
#include <cstdio>
int main()
{
printf("mastodon-cpp was compiled without Easy support.\n");
return 255;
}
#endif // WITHOUT_EASY