From 24719bec1349ebfbdb4900e27d3f7922d8ae717f Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 15 Mar 2018 14:35:58 +0100 Subject: [PATCH] Added statistics --- CMakeLists.txt | 2 +- README.md | 6 +++++- src/{mastodon.cpp => account.cpp} | 0 src/mastobotmon.cpp | 30 +++++++++++++++++++++++++++++- src/mastobotmon.hpp | 1 + 5 files changed, 36 insertions(+), 3 deletions(-) rename src/{mastodon.cpp => account.cpp} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fe123c..0acb39b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastobotmon - VERSION 0.2.2 + VERSION 0.3.0 LANGUAGES CXX) include(GNUInstallDirs) diff --git a/README.md b/README.md index 62bef83..341ecde 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,10 @@ If you use a debug build, you get more verbose error messages. Mentions are written to `data_dir/mentions_account.csv`. The format is: acct;created_at;content. +## Statistics + +Statistics are written to `data_dir/statistics_account.csv`. The format is: time;toots;followers. + # TODO * Version 0.1.0 @@ -86,7 +90,7 @@ Mentions are written to `data_dir/mentions_account.csv`. The format is: acct;cre * [x] Write mentions to file * Version 0.3.0 * [x] Respect X-RateLimit header - * [ ] Write statistics to file + * [x] Write statistics to file * Version 0.4.0 * [ ] Daemon mode * Version 0.5.0 diff --git a/src/mastodon.cpp b/src/account.cpp similarity index 100% rename from src/mastodon.cpp rename to src/account.cpp diff --git a/src/mastobotmon.cpp b/src/mastobotmon.cpp index d49ec05..da09143 100644 --- a/src/mastobotmon.cpp +++ b/src/mastobotmon.cpp @@ -64,6 +64,33 @@ const bool write_mentions(const string &straccount, Json::Value &mentions) return false; } +const bool write_statistics(const string &straccount, Json::Value &account_json) +{ + const string filepath = config["data_dir"].asString() + "/statistics_" + straccount + ".csv"; + + std::ofstream outfile(filepath, std::ios::app); + if (outfile.is_open()) + { + string output; + std::chrono::time_point now = std::chrono::system_clock::now(); + std::time_t now_t = std::chrono::system_clock::to_time_t(now); + std::tm now_tm = *std::localtime(&now_t); + std::stringstream ss; + + ss << std::put_time(&now_tm, "%Y-%m-%dT%T"); + output = ss.str() + ';'; + output += account_json["statuses_count"].asString() + ';'; + output += account_json["followers_count"].asString() + ";\n"; + outfile.write(output.c_str(), output.length()); + outfile.close(); + + return true; + } + + cerr << "Error writing file: " << filepath << '\n'; + return false; +} + int main(int argc, char *argv[]) { uint16_t mainret = 0; @@ -116,6 +143,8 @@ int main(int argc, char *argv[]) Json::Reader reader; reader.parse(answer, json); const string id = json["id"].asString(); + const string straccount = json["account"]["acct"].asString() + "@" + acc.get_instance(); + write_statistics(straccount, json); Account::parametermap parameters( { @@ -170,7 +199,6 @@ int main(int argc, char *argv[]) if (!json.empty()) { const std::uint64_t lastid = std::stoull(json[0]["id"].asString()); - const string straccount = acct + "@" + acc.get_instance(); acc.set_last_mention_id(lastid); config["accounts"][straccount]["last_mention"] = lastid; write_mentions(straccount, json); diff --git a/src/mastobotmon.hpp b/src/mastobotmon.hpp index 454a39f..a639019 100644 --- a/src/mastobotmon.hpp +++ b/src/mastobotmon.hpp @@ -47,5 +47,6 @@ const bool add_account(); const bool write_config(); const bool write_mentions(const string &straccount, Json::Value &mentions); +const bool write_statistics(const string &straccount, Json::Value &account_json); #endif // mastobotmon_HPP