Switch from mastodon-cpp to mastodonpp.

This commit is contained in:
tastytea 2020-01-15 19:05:49 +01:00
parent b35e298eea
commit 0787fefba9
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
6 changed files with 31 additions and 33 deletions

View File

@ -13,17 +13,17 @@ trigger:
- tag
steps:
- name: download mastodon-cpp for buster
- name: download mastodonpp for buster
image: plugins/download
settings:
source: https://schlomp.space/tastytea/mastodon-cpp/releases/download/0.111.5/libmastodon-cpp_0.111.5-0_buster_amd64.deb
destination: mastodon-cpp_buster.deb
source: https://schlomp.space/tastytea/mastodonpp/releases/download/0.4.0/libmastodonpp_0.4.0-0_amd64_buster.deb
destination: mastodonpp_buster.deb
# - name: download mastodon-cpp for bionic
# - name: download mastodonpp for bionic
# image: plugins/download
# settings:
# source: https://schlomp.space/tastytea/mastodon-cpp/releases/download/0.111.5/libmastodon-cpp_0.111.5-0_bionic_amd64.deb
# destination: mastodon-cpp_bionic.deb
# source: https://schlomp.space/tastytea/mastodonpp/releases/download/0.4.0/libmastodonpp_0.4.0-0_amd64_bionic.deb
# destination: mastodonpp_bionic.deb
# Workaround until the debian packages are fixed.
- name: download restclient-cpp source
@ -50,7 +50,7 @@ steps:
- apt-get update -q
- apt-get install -qq build-essential cmake asciidoc
- apt-get install -qq libboost-filesystem-dev libboost-log-dev libboost-regex-dev libjsoncpp-dev catch libcurl4-openssl-dev restclient-cpp libpoco-dev
- apt-get -qq install ./mastodon-cpp_buster.deb
- apt-get -qq install ./mastodonpp_buster.deb
- tar -xf restclient-cpp.tar.gz
- cd restclient-cpp-*
- cmake -DCMAKE_INSTALL_PREFIX=/usr .
@ -83,7 +83,7 @@ steps:
# - apt-get update -q
# - apt-get install -qq build-essential cmake asciidoc
# - apt-get install -qq libboost-filesystem-dev libboost-log-dev libboost-regex-dev libjsoncpp-dev catch libcurl4-openssl-dev restclient-cpp libpoco-dev
# - apt-get -qq install ./mastodon-cpp_bionic.deb
# - apt-get -qq install ./mastodonpp_bionic.deb
# - rm -rf build && mkdir -p build && cd build
# - cmake ..
# - make VERBOSE=1

View File

@ -6,7 +6,7 @@ find_package(jsoncpp REQUIRED CONFIG) # 1.7.4 (Debian buster) has no version.
find_package(CURL 7.52 REQUIRED)
find_package(Threads REQUIRED)
find_package(restclient-cpp 0.5 CONFIG)
find_package(mastodon-cpp REQUIRED CONFIG)
find_package(mastodonpp 0.4 REQUIRED CONFIG)
if(NOT ${restclient-cpp_FOUND})
find_file(restclient_h NAMES "restclient-cpp/restclient.h"
@ -31,7 +31,7 @@ file(GLOB sources *.cpp)
add_executable(mastorss ${sources})
target_link_libraries(mastorss
PRIVATE
jsoncpp_lib restclient-cpp mastodon-cpp::mastodon-cpp
jsoncpp_lib restclient-cpp mastodonpp::mastodonpp
Boost::filesystem Boost::log Boost::regex)
if(BUILD_SHARED_LIBS)
target_compile_definitions(mastorss PRIVATE "BOOST_ALL_DYN_LINK=1")

View File

@ -18,7 +18,7 @@
#include "exceptions.hpp"
#include <boost/log/trivial.hpp>
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodonpp/mastodonpp.hpp>
#include <algorithm>
#include <cstdlib>
@ -192,32 +192,29 @@ string Config::get_access_token(const string &instance) const
string client_id;
string client_secret;
string url;
Mastodon::API masto(instance, "");
mastodonpp::Instance masto{instance, ""};
mastodonpp::Instance::ObtainToken token{masto};
auto ret = masto.register_app1("mastorss", "urn:ietf:wg:oauth:2.0:oob",
"write",
"https://schlomp.space/tastytea/mastorss",
client_id, client_secret, url);
auto ret{token.step_1("mastorss", "write:statuses",
"https://schlomp.space/tastytea/mastorss")};
if (ret)
{
string code;
string access_token;
cout << "Visit " << url << " to authorize this application.\n";
cout << "Visit " << ret << " to authorize this application.\n";
cout << "Insert code: ";
std::getline(cin, code);
ret = masto.register_app2(client_id, client_secret,
"urn:ietf:wg:oauth:2.0:oob",
code, access_token);
ret = token.step_2(code);
if (ret)
{
BOOST_LOG_TRIVIAL(debug) << "Got access token: " << access_token;
BOOST_LOG_TRIVIAL(debug) << "Got access token.";
return access_token;
}
}
throw MastodonException{ret.error_code};
throw MastodonException{ret.curl_error_code};
}
void Config::parse()

View File

@ -22,7 +22,7 @@
#include <boost/property_tree/xml_parser.hpp>
#include <boost/regex.hpp>
#include <json/json.h>
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodonpp/mastodonpp.hpp>
#include <restclient-cpp/connection.h>
#include <algorithm>
@ -214,7 +214,7 @@ void Document::parse_rss(const pt::ptree &tree)
string Document::remove_html(string html) const
{
html = Mastodon::unescape_html(html); // Decode HTML entities.
html = mastodonpp::unescape_html(html); // Decode HTML entities.
html = regex_replace(html, regex{"<p>"}, "\n\n");

View File

@ -29,7 +29,7 @@ using std::string_view;
MastoAPI::MastoAPI(ProfileData &data)
: _profile{data}
, _masto{_profile.instance, _profile.access_token}
, _instance{_profile.instance, _profile.access_token}
{
}
@ -100,20 +100,21 @@ void MastoAPI::post_item(const Item &item)
BOOST_LOG_TRIVIAL(debug) << "Status length: " << status.size();
BOOST_LOG_TRIVIAL(debug) << "Status: \"" << status << '"';
Mastodon::parameters params{{"status", {status}}};
mastodonpp::parametermap params{{"status", status}};
if (_profile.titles_as_cw)
{
params.push_back({"spoiler_text", {item.title}});
params.insert({"spoiler_text", item.title});
}
const auto ret = _masto.post(Mastodon::API::v1::statuses, params);
mastodonpp::Connection connection{_instance};
const auto ret = connection.post(mastodonpp::API::v1::statuses, params);
if (!ret)
{
if (ret.http_error_code != 200)
if (ret.http_status != 200)
{
throw HTTPException{ret.http_error_code};
throw HTTPException{ret.http_status};
}
throw MastodonException{ret.error_code};
throw MastodonException{ret.curl_error_code};
}
BOOST_LOG_TRIVIAL(debug) << "Posted status with GUID: " << item.guid;

View File

@ -20,7 +20,7 @@
#include "config.hpp"
#include "document.hpp"
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodonpp/mastodonpp.hpp>
namespace mastorss
{
@ -33,7 +33,7 @@ public:
private:
ProfileData &_profile;
Mastodon::API _masto;
mastodonpp::Instance _instance;
constexpr static size_t _max_guids{100};
};
} // namespace mastorss