From 013a67bbe02802c233ce3b8fd38e4501c8b8e1ed Mon Sep 17 00:00:00 2001
From: tastytea
Date: Mon, 15 Jan 2018 00:08:14 +0100
Subject: [PATCH] Added examples
---
.../{example.cpp => example1_dump_json.cpp} | 2 +
src/examples/example2_parse_account.cpp | 66 +++++++++++++
src/examples/example3_mastocron.cpp | 97 +++++++++++++++++++
3 files changed, 165 insertions(+)
rename src/examples/{example.cpp => example1_dump_json.cpp} (94%)
create mode 100644 src/examples/example2_parse_account.cpp
create mode 100644 src/examples/example3_mastocron.cpp
diff --git a/src/examples/example.cpp b/src/examples/example1_dump_json.cpp
similarity index 94%
rename from src/examples/example.cpp
rename to src/examples/example1_dump_json.cpp
index 89944b7..db13ad3 100644
--- a/src/examples/example.cpp
+++ b/src/examples/example1_dump_json.cpp
@@ -1,4 +1,6 @@
/* 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
diff --git a/src/examples/example2_parse_account.cpp b/src/examples/example2_parse_account.cpp
new file mode 100644
index 0000000..035d071
--- /dev/null
+++ b/src/examples/example2_parse_account.cpp
@@ -0,0 +1,66 @@
+/* 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
+#include
+#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] << " \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("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';
+ }
+ }
+ }
+ else
+ {
+ std::cerr << "Error code: " << ret << '\n';
+ return ret;
+ }
+
+ return 0;
+}
diff --git a/src/examples/example3_mastocron.cpp b/src/examples/example3_mastocron.cpp
new file mode 100644
index 0000000..12bfd8a
--- /dev/null
+++ b/src/examples/example3_mastocron.cpp
@@ -0,0 +1,97 @@
+/* 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
+#include
+
+namespace pt = boost::property_tree;
+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::uint16_t ret;
+ pt::ptree config;
+ string lastid = "0";
+ string filename = string(getenv("HOME")) + "/.config/mastocron.json";
+
+ // Read config file, get last seen toot-id
+ try {
+ pt::read_json(filename, config);
+ lastid = config.get(hashtag, "0");
+ }
+ catch (std::exception &e)
+ {
+ // most likely no config file found, ignore
+ }
+
+ // Only get toots we haven't seen yet
+ std::vector params = { "limit=" + limit, "since_id=" + lastid };
+ ret = masto.get(API::v1::timelines_tag_hashtag, 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 != "[]")
+ {
+ std::istringstream iss(answer);
+ pt::ptree tree;
+
+ pt::read_json(iss, tree);
+ for (const pt::ptree::value_type &toot : tree.get_child(""))
+ {
+ string content = toot.second.get("content");
+ 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.second.get("account.display_name")
+ << " (" << toot.second.get("account.acct") << ") at "
+ << toot.second.get("created_at") << "\n";
+ cout << " " << toot.second.get("url") << '\n';
+ cout << "++++++++\n";
+ }
+
+ // Write the id of the newest toot in the config file
+ lastid = tree.front().second.get("id");
+ config.put(hashtag, lastid);
+ pt::write_json(filename, config);
+ }
+ }
+ else
+ {
+ std::cerr << "Error code: " << ret << '\n';
+ return ret;
+ }
+
+ return 0;
+}