Added examples
This commit is contained in:
parent
729c026b81
commit
013a67bbe0
@ -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 <iostream>
|
66
src/examples/example2_parse_account.cpp
Normal file
66
src/examples/example2_parse_account.cpp
Normal file
@ -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 <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';
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Error code: " << ret << '\n';
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
97
src/examples/example3_mastocron.cpp
Normal file
97
src/examples/example3_mastocron.cpp
Normal file
@ -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 <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <cstdlib>
|
||||
#include <mastodon-cpp.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
|
||||
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] << " <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::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<string> 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<string>("content");
|
||||
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.second.get<string>("account.display_name")
|
||||
<< " (" << toot.second.get<string>("account.acct") << ") at "
|
||||
<< toot.second.get<string>("created_at") << "\n";
|
||||
cout << " " << toot.second.get<string>("url") << '\n';
|
||||
cout << "++++++++\n";
|
||||
}
|
||||
|
||||
// Write the id of the newest toot in the config file
|
||||
lastid = tree.front().second.get<string>("id");
|
||||
config.put(hashtag, lastid);
|
||||
pt::write_json(filename, config);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Error code: " << ret << '\n';
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user