summary refs log tree commit diff stats
path: root/infinite.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2022-11-15 14:12:26 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2022-11-15 14:12:26 -0500
commitb668325558e75d97a6e99629c0a76e29235b8ac1 (patch)
tree7f411ac77666446b6a7e1b21126662d9304b26a9 /infinite.cpp
parentd7fbb904d0dc70d1eada1dbb5f57f2ee9e5b399f (diff)
downloadinfinite-b668325558e75d97a6e99629c0a76e29235b8ac1.tar.gz
infinite-b668325558e75d97a6e99629c0a76e29235b8ac1.tar.bz2
infinite-b668325558e75d97a6e99629c0a76e29235b8ac1.zip
Bot posts to Mastodon now instead of Twitter
Diffstat (limited to 'infinite.cpp')
-rw-r--r--infinite.cpp69
1 files changed, 41 insertions, 28 deletions
diff --git a/infinite.cpp b/infinite.cpp index 37c4d72..5fcea86 100644 --- a/infinite.cpp +++ b/infinite.cpp
@@ -7,7 +7,8 @@
7#include <fstream> 7#include <fstream>
8#include <dirent.h> 8#include <dirent.h>
9#include <yaml-cpp/yaml.h> 9#include <yaml-cpp/yaml.h>
10#include <twitter.h> 10#include <mastodonpp/mastodonpp.hpp>
11#include <json.hpp>
11#include <algorithm> 12#include <algorithm>
12#include <chrono> 13#include <chrono>
13#include <thread> 14#include <thread>
@@ -448,13 +449,10 @@ int main(int argc, char** argv)
448 std::string configfile(argv[1]); 449 std::string configfile(argv[1]);
449 YAML::Node config = YAML::LoadFile(configfile); 450 YAML::Node config = YAML::LoadFile(configfile);
450 451
451 twitter::auth auth; 452 mastodonpp::Instance instance{
452 auth.setConsumerKey(config["consumer_key"].as<std::string>()); 453 config["mastodon_instance"].as<std::string>(),
453 auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); 454 config["mastodon_token"].as<std::string>()};
454 auth.setAccessKey(config["access_key"].as<std::string>()); 455 mastodonpp::Connection connection{instance};
455 auth.setAccessSecret(config["access_secret"].as<std::string>());
456
457 twitter::client client(auth);
458 456
459 // Parse forms file 457 // Parse forms file
460 std::map<std::string, std::vector<std::string>> groups; 458 std::map<std::string, std::vector<std::string>> groups;
@@ -516,6 +514,7 @@ int main(int argc, char** argv)
516 } 514 }
517 515
518 std::string colorsfile(config["colors"].as<std::string>()); 516 std::string colorsfile(config["colors"].as<std::string>());
517 std::string tempfile(config["tempfile"].as<std::string>());
519 518
520 verbly::data database {config["verbly_datafile"].as<std::string>()}; 519 verbly::data database {config["verbly_datafile"].as<std::string>()};
521 520
@@ -775,36 +774,50 @@ int main(int argc, char** argv)
775 } 774 }
776 775
777 textimage.annotate(towrite, Magick::CenterGravity); 776 textimage.annotate(towrite, Magick::CenterGravity);
778 textimage.opacity(((double)MaxRGB) * 0.8); 777 textimage.opacity(((double)MaxRGB) * 0.2);
779 image.composite(textimage, 0, 0, Magick::OverCompositeOp); 778 image.composite(textimage, 0, 0, Magick::OverCompositeOp);
780 779
781 image.magick("jpg"); 780 image.magick("jpg");
781 image.write(tempfile);
782 782
783 Magick::Blob outputimg; 783 std::cout << "Generated image!" << std::endl << "Posting..." << std::endl;
784 image.write(&outputimg);
785 784
786 std::cout << "Generated image!" << std::endl << "Tweeting..." << std::endl; 785 std::string tweetText = action;
786 tweetText.resize(140);
787 787
788 std::string tweetText; 788 auto answer{connection.post(mastodonpp::API::v1::media,
789 size_t tweetLim = 140 - client.getConfiguration().getCharactersReservedPerMedia() - client.getUser().getScreenName().length() - 6; 789 {{"file", std::string("@file:") + tempfile}, {"description", tweetText}})};
790 if (action.length() > tweetLim) 790 if (!answer)
791 { 791 {
792 tweetText = "\"" + action.substr(0, tweetLim - 1) + "…\" --@" + client.getUser().getScreenName(); 792 if (answer.curl_error_code == 0)
793 {
794 std::cout << "HTTP status: " << answer.http_status << std::endl;
795 }
796 else
797 {
798 std::cout << "libcurl error " << std::to_string(answer.curl_error_code)
799 << ": " << answer.error_message << std::endl;
800 }
793 } else { 801 } else {
794 tweetText = "\"" + action + "\" --@" + client.getUser().getScreenName(); 802 nlohmann::json response_json = nlohmann::json::parse(answer.body);
795 } 803 answer = connection.post(mastodonpp::API::v1::statuses,
796 804 {{"status", tweetText}, {"media_ids",
797 try 805 std::vector<std::string_view>{response_json["id"].get<std::string>()}}});
798 {
799 long media_id = client.uploadMedia("image/jpeg", (const char*) outputimg.data(), outputimg.length());
800 client.updateStatus(tweetText, {media_id});
801 806
802 std::cout << "Done!" << std::endl << "Waiting..." << std::endl << std::endl; 807 if (!answer)
803 } catch (const twitter::twitter_error& e) 808 {
804 { 809 if (answer.curl_error_code == 0)
805 std::cout << "Twitter error: " << e.what() << std::endl; 810 {
811 std::cout << "HTTP status: " << answer.http_status << std::endl;
812 }
813 else
814 {
815 std::cout << "libcurl error " << std::to_string(answer.curl_error_code)
816 << ": " << answer.error_message << std::endl;
817 }
818 }
806 } 819 }
807 820
808 std::this_thread::sleep_for(std::chrono::hours(1)); 821 std::this_thread::sleep_for(std::chrono::hours(12));
809 } 822 }
810} 823}