From 8c97e8f516981fbaf9b43b584c2516108391b697 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 22 Feb 2019 08:30:38 +0100 Subject: [PATCH] Removed examples --- examples/example01_dump_json.cpp | 89 -------------- examples/example02_parse_account.cpp | 75 ------------ examples/example03_mastocron.cpp | 134 ---------------------- examples/example04_update_credentials.cpp | 83 -------------- examples/example05_follow_unfollow.cpp | 102 ---------------- examples/example06_toot_delete-toot.cpp | 74 ------------ examples/example07_register_app.cpp | 88 -------------- examples/example08_rate_limiting.cpp | 57 --------- examples/example09_streaming_api.cpp | 77 ------------- examples/example10_simplify.cpp | 56 --------- examples/example11_post_media.cpp | 82 ------------- examples/example12_easy_laststatus.cpp | 129 --------------------- examples/example13_easy_stream.cpp | 111 ------------------ examples/example14_easy_treeview.cpp | 130 --------------------- examples/example15_proxy.cpp | 40 ------- examples/example16_account_fields.cpp | 57 --------- examples/example17_bookmarks.cpp | 55 --------- 17 files changed, 1439 deletions(-) delete mode 100644 examples/example01_dump_json.cpp delete mode 100644 examples/example02_parse_account.cpp delete mode 100644 examples/example03_mastocron.cpp delete mode 100644 examples/example04_update_credentials.cpp delete mode 100644 examples/example05_follow_unfollow.cpp delete mode 100644 examples/example06_toot_delete-toot.cpp delete mode 100644 examples/example07_register_app.cpp delete mode 100644 examples/example08_rate_limiting.cpp delete mode 100644 examples/example09_streaming_api.cpp delete mode 100644 examples/example10_simplify.cpp delete mode 100644 examples/example11_post_media.cpp delete mode 100644 examples/example12_easy_laststatus.cpp delete mode 100644 examples/example13_easy_stream.cpp delete mode 100644 examples/example14_easy_treeview.cpp delete mode 100644 examples/example15_proxy.cpp delete mode 100644 examples/example16_account_fields.cpp delete mode 100644 examples/example17_bookmarks.cpp diff --git a/examples/example01_dump_json.cpp b/examples/example01_dump_json.cpp deleted file mode 100644 index b47a1f5..0000000 --- a/examples/example01_dump_json.cpp +++ /dev/null @@ -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 -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example02_parse_account.cpp b/examples/example02_parse_account.cpp deleted file mode 100644 index 9ecfcc4..0000000 --- a/examples/example02_parse_account.cpp +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; -using std::cout; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example03_mastocron.cpp b/examples/example03_mastocron.cpp deleted file mode 100644 index cf14c9a..0000000 --- a/examples/example03_mastocron.cpp +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#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] << " [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("

