Added account class

This commit is contained in:
tastytea 2018-02-28 06:23:42 +01:00
parent f3b4e8d7fc
commit 0f84c4f123
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
6 changed files with 133 additions and 5 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastobotmon
VERSION 0.0.1
VERSION 0.0.2
LANGUAGES CXX
)

View File

@ -7,7 +7,7 @@
* Tested OS: Linux
* C++ compiler (tested: gcc 6.4, clang 5.0)
* [cmake](https://cmake.org/) (tested: 3.9.6)
* [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (tested: 0.4.3)
* [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (tested: 0.4.4)
* [rapidjson](http://rapidjson.org/) (tested: 1.1.0)
## Get sourcecode

View File

@ -30,6 +30,7 @@ using std::string;
using std::cout;
using std::cin;
using std::cerr;
using std::uint16_t;
bool read_config(rapidjson::Document &document)
{
@ -53,6 +54,12 @@ bool read_config(rapidjson::Document &document)
return false;
}
if (!document["mode"].IsString())
{
cerr << "ERROR: \"mode\" not found\n";
return false;
}
if (!document["daemon_check"].IsUint())
{
cerr << "ERROR: \"daemon_check\" not found\n";
@ -87,10 +94,17 @@ bool read_config(rapidjson::Document &document)
cout << "Minutes af allowed inactivity: ";
std::getline(cin, minutes);
writer.Key(account.c_str());
writer.StartObject();
writer.Key("minutes");
writer.Uint(std::stoi(minutes));
writer.Key("access_token");
writer.String(get_access_token(account).c_str());
writer.EndObject();
}
writer.EndObject();
writer.Key("mode");
writer.String("cron");
writer.Key("daemon_check");
writer.Uint(10);
writer.EndObject();
@ -110,3 +124,32 @@ bool read_config(rapidjson::Document &document)
return true;
}
const string get_access_token(const string &account)
{
const string instance = account.substr(account.find('@') + 1);
Account acc(instance, "");
uint16_t ret;
string client_id;
string client_secret;
string url;
string code;
string access_token = "";
ret = acc.register_app1("mastobotmon", "urn:ietf:wg:oauth:2.0:oob", "read",
"https://github.com/tastytea/mastobotmon",
client_id, client_secret, url);
if (ret == 0)
{
cout << "Visit " << url << " and paste the generated code\nhere: ";
cin >> code;
ret = acc.register_app2(client_id, client_secret, "urn:ietf:wg:oauth:2.0:oob",
code, access_token);
if (ret == 0)
{
return access_token;
}
}
cerr << "Error: " << ret << '\n';
return "";
}

View File

@ -17,12 +17,11 @@
#include <iostream>
#include <string>
#include <cstdint>
#include <mastodon-cpp.hpp>
#include <vector>
#include <rapidjson/document.h>
#include "version.hpp"
#include "mastobotmon.hpp"
using Mastodon::API;
using std::cout;
using std::cerr;
using std::cin;
@ -36,9 +35,36 @@ int main(int argc, char *argv[])
return 1;
}
std::vector<Account> accounts;
for (const auto &member : document["accounts"].GetObject())
{
cout << member.name.GetString() << ": " << member.value.GetUint() << '\n';
// Construct an Account object for every account
string instance = member.name.GetString();
instance = instance.substr(instance.find('@') + 1);
Account acc(instance, member.value["access_token"].GetString());
acc.set_minutes(member.value["minutes"].GetUint());
accounts.push_back(acc);
}
cout << "DEBUG\n";
if (document["mode"] == "cron")
{
cout << "DEBUG\n";
for (Account &acc : accounts)
{
cout << "DEBUG\n";
std::string answer;
// std::string id;
// Account::parametermap parameters(
// {
// { "limit", { "1" } }
// });
// cout << acc.get(Mastodon::API::v1::statuses, id, parameters, answer);
cout << acc.get(Mastodon::API::v1::accounts_verify_credentials, answer);
cout << answer << '\n';
}
}
return 0;

View File

@ -17,8 +17,26 @@
#ifndef mastobotmon_HPP
#define mastobotmon_HPP
#include <string>
#include <cstdint>
#include <rapidjson/document.h>
#include <mastodon-cpp.hpp>
using std::uint16_t;
using std::string;
bool read_config(rapidjson::Document &document);
const string get_access_token(const string &account);
class Account : public Mastodon::API
{
public:
explicit Account(const string &instance, const string &access_token);
void set_minutes(uint16_t minutes);
private:
uint16_t _minutes;
string _access_token;
};
#endif // mastobotmon_HPP

41
src/mastodon.cpp Normal file
View File

@ -0,0 +1,41 @@
/* This file is part of mastobotmon.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include <cstdint>
#include <mastodon-cpp.hpp>
#include "version.hpp"
#include "mastobotmon.hpp"
using std::cout;
using std::cerr;
using std::cin;
using std::string;
using std::uint16_t;
Account::Account(const string &instance, const string &access_token)
: API(instance, access_token)
, _minutes(0)
, _access_token("")
{
//
}
void Account::set_minutes(uint16_t minutes)
{
_minutes = minutes;
}