diff options
Diffstat (limited to 'sap.cpp')
| -rw-r--r-- | sap.cpp | 64 |
1 files changed, 43 insertions, 21 deletions
| diff --git a/sap.cpp b/sap.cpp index f0c3fd1..bae15e1 100644 --- a/sap.cpp +++ b/sap.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | #include <chrono> | 4 | #include <chrono> |
| 5 | #include <fstream> | 5 | #include <fstream> |
| 6 | #include <iostream> | 6 | #include <iostream> |
| 7 | #include <json.hpp> | ||
| 7 | 8 | ||
| 8 | /* - random frames from Spongebob (using ffmpeg) | 9 | /* - random frames from Spongebob (using ffmpeg) |
| 9 | * with strange text overlaid, possibly rawr'd from | 10 | * with strange text overlaid, possibly rawr'd from |
| @@ -19,15 +20,13 @@ sap::sap( | |||
| 19 | { | 20 | { |
| 20 | // Load the config file. | 21 | // Load the config file. |
| 21 | YAML::Node config = YAML::LoadFile(configFile); | 22 | YAML::Node config = YAML::LoadFile(configFile); |
| 23 | tempfile_ = config["tempfile"].as<std::string>(); | ||
| 22 | 24 | ||
| 23 | // Set up the Twitter client. | 25 | // Set up the Mastodon client. |
| 24 | twitter::auth auth; | 26 | instance_ = std::make_unique<mastodonpp::Instance>( |
| 25 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | 27 | config["mastodon_instance"].as<std::string>(), |
| 26 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); | 28 | config["mastodon_token"].as<std::string>()); |
| 27 | auth.setAccessKey(config["access_key"].as<std::string>()); | 29 | connection_ = std::make_unique<mastodonpp::Connection>(*instance_); |
| 28 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | ||
| 29 | |||
| 30 | client_ = std::unique_ptr<twitter::client>(new twitter::client(auth)); | ||
| 31 | 30 | ||
| 32 | // Set up the text generator. | 31 | // Set up the text generator. |
| 33 | for (const YAML::Node& corpusname : config["corpuses"]) | 32 | for (const YAML::Node& corpusname : config["corpuses"]) |
| @@ -64,7 +63,7 @@ void sap::run() const | |||
| 64 | { | 63 | { |
| 65 | for (;;) | 64 | for (;;) |
| 66 | { | 65 | { |
| 67 | std::cout << "Generating tweet..." << std::endl; | 66 | std::cout << "Generating post..." << std::endl; |
| 68 | 67 | ||
| 69 | try | 68 | try |
| 70 | { | 69 | { |
| @@ -83,7 +82,7 @@ void sap::run() const | |||
| 83 | // Send the tweet. | 82 | // Send the tweet. |
| 84 | std::cout << "Sending tweet..." << std::endl; | 83 | std::cout << "Sending tweet..." << std::endl; |
| 85 | 84 | ||
| 86 | sendTweet(std::move(image)); | 85 | sendTweet(std::move(image), action); |
| 87 | 86 | ||
| 88 | std::cout << "Tweeted!" << std::endl; | 87 | std::cout << "Tweeted!" << std::endl; |
| 89 | 88 | ||
| @@ -92,25 +91,48 @@ void sap::run() const | |||
| 92 | } catch (const Magick::ErrorImage& ex) | 91 | } catch (const Magick::ErrorImage& ex) |
| 93 | { | 92 | { |
| 94 | std::cout << "Image error: " << ex.what() << std::endl; | 93 | std::cout << "Image error: " << ex.what() << std::endl; |
| 95 | } catch (const twitter::twitter_error& ex) | ||
| 96 | { | ||
| 97 | std::cout << "Twitter error: " << ex.what() << std::endl; | ||
| 98 | |||
| 99 | std::this_thread::sleep_for(std::chrono::hours(1)); | ||
| 100 | } | 94 | } |
| 101 | 95 | ||
| 102 | std::cout << std::endl; | 96 | std::cout << std::endl; |
| 103 | } | 97 | } |
| 104 | } | 98 | } |
| 105 | 99 | ||
| 106 | void sap::sendTweet(Magick::Image image) const | 100 | void sap::sendTweet(Magick::Image image, const std::string& text) const |
| 107 | { | 101 | { |
| 108 | Magick::Blob outputimg; | ||
| 109 | image.magick("jpeg"); | 102 | image.magick("jpeg"); |
| 110 | image.write(&outputimg); | 103 | image.write(tempfile_); |
| 111 | 104 | ||
| 112 | long media_id = client_->uploadMedia("image/jpeg", | 105 | auto answer{connection_->post(mastodonpp::API::v1::media, |
| 113 | static_cast<const char*>(outputimg.data()), outputimg.length()); | 106 | {{"file", std::string("@file:") + tempfile_}, {"description", text}})}; |
| 107 | if (!answer) | ||
| 108 | { | ||
| 109 | if (answer.curl_error_code == 0) | ||
| 110 | { | ||
| 111 | std::cout << "HTTP status: " << answer.http_status << std::endl; | ||
| 112 | } | ||
| 113 | else | ||
| 114 | { | ||
| 115 | std::cout << "libcurl error " << std::to_string(answer.curl_error_code) | ||
| 116 | << ": " << answer.error_message << std::endl; | ||
| 117 | } | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | nlohmann::json response_json = nlohmann::json::parse(answer.body); | ||
| 122 | answer = connection_->post(mastodonpp::API::v1::statuses, | ||
| 123 | {{"status", ""}, {"media_ids", | ||
| 124 | std::vector<std::string_view>{response_json["id"].get<std::string>()}}}); | ||
| 114 | 125 | ||
| 115 | client_->updateStatus("", {media_id}); | 126 | if (!answer) |
| 127 | { | ||
| 128 | if (answer.curl_error_code == 0) | ||
| 129 | { | ||
| 130 | std::cout << "HTTP status: " << answer.http_status << std::endl; | ||
| 131 | } | ||
| 132 | else | ||
| 133 | { | ||
| 134 | std::cout << "libcurl error " << std::to_string(answer.curl_error_code) | ||
| 135 | << ": " << answer.error_message << std::endl; | ||
| 136 | } | ||
| 137 | } | ||
| 116 | } | 138 | } |
