refactoring
This commit is contained in:
parent
59ff7f8821
commit
c3d7a33be3
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project (mastobotmon
|
project (mastobotmon
|
||||||
VERSION 0.2.0
|
VERSION 0.2.1
|
||||||
LANGUAGES CXX)
|
LANGUAGES CXX)
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
|
|
|
@ -54,6 +54,7 @@ If you use a debug build, you get more verbose error messages.
|
||||||
"accounts" : {
|
"accounts" : {
|
||||||
"account1@example.social" : {
|
"account1@example.social" : {
|
||||||
"access_token" : "xxxx",
|
"access_token" : "xxxx",
|
||||||
|
// Alert after this many minutes of inactivity
|
||||||
"minutes" : 720
|
"minutes" : 720
|
||||||
},
|
},
|
||||||
"account2@example.social" : {
|
"account2@example.social" : {
|
||||||
|
@ -61,8 +62,11 @@ If you use a debug build, you get more verbose error messages.
|
||||||
"minutes" : 1450
|
"minutes" : 1450
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"daemon_check" : 10,
|
// In daemon mode, check every x minutes
|
||||||
|
"daemon_check" : 60,
|
||||||
|
// This is the directory where the mentions and statistics will be stored.
|
||||||
"data_dir" : "/home/user/mastobotmon",
|
"data_dir" : "/home/user/mastobotmon",
|
||||||
|
// Possible values: cron (daemon mode later)
|
||||||
"mode" : "cron"
|
"mode" : "cron"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring> // strlen()
|
|
||||||
#include <cstdlib> // getenv()
|
#include <cstdlib> // getenv()
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -32,7 +31,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(Json::Value &document)
|
const bool read_config()
|
||||||
{
|
{
|
||||||
std::ifstream file(filepath);
|
std::ifstream file(filepath);
|
||||||
std::stringstream json;
|
std::stringstream json;
|
||||||
|
@ -44,31 +43,31 @@ const bool read_config(Json::Value &document)
|
||||||
|
|
||||||
Json::Reader reader;
|
Json::Reader reader;
|
||||||
|
|
||||||
if (!reader.parse(json, document))
|
if (!reader.parse(json, config))
|
||||||
{
|
{
|
||||||
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 (!config["accounts"].isObject())
|
||||||
{
|
{
|
||||||
cerr << "ERROR: \"accounts\" not found\n";
|
cerr << "ERROR: \"accounts\" not found\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document["mode"].isString())
|
if (!config["mode"].isString())
|
||||||
{
|
{
|
||||||
cerr << "ERROR: \"mode\" not found\n";
|
cerr << "ERROR: \"mode\" not found\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document["daemon_check"].isUInt())
|
if (!config["daemon_check"].isUInt())
|
||||||
{
|
{
|
||||||
cerr << "ERROR: \"daemon_check\" not found\n";
|
cerr << "ERROR: \"daemon_check\" not found\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document["data_dir"].isString())
|
if (!config["data_dir"].isString())
|
||||||
{
|
{
|
||||||
cerr << "ERROR: \"data_dir\" not found\n";
|
cerr << "ERROR: \"data_dir\" not found\n";
|
||||||
return false;
|
return false;
|
||||||
|
@ -78,13 +77,13 @@ const bool read_config(Json::Value &document)
|
||||||
{
|
{
|
||||||
cout << "No config file found. Creating new one.\n";
|
cout << "No config file found. Creating new one.\n";
|
||||||
|
|
||||||
add_account(document);
|
add_account();
|
||||||
|
|
||||||
document["mode"] = "cron";
|
config["mode"] = "cron";
|
||||||
document["daemon_check"] = 10;
|
config["daemon_check"] = 60;
|
||||||
document["data_dir"] = ".";
|
config["data_dir"] = ".";
|
||||||
|
|
||||||
return write_config(document);
|
return write_config();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -121,7 +120,7 @@ const string get_access_token(const string &account)
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool add_account(Json::Value &document)
|
const bool add_account()
|
||||||
{
|
{
|
||||||
string account;
|
string account;
|
||||||
string minutes;
|
string minutes;
|
||||||
|
@ -140,17 +139,17 @@ const bool add_account(Json::Value &document)
|
||||||
std::getline(cin, minutes);
|
std::getline(cin, minutes);
|
||||||
access_token = get_access_token(account);
|
access_token = get_access_token(account);
|
||||||
|
|
||||||
document["accounts"][account]["minutes"] = std::stoi(minutes);
|
config["accounts"][account]["minutes"] = std::stoi(minutes);
|
||||||
document["accounts"][account]["access_token"] = access_token;
|
config["accounts"][account]["access_token"] = access_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
return write_config(document);
|
return write_config();
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool write_config(Json::Value &document)
|
const bool write_config()
|
||||||
{
|
{
|
||||||
Json::StyledWriter writer;
|
Json::StyledWriter writer;
|
||||||
const string output = writer.write(document);
|
const string output = writer.write(config);
|
||||||
|
|
||||||
std::ofstream outfile(filepath);
|
std::ofstream outfile(filepath);
|
||||||
if (outfile.is_open())
|
if (outfile.is_open())
|
||||||
|
|
|
@ -35,14 +35,16 @@ using std::cin;
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::uint16_t;
|
using std::uint16_t;
|
||||||
|
|
||||||
const bool write_mentions(const string &filepath, Json::Value &mentions)
|
Json::Value config; // Declared in mastobotmon.hpp
|
||||||
|
|
||||||
|
const bool write_mentions(const string &straccount, Json::Value &mentions)
|
||||||
{
|
{
|
||||||
|
const string filepath = config["data_dir"].asString() + "/mentions_" + straccount + ".csv";
|
||||||
const std::regex restrip("<[^>]*>");
|
const std::regex restrip("<[^>]*>");
|
||||||
|
|
||||||
std::ofstream outfile(filepath, std::ios::app);
|
std::ofstream outfile(filepath, std::ios::app);
|
||||||
if (outfile.is_open())
|
if (outfile.is_open())
|
||||||
{
|
{
|
||||||
cout << filepath << '\n';
|
|
||||||
string output;
|
string output;
|
||||||
for (auto &mention : mentions)
|
for (auto &mention : mentions)
|
||||||
{
|
{
|
||||||
|
@ -53,20 +55,19 @@ const bool write_mentions(const string &filepath, Json::Value &mentions)
|
||||||
outfile.write(output.c_str(), output.length());
|
outfile.write(output.c_str(), output.length());
|
||||||
}
|
}
|
||||||
outfile.close();
|
outfile.close();
|
||||||
|
cout << "New mentions in: " << filepath << '\n';
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
cout << "NOT OPEN" << '\n';
|
|
||||||
cout << filepath << '\n';
|
|
||||||
|
|
||||||
|
cerr << "Error writing file: " << filepath << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Json::Value document;
|
|
||||||
uint16_t mainret = 0;
|
uint16_t mainret = 0;
|
||||||
if (!read_config(document))
|
if (!read_config())
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -75,13 +76,13 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if ((std::strncmp(argv[1], "add", 3)) == 0)
|
if ((std::strncmp(argv[1], "add", 3)) == 0)
|
||||||
{
|
{
|
||||||
add_account(document);
|
add_account();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Account> accounts;
|
std::vector<Account> accounts;
|
||||||
|
|
||||||
for (auto it = document["accounts"].begin(); it != document["accounts"].end(); ++it)
|
for (auto it = config["accounts"].begin(); it != config["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 = it.name();
|
string instance = it.name();
|
||||||
|
@ -97,7 +98,7 @@ int main(int argc, char *argv[])
|
||||||
accounts.push_back(*acc);
|
accounts.push_back(*acc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (document["mode"] == "cron")
|
if (config["mode"] == "cron")
|
||||||
{
|
{
|
||||||
for (Account &acc : accounts)
|
for (Account &acc : accounts)
|
||||||
{
|
{
|
||||||
|
@ -165,8 +166,8 @@ int main(int argc, char *argv[])
|
||||||
const std::uint64_t lastid = std::stoull(json[0]["id"].asString());
|
const std::uint64_t lastid = std::stoull(json[0]["id"].asString());
|
||||||
const string straccount = acct + "@" + acc.get_instance();
|
const string straccount = acct + "@" + acc.get_instance();
|
||||||
acc.set_last_mention_id(lastid);
|
acc.set_last_mention_id(lastid);
|
||||||
document["accounts"][straccount]["last_mention"] = lastid;
|
config["accounts"][straccount]["last_mention"] = lastid;
|
||||||
write_mentions(document["data_dir"].asString() + "/mentions_" + straccount + ".csv", json);
|
write_mentions(straccount, json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,7 +181,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!write_config(document))
|
if (!write_config())
|
||||||
{
|
{
|
||||||
cerr << "Couldn't write config file\n";
|
cerr << "Couldn't write config file\n";
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
using std::uint16_t;
|
using std::uint16_t;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
extern Json::Value config;
|
||||||
|
|
||||||
class Account : public Mastodon::API
|
class Account : public Mastodon::API
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -39,11 +41,11 @@ private:
|
||||||
std::uint64_t _last_mention_id;
|
std::uint64_t _last_mention_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
const bool read_config(Json::Value &document);
|
const bool read_config();
|
||||||
const string get_access_token(const string &account);
|
const string get_access_token(const string &account);
|
||||||
const bool add_account(Json::Value &document);
|
const bool add_account();
|
||||||
const bool write_config(Json::Value &document);
|
const bool write_config();
|
||||||
|
|
||||||
const bool write_mentions(const string &filepath, Json::Value &mentions);
|
const bool write_mentions(const string &straccount, Json::Value &mentions);
|
||||||
|
|
||||||
#endif // mastobotmon_HPP
|
#endif // mastobotmon_HPP
|
||||||
|
|
Loading…
Reference in New Issue