From 4f88ef857af5a4356b2ea6c069416c9931d47ab2 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 4 Dec 2016 20:00:43 -0500 Subject: Switched from twitcurl to libtwitter++ Also replaced usage of the C randomization interface with the C++ randomization interface, and replaced the UNIX-reliant sleep() call with the C++ thread interface. Also made some other style-related code changes. --- .gitmodules | 6 ++-- CMakeLists.txt | 9 ++---- blessed.cpp | 86 +++++++++++++++++++++++++++------------------------- vendor/libtwittercpp | 1 + vendor/twitcurl | 1 - 5 files changed, 52 insertions(+), 51 deletions(-) create mode 160000 vendor/libtwittercpp delete mode 160000 vendor/twitcurl diff --git a/.gitmodules b/.gitmodules index 08a42de..79aa7f0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,9 @@ [submodule "vendor/verbly"] path = vendor/verbly url = https://github.com/hatkirby/verbly -[submodule "vendor/twitcurl"] - path = vendor/twitcurl - url = https://github.com/swatkat/twitcurl [submodule "vendor/yaml-cpp"] path = vendor/yaml-cpp url = https://github.com/jbeder/yaml-cpp +[submodule "vendor/libtwittercpp"] + path = vendor/libtwittercpp + url = https://github.com/hatkirby/libtwittercpp diff --git a/CMakeLists.txt b/CMakeLists.txt index be6cc89..793ae7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,12 @@ cmake_minimum_required (VERSION 3.1) project (blessed) -find_package(PkgConfig) -pkg_check_modules(sqlite3 sqlite3 REQUIRED) - -add_subdirectory(vendor/twitcurl/libtwitcurl) +add_subdirectory(vendor/libtwittercpp) add_subdirectory(vendor/verbly) add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) -include_directories(vendor/twitcurl/libtwitcurl ${sqlite3_INCLUDE_DIR} vendor/verbly/lib vendor/yaml-cpp/include) +include_directories(vendor/libtwittercpp/src vendor/verbly/lib vendor/yaml-cpp/include) add_executable(blessed blessed.cpp) set_property(TARGET blessed PROPERTY CXX_STANDARD 11) set_property(TARGET blessed PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(blessed ${sqlite3_LIBRARIES} yaml-cpp twitcurl curl verbly) +target_link_libraries(blessed twitter++ yaml-cpp verbly) diff --git a/blessed.cpp b/blessed.cpp index faac3f8..69cc5f3 100644 --- a/blessed.cpp +++ b/blessed.cpp @@ -1,58 +1,60 @@ #include #include -#include -#include #include -#include +#include #include -#include +#include +#include +#include +#include int main(int argc, char** argv) { - srand(time(NULL)); + std::random_device random_device; + std::mt19937 random_engine{random_device()}; YAML::Node config = YAML::LoadFile("config.yml"); - twitCurl twitter; - twitter.getOAuth().setConsumerKey(config["consumer_key"].as()); - twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as()); - twitter.getOAuth().setOAuthTokenKey(config["access_key"].as()); - twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as()); + 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); verbly::data database {"data.sqlite3"}; std::vector emojis; - std::ifstream emojifile("emojis.txt"); - if (!emojifile.is_open()) - { - std::cout << "Could not find emoji." << std::endl; - return -1; - } - - for (;;) { - std::string line; - if (!getline(emojifile, line)) + std::ifstream emojifile("emojis.txt"); + if (!emojifile.is_open()) { - break; + std::cout << "Could not find emoji." << std::endl; + return -1; } - - if (line.back() == '\r') + + std::string line; + while (getline(emojifile, line)) { - line.pop_back(); - } + if (line.back() == '\r') + { + line.pop_back(); + } - emojis.push_back(line); + emojis.push_back(line); + } } - emojifile.close(); + std::uniform_int_distribution emojidist(0, emojis.size()-1); + std::bernoulli_distribution continuedist(1.0/2.0); for (;;) { - std::cout << "Generating tweet" << std::endl; + std::cout << "Generating tweet..." << std::endl; std::string exclamation; - for (;;) + while (exclamation.empty()) { verbly::verb v = database.verbs().random().limit(1).has_pronunciation().run().front(); auto rhyms = database.nouns().rhymes_with(v).random().limit(1).is_not_proper().run(); @@ -62,7 +64,6 @@ int main(int argc, char** argv) if (n.base_form() != v.base_form()) { exclamation = "god " + v.base_form() + " this " + n.base_form(); - break; } } } @@ -73,10 +74,10 @@ int main(int argc, char** argv) do { emn++; - std::string em = emojis[rand() % emojis.size()]; + std::string em = emojis[emojidist(random_engine)]; emojibef += em; emojiaft = em + emojiaft; - } while (rand() % 2 == 0); + } while (continuedist(random_engine)); std::string result; if (emn > 3) @@ -85,19 +86,22 @@ int main(int argc, char** argv) } else { result = emojibef + " " + exclamation + " " + emojiaft; } + result.resize(140); + std::cout << result << std::endl; - std::string replyMsg; - if (twitter.statusUpdate(result)) + try { - twitter.getLastWebResponse(replyMsg); - std::cout << "Twitter message: " << replyMsg << std::endl; - } else { - twitter.getLastCurlError(replyMsg); - std::cout << "Curl error: " << replyMsg << std::endl; + client.updateStatus(result); + + std::cout << "Tweeted!" << std::endl; + } catch (const twitter::twitter_error& e) + { + std::cout << "Twitter error: " << e.what() << std::endl; } - std::cout << "Waiting" << std::endl; - sleep(60 * 60); + std::cout << "Waiting..." << std::endl; + + std::this_thread::sleep_for(std::chrono::hours(1)); } } diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 index 0000000..d783c17 --- /dev/null +++ b/vendor/libtwittercpp @@ -0,0 +1 @@ +Subproject commit d783c17151a98466e304b1e5f33bfca0be885fd8 diff --git a/vendor/twitcurl b/vendor/twitcurl deleted file mode 160000 index 6659e86..0000000 --- a/vendor/twitcurl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6659e86de7481e50977b7569c75138f7f69ad3c7 -- cgit 1.4.1