switched from rapidjson to jsonpp
This commit is contained in:
parent
010137c0e3
commit
2e2c3fa5b6
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project (mastobotmon
|
project (mastobotmon
|
||||||
VERSION 0.1.3
|
VERSION 0.1.4
|
||||||
LANGUAGES CXX)
|
LANGUAGES CXX)
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
|
@ -19,5 +19,5 @@ configure_file (
|
||||||
|
|
||||||
file(GLOB sources src/*.cpp)
|
file(GLOB sources src/*.cpp)
|
||||||
add_executable(mastobotmon ${sources})
|
add_executable(mastobotmon ${sources})
|
||||||
target_link_libraries(mastobotmon mastodon-cpp)
|
target_link_libraries(mastobotmon mastodon-cpp jsoncpp)
|
||||||
install(TARGETS mastobotmon DESTINATION ${CMAKE_INSTALL_BINDIR})
|
install(TARGETS mastobotmon DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
* C++ compiler (tested: gcc 6.4, clang 5.0)
|
* C++ compiler (tested: gcc 6.4, clang 5.0)
|
||||||
* [cmake](https://cmake.org/) (tested: 3.9.6)
|
* [cmake](https://cmake.org/) (tested: 3.9.6)
|
||||||
* [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.4.4)
|
* [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.4.4)
|
||||||
* [rapidjson](http://rapidjson.org/) (tested: 1.1.0)
|
* [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.1)
|
||||||
|
|
||||||
## Get sourcecode
|
## Get sourcecode
|
||||||
|
|
||||||
|
|
|
@ -14,17 +14,13 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define RAPIDJSON_HAS_STDSTRING 1
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring> // strlen()
|
#include <cstring> // strlen()
|
||||||
#include <cstdlib> // getenv()
|
#include <cstdlib> // getenv()
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <rapidjson/document.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include <rapidjson/stringbuffer.h>
|
|
||||||
#include <rapidjson/prettywriter.h>
|
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "mastobotmon.hpp"
|
#include "mastobotmon.hpp"
|
||||||
|
|
||||||
|
@ -36,7 +32,7 @@ using std::uint16_t;
|
||||||
|
|
||||||
const string filepath = string(std::getenv("HOME")) + "/.config/mastobotmon.json";
|
const string filepath = string(std::getenv("HOME")) + "/.config/mastobotmon.json";
|
||||||
|
|
||||||
const bool read_config(rapidjson::Document &document)
|
const bool read_config(Json::Value &document)
|
||||||
{
|
{
|
||||||
std::ifstream file(filepath);
|
std::ifstream file(filepath);
|
||||||
std::stringstream json;
|
std::stringstream json;
|
||||||
|
@ -46,25 +42,27 @@ const bool read_config(rapidjson::Document &document)
|
||||||
json << file.rdbuf();
|
json << file.rdbuf();
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
if (document.Parse(json.str().c_str()).HasParseError())
|
Json::Reader reader;
|
||||||
|
|
||||||
|
if (!reader.parse(json, document))
|
||||||
{
|
{
|
||||||
cerr << "ERROR: couldn't parse config file. Are you sure the JSON is well-formed?\n";
|
cerr << "ERROR: couldn't parse config file. Are you sure the JSON is well-formed?\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document["accounts"].IsObject())
|
if (!document["accounts"].isObject())
|
||||||
{
|
{
|
||||||
cerr << "ERROR: \"accounts\" not found\n";
|
cerr << "ERROR: \"accounts\" not found\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document["mode"].IsString())
|
if (!document["mode"].isString())
|
||||||
{
|
{
|
||||||
cerr << "ERROR: \"mode\" not found\n";
|
cerr << "ERROR: \"mode\" not found\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document["daemon_check"].IsUint())
|
if (!document["daemon_check"].isUInt())
|
||||||
{
|
{
|
||||||
cerr << "ERROR: \"daemon_check\" not found\n";
|
cerr << "ERROR: \"daemon_check\" not found\n";
|
||||||
return false;
|
return false;
|
||||||
|
@ -74,15 +72,10 @@ const bool read_config(rapidjson::Document &document)
|
||||||
{
|
{
|
||||||
cout << "No config file found. Creating new one.\n";
|
cout << "No config file found. Creating new one.\n";
|
||||||
|
|
||||||
rapidjson::Document::AllocatorType &allocator = document.GetAllocator();
|
|
||||||
|
|
||||||
document.SetObject();
|
|
||||||
rapidjson::Value object(rapidjson::kObjectType);
|
|
||||||
document.AddMember("accounts", object.Move(), allocator);
|
|
||||||
add_account(document);
|
add_account(document);
|
||||||
|
|
||||||
document.AddMember("mode", "cron", allocator);
|
document["mode"] = "cron";
|
||||||
document.AddMember("daemon_check", 10, allocator);
|
document["daemon_check"] = 10;
|
||||||
|
|
||||||
return write_config(document);
|
return write_config(document);
|
||||||
}
|
}
|
||||||
|
@ -121,12 +114,11 @@ const string get_access_token(const string &account)
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool add_account(rapidjson::Document &document)
|
const bool add_account(Json::Value &document)
|
||||||
{
|
{
|
||||||
string account;
|
string account;
|
||||||
string minutes;
|
string minutes;
|
||||||
string access_token;
|
string access_token;
|
||||||
rapidjson::Document::AllocatorType &allocator = document.GetAllocator();
|
|
||||||
|
|
||||||
cout << "Adding accounts (user@domain), blank line to stop.\n";
|
cout << "Adding accounts (user@domain), blank line to stop.\n";
|
||||||
while (true)
|
while (true)
|
||||||
|
@ -141,28 +133,22 @@ const bool add_account(rapidjson::Document &document)
|
||||||
std::getline(cin, minutes);
|
std::getline(cin, minutes);
|
||||||
access_token = get_access_token(account);
|
access_token = get_access_token(account);
|
||||||
|
|
||||||
rapidjson::Value vobject(rapidjson::kObjectType);
|
document["accounts"][account]["minutes"] = std::stoi(minutes);
|
||||||
rapidjson::Value vaccount(account, allocator);
|
document["accounts"][account]["access_token"] = access_token;
|
||||||
rapidjson::Value vaccess_token(access_token, allocator);
|
|
||||||
|
|
||||||
vobject.AddMember("minutes", std::stoi(minutes), allocator);
|
|
||||||
vobject.AddMember("access_token", vaccess_token.Move(), allocator);
|
|
||||||
document["accounts"].AddMember(vaccount.Move(), vobject, allocator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return write_config(document);
|
return write_config(document);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool write_config(rapidjson::Document &document)
|
const bool write_config(Json::Value &document)
|
||||||
{
|
{
|
||||||
rapidjson::StringBuffer buffer;
|
Json::StyledWriter writer;
|
||||||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
const string output = writer.write(document);
|
||||||
document.Accept(writer);
|
|
||||||
|
|
||||||
std::ofstream outfile(filepath);
|
std::ofstream outfile(filepath);
|
||||||
if (outfile.is_open())
|
if (outfile.is_open())
|
||||||
{
|
{
|
||||||
outfile.write(buffer.GetString(), std::strlen(buffer.GetString()));
|
outfile.write(output.c_str(), output.length());
|
||||||
outfile.close();
|
outfile.close();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -14,8 +14,6 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define RAPIDJSON_HAS_STDSTRING 1
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
@ -25,7 +23,7 @@
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip> // get_time
|
#include <iomanip> // get_time
|
||||||
#include <rapidjson/document.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "mastobotmon.hpp"
|
#include "mastobotmon.hpp"
|
||||||
|
|
||||||
|
@ -37,7 +35,7 @@ using std::uint16_t;
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
rapidjson::Document document;
|
Json::Value document;
|
||||||
if (!read_config(document))
|
if (!read_config(document))
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -53,15 +51,15 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
std::vector<Account> accounts;
|
std::vector<Account> accounts;
|
||||||
|
|
||||||
for (const auto &member : document["accounts"].GetObject())
|
for (auto it = document["accounts"].begin(); it != document["accounts"].end(); ++it)
|
||||||
{
|
{
|
||||||
// Construct an Account object for every account and store it in a vector
|
// Construct an Account object for every account and store it in a vector
|
||||||
string instance = member.name.GetString();
|
string instance = it.name();
|
||||||
instance = instance.substr(instance.find('@') + 1);
|
instance = instance.substr(instance.find('@') + 1);
|
||||||
|
|
||||||
Account *acc = new Account(instance, member.value["access_token"].GetString());
|
Account *acc = new Account(instance, (*it)["access_token"].asString());
|
||||||
acc->set_useragent("mastobotmon/" + string(global::version));
|
acc->set_useragent("mastobotmon/" + string(global::version));
|
||||||
acc->set_minutes(member.value["minutes"].GetUint());
|
acc->set_minutes((*it)["minutes"].asUInt());
|
||||||
accounts.push_back(*acc);
|
accounts.push_back(*acc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,9 +71,10 @@ int main(int argc, char *argv[])
|
||||||
uint16_t ret = acc.get(Mastodon::API::v1::accounts_verify_credentials, answer);
|
uint16_t ret = acc.get(Mastodon::API::v1::accounts_verify_credentials, answer);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
{
|
{
|
||||||
rapidjson::Document json;
|
Json::Value json;
|
||||||
json.Parse(answer.c_str());
|
Json::Reader reader;
|
||||||
const string id = json["id"].GetString();
|
reader.parse(answer, json);
|
||||||
|
const string id = json["id"].asString();
|
||||||
|
|
||||||
Account::parametermap parameters(
|
Account::parametermap parameters(
|
||||||
{
|
{
|
||||||
|
@ -84,10 +83,10 @@ int main(int argc, char *argv[])
|
||||||
ret = acc.get(Mastodon::API::v1::accounts_id_statuses, id, parameters, answer);
|
ret = acc.get(Mastodon::API::v1::accounts_id_statuses, id, parameters, answer);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
{
|
{
|
||||||
json.Parse(answer.c_str());
|
reader.parse(answer, json);
|
||||||
const string acct = json[0]["account"]["acct"].GetString();
|
const string acct = json[0]["account"]["acct"].asString();
|
||||||
|
|
||||||
std::istringstream isslast(json[0]["created_at"].GetString());
|
std::istringstream isslast(json[0]["created_at"].asString());
|
||||||
struct std::tm tm = {0};
|
struct std::tm tm = {0};
|
||||||
isslast >> std::get_time(&tm, "%Y-%m-%dT%T");
|
isslast >> std::get_time(&tm, "%Y-%m-%dT%T");
|
||||||
std::time_t time = timegm(&tm);
|
std::time_t time = timegm(&tm);
|
||||||
|
|
|
@ -17,20 +17,18 @@
|
||||||
#ifndef mastobotmon_HPP
|
#ifndef mastobotmon_HPP
|
||||||
#define mastobotmon_HPP
|
#define mastobotmon_HPP
|
||||||
|
|
||||||
#define RAPIDJSON_HAS_STDSTRING 1
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <rapidjson/document.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include <mastodon-cpp.hpp>
|
#include <mastodon-cpp.hpp>
|
||||||
|
|
||||||
using std::uint16_t;
|
using std::uint16_t;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
const bool read_config(rapidjson::Document &document);
|
const bool read_config(Json::Value &document);
|
||||||
const string get_access_token(const string &account);
|
const string get_access_token(const string &account);
|
||||||
const bool add_account(rapidjson::Document &document);
|
const bool add_account(Json::Value &document);
|
||||||
const bool write_config(rapidjson::Document &document);
|
const bool write_config(Json::Value &document);
|
||||||
|
|
||||||
class Account : public Mastodon::API
|
class Account : public Mastodon::API
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue