From 4aedd681a0ddf511f3bb4254e2960fce7f400b04 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 27 Nov 2016 16:49:48 -0500 Subject: Switched from twitcurl to libtwitter++ Also updated verbly to remove dependence on json submodule, replaced uses of the C random API with the C++ random API, and replaced the UNIX-dependent sleep call with the sleep functionality in the C++ standard library. --- .gitmodules | 6 ++-- CMakeLists.txt | 13 +++----- nancy.cpp | 84 ++++++++++++++++++++++++++++------------------------ vendor/libtwittercpp | 1 + vendor/twitcurl | 1 - vendor/verbly | 2 +- 6 files changed, 54 insertions(+), 53 deletions(-) create mode 160000 vendor/libtwittercpp delete mode 160000 vendor/twitcurl diff --git a/.gitmodules b/.gitmodules index 253a6d8..4f93bea 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,9 @@ -[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/verbly"] path = vendor/verbly url = https://github.com/hatkirby/verbly +[submodule "vendor/libtwittercpp"] + path = vendor/libtwittercpp + url = https://github.com/hatkirby/libtwittercpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d57c96a..088e566 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,12 @@ -cmake_minimum_required (VERSION 2.6) +cmake_minimum_required (VERSION 3.1) project (nancy) -set(CMAKE_BUILD_TYPE Debug) - -add_subdirectory(vendor/twitcurl/libtwitcurl) +add_subdirectory(vendor/libtwittercpp) add_subdirectory(vendor/verbly) add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) -find_package(PkgConfig) -pkg_check_modules(sqlite3 sqlite3 REQUIRED) - -include_directories(vendor/yaml-cpp/include vendor/twitcurl/libtwitcurl vendor/verbly/lib) +include_directories(vendor/yaml-cpp/include vendor/libtwittercpp/src vendor/verbly/lib) add_executable(nancy nancy.cpp) set_property(TARGET nancy PROPERTY CXX_STANDARD 11) set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(nancy yaml-cpp twitcurl curl verbly ${sqlite3_LIBRARIES}) +target_link_libraries(nancy yaml-cpp twitter++ verbly) diff --git a/nancy.cpp b/nancy.cpp index 6a89061..47f9c18 100644 --- a/nancy.cpp +++ b/nancy.cpp @@ -1,11 +1,12 @@ #include +#include +#include +#include #include -#include -#include #include -#include -#include -#include +#include +#include +#include std::string capitalize(std::string input) { @@ -33,28 +34,27 @@ std::string capitalize(std::string input) int main(int argc, char** argv) { - srand(time(NULL)); - - YAML::Node config = YAML::LoadFile("config.yml"); + std::random_device random_device; + std::mt19937 random_engine{random_device()}; // Forms std::vector forms; - std::ifstream formfile("forms.txt"); - if (formfile.is_open()) { - while (!formfile.eof()) + std::ifstream formfile("forms.txt"); + if (formfile.is_open()) { - std::string l; - getline(formfile, l); - if (l.back() == '\r') + while (!formfile.eof()) { - l.pop_back(); - } + std::string l; + getline(formfile, l); + if (l.back() == '\r') + { + l.pop_back(); + } - forms.push_back(l); + forms.push_back(l); + } } - - formfile.close(); } if (forms.size() == 0) @@ -67,31 +67,36 @@ int main(int argc, char** argv) // verbly verbly::data database("data.sqlite3"); - // Twitter - 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 + YAML::Node config = YAML::LoadFile("config.yml"); + + 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); for (;;) { - std::cout << "Generating tweet" << std::endl; + std::cout << "Generating tweet..." << std::endl; - std::string form = forms[rand() % forms.size()]; + int form_i = std::uniform_int_distribution(0, forms.size()-1)(random_engine); + std::string form = forms[form_i]; // Adjectives int i; while ((i = form.find("{adj}")) != std::string::npos) { - verbly::adjective adj = database.adjectives().random(true).limit(1).run().front(); + verbly::adjective adj = database.adjectives().random().limit(1).run().front(); form.replace(i, 5, capitalize(adj.base_form())); } // Nouns while ((i = form.find("{noun}")) != std::string::npos) { - verbly::noun n = database.nouns().is_not_proper(true).random(true).limit(1).run().front(); + verbly::noun n = database.nouns().is_not_proper().random().limit(1).run().front(); form.replace(i, 6, capitalize(n.singular_form())); } @@ -99,18 +104,19 @@ int main(int argc, char** argv) { continue; } - - std::string replyMsg; - if (twitter.statusUpdate(form)) + + try { - twitter.getLastWebResponse(replyMsg); - std::cout << "Twitter message: " << replyMsg << std::endl; - } else { - twitter.getLastCurlError(replyMsg); - std::cout << "Curl error: " << replyMsg << std::endl; + client.updateStatus(form); + + 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 * 3); + 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..d90a1e7 --- /dev/null +++ b/vendor/libtwittercpp @@ -0,0 +1 @@ +Subproject commit d90a1e74c77ba67f25a812609fd49d479bc464dd 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 diff --git a/vendor/verbly b/vendor/verbly index 02c187f..1f898f3 160000 --- a/vendor/verbly +++ b/vendor/verbly @@ -1 +1 @@ -Subproject commit 02c187fd3141203024b6f359ec714c0b804583c0 +Subproject commit 1f898f3bd66c29672275c2c884b17ba662ced626 -- cgit 1.4.1