Easyfied a bit
This commit is contained in:
parent
9b665c5a59
commit
770116a6ab
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project (mastobotmon
|
project (mastobotmon
|
||||||
VERSION 0.3.7
|
VERSION 0.3.8
|
||||||
LANGUAGES CXX)
|
LANGUAGES CXX)
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
|
|
|
@ -28,7 +28,7 @@ using std::string;
|
||||||
using std::uint16_t;
|
using std::uint16_t;
|
||||||
|
|
||||||
Account::Account(const string &instance, const string &access_token)
|
Account::Account(const string &instance, const string &access_token)
|
||||||
: API(instance, access_token)
|
: Easy(instance, access_token)
|
||||||
, _minutes(0)
|
, _minutes(0)
|
||||||
, _last_mention_id(0)
|
, _last_mention_id(0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -27,6 +27,8 @@
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
|
#include <mastodon-cpp/easy/entities/account.hpp>
|
||||||
|
#include <mastodon-cpp/easy/entities/notification.hpp>
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "mastobotmon.hpp"
|
#include "mastobotmon.hpp"
|
||||||
|
|
||||||
|
@ -35,10 +37,24 @@ using std::cerr;
|
||||||
using std::cin;
|
using std::cin;
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::uint16_t;
|
using std::uint16_t;
|
||||||
|
using Mastodon::Easy;
|
||||||
|
using std::chrono::system_clock;
|
||||||
|
|
||||||
Json::Value config; // Declared in mastobotmon.hpp
|
Json::Value config; // Declared in mastobotmon.hpp
|
||||||
|
|
||||||
const bool write_mentions(const string &straccount, Json::Value &mentions)
|
// Transform time_point into a string with the universal time
|
||||||
|
const string get_universal_time(const system_clock::time_point &timepoint)
|
||||||
|
{
|
||||||
|
std::time_t time = system_clock::to_time_t(timepoint);
|
||||||
|
std::tm *timeinfo = std::gmtime(&time);
|
||||||
|
char buffer[9];
|
||||||
|
|
||||||
|
std::strftime(buffer, 9, "%T", timeinfo);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool write_mentions(const string &straccount, std::vector<std::shared_ptr<Easy::Notification>> &mentions)
|
||||||
{
|
{
|
||||||
const string filepath = config["data_dir"].asString() + "/mentions_" + straccount + ".csv";
|
const string filepath = config["data_dir"].asString() + "/mentions_" + straccount + ".csv";
|
||||||
const std::regex restrip("<[^>]*>");
|
const std::regex restrip("<[^>]*>");
|
||||||
|
@ -47,12 +63,12 @@ const bool write_mentions(const string &straccount, Json::Value &mentions)
|
||||||
if (outfile.is_open())
|
if (outfile.is_open())
|
||||||
{
|
{
|
||||||
string output;
|
string output;
|
||||||
for (auto &mention : mentions)
|
for (std::shared_ptr<Easy::Notification> &mention : mentions)
|
||||||
{
|
{
|
||||||
output = mention["status"]["account"]["acct"].asString() + ';';
|
output = mention->status().account().acct() + ';';
|
||||||
output += mention["status"]["created_at"].asString() + ';';
|
output += get_universal_time(mention->status().created_at()) + ';';
|
||||||
output += mention["status"]["content"].asString() + ';';
|
output += mention->status().content() + ';';
|
||||||
output += mention["status"]["url"].asString() + '\n';
|
output += mention->status().url() + '\n';
|
||||||
output = std::regex_replace(output, restrip, "");
|
output = std::regex_replace(output, restrip, "");
|
||||||
outfile.write(output.c_str(), output.length());
|
outfile.write(output.c_str(), output.length());
|
||||||
}
|
}
|
||||||
|
@ -66,7 +82,7 @@ const bool write_mentions(const string &straccount, Json::Value &mentions)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool write_statistics(const string &straccount, Json::Value &account_json)
|
const bool write_statistics(const string &straccount, Easy::Account &account_entity)
|
||||||
{
|
{
|
||||||
const string filepath = config["data_dir"].asString() + "/statistics_" + straccount + ".csv";
|
const string filepath = config["data_dir"].asString() + "/statistics_" + straccount + ".csv";
|
||||||
|
|
||||||
|
@ -81,8 +97,8 @@ const bool write_statistics(const string &straccount, Json::Value &account_json)
|
||||||
|
|
||||||
ss << std::put_time(&now_tm, "%Y-%m-%dT%T");
|
ss << std::put_time(&now_tm, "%Y-%m-%dT%T");
|
||||||
output = ss.str() + ';';
|
output = ss.str() + ';';
|
||||||
output += account_json["statuses_count"].asString() + ';';
|
output += account_entity.statuses_count() + ';';
|
||||||
output += account_json["followers_count"].asString() + '\n';
|
output += account_entity.followers_count() + '\n';
|
||||||
outfile.write(output.c_str(), output.length());
|
outfile.write(output.c_str(), output.length());
|
||||||
outfile.close();
|
outfile.close();
|
||||||
|
|
||||||
|
@ -143,12 +159,10 @@ int main(int argc, char *argv[])
|
||||||
cerr << "Counter will reset at " << acc->get_header("X-RateLimit-Reset") << '\n';
|
cerr << "Counter will reset at " << acc->get_header("X-RateLimit-Reset") << '\n';
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
Json::Value json;
|
Easy::Account account_entity(answer);
|
||||||
std::stringstream ss(answer);
|
const string id = std::to_string(account_entity.id());
|
||||||
ss >> json;
|
const string straccount = account_entity.acct() + "@" + acc->get_instance();
|
||||||
const string id = json["id"].asString();
|
write_statistics(straccount, account_entity);
|
||||||
const string straccount = json["acct"].asString() + "@" + acc->get_instance();
|
|
||||||
write_statistics(straccount, json);
|
|
||||||
|
|
||||||
Account::parametermap parameters(
|
Account::parametermap parameters(
|
||||||
{
|
{
|
||||||
|
@ -157,17 +171,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)
|
||||||
{
|
{
|
||||||
ss.str(answer);
|
account_entity.from_string(answer);
|
||||||
ss >> json;
|
const string acct = account_entity.acct();
|
||||||
const string acct = json[0]["account"]["acct"].asString();
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
const auto now = std::chrono::system_clock::now();
|
const auto now = std::chrono::system_clock::now();
|
||||||
const auto last = std::chrono::system_clock::from_time_t(time);
|
const auto last = account_entity.created_at();
|
||||||
auto elapsed = std::chrono::duration_cast<std::chrono::minutes>(now - last);
|
auto elapsed = std::chrono::duration_cast<std::chrono::minutes>(now - last);
|
||||||
|
|
||||||
if (elapsed.count() > acc->get_minutes())
|
if (elapsed.count() > acc->get_minutes())
|
||||||
|
@ -200,14 +207,17 @@ int main(int argc, char *argv[])
|
||||||
ret = acc->get_mentions(answer);
|
ret = acc->get_mentions(answer);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
{
|
{
|
||||||
ss.str(answer);
|
std::vector<std::shared_ptr<Easy::Notification>> notifications;
|
||||||
ss >> json;
|
for (const string &str : Easy::json_array_to_vector(answer))
|
||||||
if (!json.empty())
|
|
||||||
{
|
{
|
||||||
const std::uint64_t lastid = std::stoull(json[0]["id"].asString());
|
notifications.push_back(std::make_shared<Easy::Notification>(str));
|
||||||
|
}
|
||||||
|
if (!notifications.empty())
|
||||||
|
{
|
||||||
|
const std::uint64_t lastid = notifications[0]->id();
|
||||||
acc->set_last_mention_id(lastid);
|
acc->set_last_mention_id(lastid);
|
||||||
config["accounts"][straccount]["last_mention"] = lastid;
|
config["accounts"][straccount]["last_mention"] = lastid;
|
||||||
write_mentions(straccount, json);
|
write_mentions(straccount, notifications);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,15 +19,19 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include <mastodon-cpp/mastodon-cpp.hpp>
|
#include <mastodon-cpp/mastodon-cpp.hpp>
|
||||||
|
#include <mastodon-cpp/easy/easy.hpp>
|
||||||
|
#include <mastodon-cpp/easy/entities/notification.hpp>
|
||||||
|
|
||||||
using std::uint16_t;
|
using std::uint16_t;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
extern Json::Value config;
|
extern Json::Value config;
|
||||||
|
|
||||||
class Account : public Mastodon::API
|
class Account : public Mastodon::Easy
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit Account(const string &instance, const string &access_token);
|
explicit Account(const string &instance, const string &access_token);
|
||||||
|
@ -46,7 +50,9 @@ const string get_access_token(const string &account);
|
||||||
const bool add_account();
|
const bool add_account();
|
||||||
const bool write_config();
|
const bool write_config();
|
||||||
|
|
||||||
const bool write_mentions(const string &straccount, Json::Value &mentions);
|
const string get_universal_time(const system_clock::time_point &timepoint);
|
||||||
|
const bool write_mentions(const string &straccount,
|
||||||
|
std::vector<std::shared_ptr<Mastodon::Easy::Notification>> &mentions);
|
||||||
const bool write_statistics(const string &straccount, Json::Value &account_json);
|
const bool write_statistics(const string &straccount, Json::Value &account_json);
|
||||||
|
|
||||||
#endif // mastobotmon_HPP
|
#endif // mastobotmon_HPP
|
||||||
|
|
Loading…
Reference in New Issue