refactoring

This commit is contained in:
tastytea 2018-03-07 18:33:13 +01:00
parent 59ff7f8821
commit c3d7a33be3
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
5 changed files with 42 additions and 36 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastobotmon
VERSION 0.2.0
VERSION 0.2.1
LANGUAGES CXX)
include(GNUInstallDirs)

View File

@ -54,6 +54,7 @@ If you use a debug build, you get more verbose error messages.
"accounts" : {
"account1@example.social" : {
"access_token" : "xxxx",
// Alert after this many minutes of inactivity
"minutes" : 720
},
"account2@example.social" : {
@ -61,8 +62,11 @@ If you use a debug build, you get more verbose error messages.
"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",
// Possible values: cron (daemon mode later)
"mode" : "cron"
}

View File

@ -16,7 +16,6 @@
#include <iostream>
#include <string>
#include <cstring> // strlen()
#include <cstdlib> // getenv()
#include <fstream>
#include <sstream>
@ -32,7 +31,7 @@ using std::uint16_t;
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::stringstream json;
@ -44,31 +43,31 @@ const bool read_config(Json::Value &document)
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";
return false;
}
if (!document["accounts"].isObject())
if (!config["accounts"].isObject())
{
cerr << "ERROR: \"accounts\" not found\n";
return false;
}
if (!document["mode"].isString())
if (!config["mode"].isString())
{
cerr << "ERROR: \"mode\" not found\n";
return false;
}
if (!document["daemon_check"].isUInt())
if (!config["daemon_check"].isUInt())
{
cerr << "ERROR: \"daemon_check\" not found\n";
return false;
}
if (!document["data_dir"].isString())
if (!config["data_dir"].isString())
{
cerr << "ERROR: \"data_dir\" not found\n";
return false;
@ -78,13 +77,13 @@ const bool read_config(Json::Value &document)
{
cout << "No config file found. Creating new one.\n";
add_account(document);
add_account();
document["mode"] = "cron";
document["daemon_check"] = 10;
document["data_dir"] = ".";
config["mode"] = "cron";
config["daemon_check"] = 60;
config["data_dir"] = ".";
return write_config(document);
return write_config();
}
return true;
@ -121,7 +120,7 @@ const string get_access_token(const string &account)
return "";
}
const bool add_account(Json::Value &document)
const bool add_account()
{
string account;
string minutes;
@ -140,17 +139,17 @@ const bool add_account(Json::Value &document)
std::getline(cin, minutes);
access_token = get_access_token(account);
document["accounts"][account]["minutes"] = std::stoi(minutes);
document["accounts"][account]["access_token"] = access_token;
config["accounts"][account]["minutes"] = std::stoi(minutes);
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;
const string output = writer.write(document);
const string output = writer.write(config);
std::ofstream outfile(filepath);
if (outfile.is_open())

View File

@ -35,14 +35,16 @@ using std::cin;
using std::string;
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("<[^>]*>");
std::ofstream outfile(filepath, std::ios::app);
if (outfile.is_open())
{
cout << filepath << '\n';
string output;
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.close();
cout << "New mentions in: " << filepath << '\n';
return true;
}
cout << "NOT OPEN" << '\n';
cout << filepath << '\n';
cerr << "Error writing file: " << filepath << '\n';
return false;
}
int main(int argc, char *argv[])
{
Json::Value document;
uint16_t mainret = 0;
if (!read_config(document))
if (!read_config())
{
return 1;
}
@ -75,13 +76,13 @@ int main(int argc, char *argv[])
{
if ((std::strncmp(argv[1], "add", 3)) == 0)
{
add_account(document);
add_account();
}
}
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
string instance = it.name();
@ -97,7 +98,7 @@ int main(int argc, char *argv[])
accounts.push_back(*acc);
}
if (document["mode"] == "cron")
if (config["mode"] == "cron")
{
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 string straccount = acct + "@" + acc.get_instance();
acc.set_last_mention_id(lastid);
document["accounts"][straccount]["last_mention"] = lastid;
write_mentions(document["data_dir"].asString() + "/mentions_" + straccount + ".csv", json);
config["accounts"][straccount]["last_mention"] = lastid;
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";
}

View File

@ -25,6 +25,8 @@
using std::uint16_t;
using std::string;
extern Json::Value config;
class Account : public Mastodon::API
{
public:
@ -39,11 +41,11 @@ private:
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 bool add_account(Json::Value &document);
const bool write_config(Json::Value &document);
const bool add_account();
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