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 - tag
steps: steps:
- name: download mastodon-cpp for buster - name: download mastodonpp for buster
image: plugins/download image: plugins/download
settings: settings:
source: https://schlomp.space/tastytea/mastodon-cpp/releases/download/0.111.5/libmastodon-cpp_0.111.5-0_buster_amd64.deb source: https://schlomp.space/tastytea/mastodonpp/releases/download/0.4.0/libmastodonpp_0.4.0-0_amd64_buster.deb
destination: mastodon-cpp_buster.deb destination: mastodonpp_buster.deb
# - name: download mastodon-cpp for bionic # - name: download mastodonpp for bionic
# image: plugins/download # image: plugins/download
# settings: # settings:
# source: https://schlomp.space/tastytea/mastodon-cpp/releases/download/0.111.5/libmastodon-cpp_0.111.5-0_bionic_amd64.deb # source: https://schlomp.space/tastytea/mastodonpp/releases/download/0.4.0/libmastodonpp_0.4.0-0_amd64_bionic.deb
# destination: mastodon-cpp_bionic.deb # destination: mastodonpp_bionic.deb
# Workaround until the debian packages are fixed. # Workaround until the debian packages are fixed.
- name: download restclient-cpp source - name: download restclient-cpp source
@ -50,7 +50,7 @@ steps:
- apt-get update -q - apt-get update -q
- apt-get install -qq build-essential cmake asciidoc - 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 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 - tar -xf restclient-cpp.tar.gz
- cd restclient-cpp-* - cd restclient-cpp-*
- cmake -DCMAKE_INSTALL_PREFIX=/usr . - cmake -DCMAKE_INSTALL_PREFIX=/usr .
@ -83,7 +83,7 @@ steps:
# - apt-get update -q # - apt-get update -q
# - apt-get install -qq build-essential cmake asciidoc # - 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 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 # - rm -rf build && mkdir -p build && cd build
# - cmake .. # - cmake ..
# - make VERBOSE=1 # - 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(CURL 7.52 REQUIRED)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(restclient-cpp 0.5 CONFIG) 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}) if(NOT ${restclient-cpp_FOUND})
find_file(restclient_h NAMES "restclient-cpp/restclient.h" find_file(restclient_h NAMES "restclient-cpp/restclient.h"
@ -31,7 +31,7 @@ file(GLOB sources *.cpp)
add_executable(mastorss ${sources}) add_executable(mastorss ${sources})
target_link_libraries(mastorss target_link_libraries(mastorss
PRIVATE PRIVATE
jsoncpp_lib restclient-cpp mastodon-cpp::mastodon-cpp jsoncpp_lib restclient-cpp mastodonpp::mastodonpp
Boost::filesystem Boost::log Boost::regex) Boost::filesystem Boost::log Boost::regex)
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS)
target_compile_definitions(mastorss PRIVATE "BOOST_ALL_DYN_LINK=1") target_compile_definitions(mastorss PRIVATE "BOOST_ALL_DYN_LINK=1")

View File

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

View File

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

View File

@ -29,7 +29,7 @@ using std::string_view;
MastoAPI::MastoAPI(ProfileData &data) MastoAPI::MastoAPI(ProfileData &data)
: _profile{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 length: " << status.size();
BOOST_LOG_TRIVIAL(debug) << "Status: \"" << status << '"'; BOOST_LOG_TRIVIAL(debug) << "Status: \"" << status << '"';
Mastodon::parameters params{{"status", {status}}}; mastodonpp::parametermap params{{"status", status}};
if (_profile.titles_as_cw) 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)
{ {
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; BOOST_LOG_TRIVIAL(debug) << "Posted status with GUID: " << item.guid;

View File

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