switched from rapidjson to jsonpp
This commit is contained in:
parent
010137c0e3
commit
2e2c3fa5b6
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required (VERSION 3.7)
|
||||
project (mastobotmon
|
||||
VERSION 0.1.3
|
||||
VERSION 0.1.4
|
||||
LANGUAGES CXX)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
@ -19,5 +19,5 @@ configure_file (
|
|||
|
||||
file(GLOB sources src/*.cpp)
|
||||
add_executable(mastobotmon ${sources})
|
||||
target_link_libraries(mastobotmon mastodon-cpp)
|
||||
target_link_libraries(mastobotmon mastodon-cpp jsoncpp)
|
||||
install(TARGETS mastobotmon DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
* C++ compiler (tested: gcc 6.4, clang 5.0)
|
||||
* [cmake](https://cmake.org/) (tested: 3.9.6)
|
||||
* [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
|
||||
|
||||
|
|
|
@ -14,17 +14,13 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define RAPIDJSON_HAS_STDSTRING 1
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstring> // strlen()
|
||||
#include <cstdlib> // getenv()
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/stringbuffer.h>
|
||||
#include <rapidjson/prettywriter.h>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include "version.hpp"
|
||||
#include "mastobotmon.hpp"
|
||||
|
||||
|
@ -36,7 +32,7 @@ using std::uint16_t;
|
|||
|
||||
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::stringstream json;
|
||||
|
@ -46,25 +42,27 @@ const bool read_config(rapidjson::Document &document)
|
|||
json << file.rdbuf();
|
||||
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";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!document["accounts"].IsObject())
|
||||
if (!document["accounts"].isObject())
|
||||
{
|
||||
cerr << "ERROR: \"accounts\" not found\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!document["mode"].IsString())
|
||||
if (!document["mode"].isString())
|
||||
{
|
||||
cerr << "ERROR: \"mode\" not found\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!document["daemon_check"].IsUint())
|
||||
if (!document["daemon_check"].isUInt())
|
||||
{
|
||||
cerr << "ERROR: \"daemon_check\" not found\n";
|
||||
return false;
|
||||
|
@ -74,15 +72,10 @@ const bool read_config(rapidjson::Document &document)
|
|||
{
|
||||
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);
|
||||
|
||||
document.AddMember("mode", "cron", allocator);
|
||||
document.AddMember("daemon_check", 10, allocator);
|
||||
document["mode"] = "cron";
|
||||
document["daemon_check"] = 10;
|
||||
|
||||
return write_config(document);
|
||||
}
|
||||
|
@ -121,12 +114,11 @@ const string get_access_token(const string &account)
|
|||
return "";
|
||||
}
|
||||
|
||||
const bool add_account(rapidjson::Document &document)
|
||||
const bool add_account(Json::Value &document)
|
||||
{
|
||||
string account;
|
||||
string minutes;
|
||||
string access_token;
|
||||
rapidjson::Document::AllocatorType &allocator = document.GetAllocator();
|
||||
|
||||
cout << "Adding accounts (user@domain), blank line to stop.\n";
|
||||
while (true)
|
||||
|
@ -141,28 +133,22 @@ const bool add_account(rapidjson::Document &document)
|
|||
std::getline(cin, minutes);
|
||||
access_token = get_access_token(account);
|
||||
|
||||
rapidjson::Value vobject(rapidjson::kObjectType);
|
||||
rapidjson::Value vaccount(account, allocator);
|
||||
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);
|
||||
document["accounts"][account]["minutes"] = std::stoi(minutes);
|
||||
document["accounts"][account]["access_token"] = access_token;
|
||||
}
|
||||
|
||||
return write_config(document);
|
||||
}
|
||||
|
||||
const bool write_config(rapidjson::Document &document)
|
||||
const bool write_config(Json::Value &document)
|
||||
{
|
||||
rapidjson::StringBuffer buffer;
|
||||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
||||
document.Accept(writer);
|
||||
Json::StyledWriter writer;
|
||||
const string output = writer.write(document);
|
||||
|
||||
std::ofstream outfile(filepath);
|
||||
if (outfile.is_open())
|
||||
{
|
||||
outfile.write(buffer.GetString(), std::strlen(buffer.GetString()));
|
||||
outfile.write(output.c_str(), output.length());
|
||||
outfile.close();
|
||||
|
||||
return true;
|
||||
|
|
|
@ -14,8 +14,6 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define RAPIDJSON_HAS_STDSTRING 1
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
@ -25,7 +23,7 @@
|
|||
#include <ctime>
|
||||
#include <sstream>
|
||||
#include <iomanip> // get_time
|
||||
#include <rapidjson/document.h>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include "version.hpp"
|
||||
#include "mastobotmon.hpp"
|
||||
|
||||
|
@ -37,7 +35,7 @@ using std::uint16_t;
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
rapidjson::Document document;
|
||||
Json::Value document;
|
||||
if (!read_config(document))
|
||||
{
|
||||
return 1;
|
||||
|
@ -53,15 +51,15 @@ int main(int argc, char *argv[])
|
|||
|
||||
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
|
||||
string instance = member.name.GetString();
|
||||
string instance = it.name();
|
||||
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_minutes(member.value["minutes"].GetUint());
|
||||
acc->set_minutes((*it)["minutes"].asUInt());
|
||||
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);
|
||||
if (ret == 0)
|
||||
{
|
||||
rapidjson::Document json;
|
||||
json.Parse(answer.c_str());
|
||||
const string id = json["id"].GetString();
|
||||
Json::Value json;
|
||||
Json::Reader reader;
|
||||
reader.parse(answer, json);
|
||||
const string id = json["id"].asString();
|
||||
|
||||
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);
|
||||
if (ret == 0)
|
||||
{
|
||||
json.Parse(answer.c_str());
|
||||
const string acct = json[0]["account"]["acct"].GetString();
|
||||
reader.parse(answer, json);
|
||||
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};
|
||||
isslast >> std::get_time(&tm, "%Y-%m-%dT%T");
|
||||
std::time_t time = timegm(&tm);
|
||||
|
|
|
@ -17,20 +17,18 @@
|
|||
#ifndef mastobotmon_HPP
|
||||
#define mastobotmon_HPP
|
||||
|
||||
#define RAPIDJSON_HAS_STDSTRING 1
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <rapidjson/document.h>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <mastodon-cpp.hpp>
|
||||
|
||||
using std::uint16_t;
|
||||
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 bool add_account(rapidjson::Document &document);
|
||||
const bool write_config(rapidjson::Document &document);
|
||||
const bool add_account(Json::Value &document);
|
||||
const bool write_config(Json::Value &document);
|
||||
|
||||
class Account : public Mastodon::API
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue