Add MastoAPI.

This commit is contained in:
tastytea 2019-12-28 07:13:30 +01:00
parent ec8bc6508c
commit 477bf265d4
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 129 additions and 0 deletions

89
src/mastoapi.cpp Normal file
View File

@ -0,0 +1,89 @@
/* This file is part of mastorss.
* Copyright © 2019 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 "exceptions.hpp"
#include "mastoapi.hpp"
#include <boost/log/trivial.hpp>
#include <string>
namespace mastorss
{
using std::string;
MastoAPI::MastoAPI(const ProfileData &data)
: _profile{data}
, _masto{_profile.instance, _profile.access_token}
{
}
void MastoAPI::post_item(const Item &item)
{
string status{[&]
{
if (_profile.titles_as_cw)
{
if (_profile.titles_only)
{
return string{};
}
return item.description;
}
string s{item.title};
if (!_profile.titles_only)
{
s.append("\n\n" + item.description);
}
return s;
}()};
status.append("\n\n" + item.link);
if (!_profile.append.empty())
{
const size_t len{status.size()};
const size_t len_append{_profile.append.size() + 2};
const size_t len_max{_profile.max_size};
if ((len + len_append) > len_max)
{
status.resize(len_max - len_append);
}
status.append("\n\n" + _profile.append);
}
Mastodon::parameters params{{"status", {status}}};
if (_profile.titles_as_cw)
{
params.push_back({"spoiler_text", {item.title}});
}
const auto ret = _masto.post(Mastodon::API::v1::statuses, params);
if (!ret)
{
if (ret.http_error_code != 200)
{
throw HTTPException{ret.http_error_code};
}
else
{
throw MastodonException{ret.error_code};
}
}
BOOST_LOG_TRIVIAL(debug) << "Posted status with GUID: " << item.guid;
}
} // namespace mastorss

40
src/mastoapi.hpp Normal file
View File

@ -0,0 +1,40 @@
/* This file is part of mastorss.
* Copyright © 2019 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/>.
*/
#ifndef MASTORSS_MASTOAPI_HPP
#define MASTORSS_MASTOAPI_HPP
#include "config.hpp"
#include "document.hpp"
#include <mastodon-cpp/mastodon-cpp.hpp>
namespace mastorss
{
class MastoAPI
{
public:
explicit MastoAPI(const ProfileData &data);
void post_item(const Item &item);
private:
const ProfileData &_profile;
Mastodon::API _masto;
};
} // namespace mastorss
#endif // MASTORSS_MASTOAPI_HPP