"); - 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; -} diff --git a/examples/example04_update_credentials.cpp b/examples/example04_update_credentials.cpp deleted file mode 100644 index 7278e9a..0000000 --- a/examples/example04_update_credentials.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* This file is part of mastodon-cpp. - * Updates your name, description and avatar. - */ - -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example05_follow_unfollow.cpp b/examples/example05_follow_unfollow.cpp deleted file mode 100644 index 6a4e887..0000000 --- a/examples/example05_follow_unfollow.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* This file is part of mastodon-cpp. - * Follow someone, then unfollow. - */ - -#include -#include -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example06_toot_delete-toot.cpp b/examples/example06_toot_delete-toot.cpp deleted file mode 100644 index 2f90189..0000000 --- a/examples/example06_toot_delete-toot.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* This file is part of mastodon-cpp. - * Post a status, then delete it. - */ - -#include -#include -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example07_register_app.cpp b/examples/example07_register_app.cpp deleted file mode 100644 index 9ff72eb..0000000 --- a/examples/example07_register_app.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* This file is part of mastodon-cpp. - * Register as an application. - */ - -#include -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 2) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example08_rate_limiting.cpp b/examples/example08_rate_limiting.cpp deleted file mode 100644 index 1255ccc..0000000 --- a/examples/example08_rate_limiting.cpp +++ /dev/null @@ -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 -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example09_streaming_api.cpp b/examples/example09_streaming_api.cpp deleted file mode 100644 index 3f8d7c3..0000000 --- a/examples/example09_streaming_api.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* This file is part of mastodon-cpp. - * Streaming API example - */ - - -#include -#include -#include -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \n"; - return 1; - } - - static std::string answer; - static std::unique_ptr 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 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; -} diff --git a/examples/example10_simplify.cpp b/examples/example10_simplify.cpp deleted file mode 100644 index 0212cf6..0000000 --- a/examples/example10_simplify.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* This file is part of mastodon-cpp. - * Simplify the interface - */ - - -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#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] << " \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; -} diff --git a/examples/example11_post_media.cpp b/examples/example11_post_media.cpp deleted file mode 100644 index 0948ef3..0000000 --- a/examples/example11_post_media.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* This file is part of mastodon-cpp. - * Post the same media 2 times, with URLs - */ - -#include -#include -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example12_easy_laststatus.cpp b/examples/example12_easy_laststatus.cpp deleted file mode 100644 index d5de54a..0000000 --- a/examples/example12_easy_laststatus.cpp +++ /dev/null @@ -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 -#include -#include - -// If we are compiling mastodon-cpp, use another include path -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" - #include "easy/all.hpp" -#else - #include - // Include all headers in mastodon-cpp/easy/ - #include -#endif - -using Mastodon::API; -using Mastodon::Easy; -using std::cout; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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 attachments; - std::vector 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 -int main() -{ - printf("mastodon-cpp was compiled without Easy support.\n"); - return 255; -} -#endif // WITHOUT_EASY diff --git a/examples/example13_easy_stream.cpp b/examples/example13_easy_stream.cpp deleted file mode 100644 index 444ddfc..0000000 --- a/examples/example13_easy_stream.cpp +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include - -// If we are compiling mastodon-cpp, use another include path -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" - #include "easy/all.hpp" -#else - #include - // Include all headers in mastodon-cpp/easy/ - #include -#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] << " \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 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 lock(ptr->get_mutex()); - - // Parse event stream and clear it afterwards - std::vector 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 -int main() -{ - printf("mastodon-cpp was compiled without Easy support.\n"); - return 255; -} -#endif // WITHOUT_EASY diff --git a/examples/example14_easy_treeview.cpp b/examples/example14_easy_treeview.cpp deleted file mode 100644 index 677536b..0000000 --- a/examples/example14_easy_treeview.cpp +++ /dev/null @@ -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://// - * - * 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 -#include -#include -#include -#include -#include - -// If we are compiling mastodon-cpp, use another include path -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" - #include "easy/all.hpp" -#else - #include - // Include all headers in mastodon-cpp/easy/ - #include -#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] << " \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 -int main() -{ - printf("mastodon-cpp was compiled without Easy support.\n"); - return 255; -} -#endif // WITHOUT_EASY diff --git a/examples/example15_proxy.cpp b/examples/example15_proxy.cpp deleted file mode 100644 index 1db27f5..0000000 --- a/examples/example15_proxy.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* This file is part of mastodon-cpp. - * This example shows how to use a proxy. - */ - -#include -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" -#else - #include -#endif - -using std::cout; -using Mastodon::API; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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; -} diff --git a/examples/example16_account_fields.cpp b/examples/example16_account_fields.cpp deleted file mode 100644 index 6a05a7e..0000000 --- a/examples/example16_account_fields.cpp +++ /dev/null @@ -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 -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" - #include "easy/all.hpp" -#else - #include - #include -#endif - -using std::cout; -using Mastodon::API; -using Mastodon::Easy; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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 fields(account.fields()); - - for (const auto &field : fields) - { - cout << "Name: " << field.first << "\nValue: " << field.second << "\n\n"; - } - - return 0; -} - -#else -#include -int main() -{ - printf("mastodon-cpp was compiled without Easy support.\n"); - return 255; -} -#endif // WITHOUT_EASY diff --git a/examples/example17_bookmarks.cpp b/examples/example17_bookmarks.cpp deleted file mode 100644 index 7677a68..0000000 --- a/examples/example17_bookmarks.cpp +++ /dev/null @@ -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 -#include -#include -#include -#ifdef MASTODON_CPP - #include "mastodon-cpp.hpp" - #include "easy/all.hpp" -#else - #include - #include -#endif - -using std::cout; -using Mastodon::API; -using Mastodon::Easy; - -int main(int argc, char *argv[]) -{ - if (argc < 3) - { - std::cerr << "usage: " << argv[0] << " \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 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 -int main() -{ - printf("mastodon-cpp was compiled without Easy support.\n"); - return 255; -} -#endif // WITHOUT_EASY