From b668325558e75d97a6e99629c0a76e29235b8ac1 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 15 Nov 2022 14:12:26 -0500 Subject: Bot posts to Mastodon now instead of Twitter --- infinite.cpp | 69 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 28 deletions(-) (limited to 'infinite.cpp') diff --git a/infinite.cpp b/infinite.cpp index 37c4d72..5fcea86 100644 --- a/infinite.cpp +++ b/infinite.cpp @@ -7,7 +7,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -448,13 +449,10 @@ int main(int argc, char** argv) std::string configfile(argv[1]); YAML::Node config = YAML::LoadFile(configfile); - twitter::auth auth; - auth.setConsumerKey(config["consumer_key"].as()); - auth.setConsumerSecret(config["consumer_secret"].as()); - auth.setAccessKey(config["access_key"].as()); - auth.setAccessSecret(config["access_secret"].as()); - - twitter::client client(auth); + mastodonpp::Instance instance{ + config["mastodon_instance"].as(), + config["mastodon_token"].as()}; + mastodonpp::Connection connection{instance}; // Parse forms file std::map> groups; @@ -516,6 +514,7 @@ int main(int argc, char** argv) } std::string colorsfile(config["colors"].as()); + std::string tempfile(config["tempfile"].as()); verbly::data database {config["verbly_datafile"].as()}; @@ -775,36 +774,50 @@ int main(int argc, char** argv) } textimage.annotate(towrite, Magick::CenterGravity); - textimage.opacity(((double)MaxRGB) * 0.8); + textimage.opacity(((double)MaxRGB) * 0.2); image.composite(textimage, 0, 0, Magick::OverCompositeOp); image.magick("jpg"); + image.write(tempfile); - Magick::Blob outputimg; - image.write(&outputimg); + std::cout << "Generated image!" << std::endl << "Posting..." << std::endl; - std::cout << "Generated image!" << std::endl << "Tweeting..." << std::endl; + std::string tweetText = action; + tweetText.resize(140); - std::string tweetText; - size_t tweetLim = 140 - client.getConfiguration().getCharactersReservedPerMedia() - client.getUser().getScreenName().length() - 6; - if (action.length() > tweetLim) + auto answer{connection.post(mastodonpp::API::v1::media, + {{"file", std::string("@file:") + tempfile}, {"description", tweetText}})}; + if (!answer) { - tweetText = "\"" + action.substr(0, tweetLim - 1) + "…\" --@" + client.getUser().getScreenName(); + if (answer.curl_error_code == 0) + { + std::cout << "HTTP status: " << answer.http_status << std::endl; + } + else + { + std::cout << "libcurl error " << std::to_string(answer.curl_error_code) + << ": " << answer.error_message << std::endl; + } } else { - tweetText = "\"" + action + "\" --@" + client.getUser().getScreenName(); - } - - try - { - long media_id = client.uploadMedia("image/jpeg", (const char*) outputimg.data(), outputimg.length()); - client.updateStatus(tweetText, {media_id}); + nlohmann::json response_json = nlohmann::json::parse(answer.body); + answer = connection.post(mastodonpp::API::v1::statuses, + {{"status", tweetText}, {"media_ids", + std::vector{response_json["id"].get()}}}); - std::cout << "Done!" << std::endl << "Waiting..." << std::endl << std::endl; - } catch (const twitter::twitter_error& e) - { - std::cout << "Twitter error: " << e.what() << std::endl; + if (!answer) + { + if (answer.curl_error_code == 0) + { + std::cout << "HTTP status: " << answer.http_status << std::endl; + } + else + { + std::cout << "libcurl error " << std::to_string(answer.curl_error_code) + << ": " << answer.error_message << std::endl; + } + } } - std::this_thread::sleep_for(std::chrono::hours(1)); + std::this_thread::sleep_for(std::chrono::hours(12)); } } -- cgit 1.4.1