Alerts in cron mode atre working

This commit is contained in:
tastytea 2018-03-02 06:11:18 +01:00
parent dbbc9cea96
commit c3858936d0
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
5 changed files with 32 additions and 7 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastobotmon
VERSION 0.0.3
VERSION 0.1.0
LANGUAGES CXX
)

View File

@ -47,10 +47,11 @@ If you use a debug build, you get more verbose error messages.
# TODO
* Version 0.1.0
* [ ] Cron mode
* [x] Cron mode
* [x] Config file
* [ ] Alert if account seems inactive
* [x] Alert if account seems inactive
* Version 0.2.0
* [ ] Allow to add accounts later
* [ ] Write mentions to file
* Version 0.3.0
* [ ] Write statistics to file

View File

@ -18,6 +18,10 @@
#include <string>
#include <cstdint>
#include <vector>
#include <chrono>
#include <ctime>
#include <sstream>
#include <iomanip> // get_time
#include <rapidjson/document.h>
#include "version.hpp"
#include "mastobotmon.hpp"
@ -26,6 +30,7 @@ using std::cout;
using std::cerr;
using std::cin;
using std::string;
using std::uint16_t;
int main(int argc, char *argv[])
{
@ -69,8 +74,21 @@ int main(int argc, char *argv[])
if (ret == 0)
{
json.Parse(answer.c_str());
cout << "The last toot of " << json[0]["account"]["acct"].GetString()
<< " was at " << json[0]["created_at"].GetString() << ".\n";
const string acct = json[0]["account"]["acct"].GetString();
std::istringstream isslast(json[0]["created_at"].GetString());
struct std::tm tm = {0};
isslast >> std::get_time(&tm, "%Y-%m-%dT%T");
std::time_t time = mktime(&tm);
const auto now = std::chrono::system_clock::now();
const auto last = std::chrono::system_clock::from_time_t(time);
auto elapsed = std::chrono::duration_cast<std::chrono::minutes>(now - last);
if (elapsed.count() > acc.get_minutes())
{
cout << "ALERT: " << acct << " is inactive since " << elapsed.count() << " minutes.\n";
}
}
}

View File

@ -32,7 +32,8 @@ class Account : public Mastodon::API
{
public:
explicit Account(const string &instance, const string &access_token);
void set_minutes(uint16_t minutes);
const void set_minutes(uint16_t minutes);
const uint16_t get_minutes() const;
private:
uint16_t _minutes;

View File

@ -34,7 +34,12 @@ Account::Account(const string &instance, const string &access_token)
//
}
void Account::set_minutes(uint16_t minutes)
const void Account::set_minutes(uint16_t minutes)
{
_minutes = minutes;
}
const uint16_t Account::get_minutes() const
{
return _minutes;
}