From fb26c5be267339b9024d6669df09b5c57d15b9ad Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 13 Nov 2022 22:29:05 -0500 Subject: Bot now uses Mastodon instead of Twitter --- .gitignore | 5 +++ .gitmodules | 3 ++ CMakeLists.txt | 10 ++--- vendor/mastodonpp | 1 + yandere.cpp | 115 ++++++++++++++++++++++++++++++------------------------ 5 files changed, 78 insertions(+), 56 deletions(-) create mode 100644 .gitignore create mode 160000 vendor/mastodonpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf41023 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +chemist +cmake_install.cmake +CMakeCache.txt +CMakeFiles +Makefile diff --git a/.gitmodules b/.gitmodules index 78fd03a..6d4576f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "vendor/libtwittercpp"] path = vendor/libtwittercpp url = https://github.com/hatkirby/libtwittercpp +[submodule "vendor/mastodonpp"] + path = vendor/mastodonpp + url = https://schlomp.space/tastytea/mastodonpp.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 94776f1..c77e9fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,11 @@ cmake_minimum_required (VERSION 3.1) project (yandere) -set(CMAKE_BUILD_TYPE Debug) - -add_subdirectory(vendor/libtwittercpp) +add_subdirectory(vendor/mastodonpp) add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) -include_directories(vendor/libtwittercpp/src vendor/yaml-cpp/include) +include_directories(vendor/mastodonpp/include vendor/yaml-cpp/include) add_executable(yandere yandere.cpp) -set_property(TARGET yandere PROPERTY CXX_STANDARD 11) +set_property(TARGET yandere PROPERTY CXX_STANDARD 17) set_property(TARGET yandere PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(yandere yaml-cpp twitter++) +target_link_libraries(yandere yaml-cpp mastodonpp) diff --git a/vendor/mastodonpp b/vendor/mastodonpp new file mode 160000 index 0000000..c48f1dc --- /dev/null +++ b/vendor/mastodonpp @@ -0,0 +1 @@ +Subproject commit c48f1dc3d0566cef2baf96df7b3a7c55490a3e91 diff --git a/yandere.cpp b/yandere.cpp index 4edb550..31746e1 100644 --- a/yandere.cpp +++ b/yandere.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include @@ -12,17 +12,17 @@ template std::string implode(InputIterator first, InputIterator last, std::string delimiter) { std::stringstream result; - + for (InputIterator it = first; it != last; it++) { if (it != first) { result << delimiter; } - + result << *it; } - + return result.str(); } @@ -30,52 +30,41 @@ template Container split(std::string input, std::string delimiter) { Container result; - + while (!input.empty()) { int divider = input.find(delimiter); if (divider == std::string::npos) { result.push_back(input); - + input = ""; } else { result.push_back(input.substr(0, divider)); - + input = input.substr(divider+delimiter.length()); } } - + return result; } -int main(int argc, char** argv) +void run(const std::string& configfile) { - if (argc != 2) - { - std::cout << "usage: yandere [configfile]" << std::endl; - return -1; - } - - 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}; + std::map> groups; std::ifstream datafile(config["forms"].as()); if (!datafile.is_open()) { std::cout << "Could not find forms file." << std::endl; - return 1; } - + bool newgroup = true; std::string line; std::string curgroup; @@ -85,7 +74,7 @@ int main(int argc, char** argv) { line.pop_back(); } - + if (newgroup) { curgroup = line; @@ -112,10 +101,10 @@ int main(int argc, char** argv) for (;;) { - std::cout << "Generating tweet" << std::endl; - + std::cout << "Generating post" << std::endl; + std::map variables; - + std::string action = "{MAIN}"; int tknloc; while ((tknloc = action.find("{")) != std::string::npos) @@ -128,7 +117,7 @@ int main(int argc, char** argv) modifier = token.substr(modloc+1); token = token.substr(0, modloc); } - + int eqloc; std::string eqvarname; if ((eqloc = token.find("=")) != std::string::npos) @@ -136,12 +125,12 @@ int main(int argc, char** argv) eqvarname = token.substr(0, eqloc); token = token.substr(eqloc+1); } - + std::string canontkn; std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) { return std::toupper(ch); }); - + std::string result; if (canontkn == "\\N") { @@ -155,12 +144,12 @@ int main(int argc, char** argv) result = group[dist(random_engine)]; } - + if (!eqvarname.empty()) { variables[eqvarname] = result; } - + if (modifier == "indefinite") { if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1]))) @@ -173,7 +162,7 @@ int main(int argc, char** argv) result = "a " + result; } } - + std::string finalresult; if (islower(token[0])) { @@ -187,30 +176,56 @@ int main(int argc, char** argv) { word[0] = std::toupper(word[0]); } - + finalresult = implode(std::begin(words), std::end(words), " "); } else { finalresult = result; } - + action.replace(tknloc, action.find("}")-tknloc+1, finalresult); } - - action.resize(140); - - try - { - client.updateStatus(action); - } catch (const twitter::twitter_error& e) + + const mastodonpp::parametermap parameters{{"status", action}}; + auto answer{connection.post(mastodonpp::API::v1::statuses, parameters)}; + if (!answer) { - std::cout << "Twitter error: " << e.what() << std::endl; + 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::cout << action << std::endl; std::cout << "Waiting" << std::endl; - + std::this_thread::sleep_for(std::chrono::hours(1)); - + std::cout << std::endl; } } + +int main(int argc, char** argv) +{ + if (argc != 2) + { + std::cout << "usage: yandere [configfile]" << std::endl; + return -1; + } + + std::string configfile(argv[1]); + + try + { + run(configfile); + } catch (const mastodonpp::CURLException& e) + { + std::cout << e.what() << std::endl; + } + + return 0; +} -- cgit 1.4.1