From 793559182f0ed010ca2619e68c36fece491c70e0 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 27 Nov 2016 14:52:06 -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 | 9 +++---- furries.cpp | 72 ++++++++++++++++++++++++++++------------------------ vendor/libtwittercpp | 1 + vendor/twitcurl | 1 - vendor/verbly | 2 +- 6 files changed, 47 insertions(+), 44 deletions(-) create mode 160000 vendor/libtwittercpp delete mode 160000 vendor/twitcurl diff --git a/.gitmodules b/.gitmodules index ad942bc..79aa7f0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,9 @@ -[submodule "vendor/twitcurl"] - path = vendor/twitcurl - url = https://github.com/swatkat/twitcurl [submodule "vendor/verbly"] path = vendor/verbly url = https://github.com/hatkirby/verbly [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 266c9d0..e6a8aa7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,12 @@ cmake_minimum_required (VERSION 3.1) project (furries) -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(furries furries.cpp) set_property(TARGET furries PROPERTY CXX_STANDARD 11) set_property(TARGET furries PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(furries verbly ${sqlite3_LIBRARIES} yaml-cpp twitcurl curl) +target_link_libraries(furries verbly twitter++ yaml-cpp) diff --git a/furries.cpp b/furries.cpp index 1d10ccf..7e2f591 100644 --- a/furries.cpp +++ b/furries.cpp @@ -1,11 +1,11 @@ #include +#include +#include #include -#include -#include #include -#include -#include -#include +#include +#include +#include class fill_blanks { private: @@ -150,7 +150,8 @@ class fill_blanks { } } - void visit(verbly::token& it) + template + void visit(verbly::token& it, RNG&& rng) { switch (it.get_type()) { @@ -160,7 +161,7 @@ class fill_blanks { { if (!tkn.is_complete()) { - visit(tkn); + visit(tkn, rng); break; } @@ -209,7 +210,8 @@ class fill_blanks { continue; } - verbly::frame fr = filtered[rand() % filtered.size()]; + int fr_i = std::uniform_int_distribution(0, filtered.size()-1)(rng); + verbly::frame fr = filtered[fr_i]; verbly::token utter; for (auto part : fr.parts()) { @@ -251,7 +253,7 @@ class fill_blanks { continue; } else if (part.get_synrestrs().count("adv_loc") == 1) { - if (rand() % 2 == 0) + if (std::bernoulli_distribution(1.0/2.0)(rng)) { utter << verbly::token{"here"}; } else { @@ -286,7 +288,7 @@ class fill_blanks { auto selrestrs = fr.roles()[part.get_role()]; auto query = database.nouns().limit(1).random().is_not_proper().full_hyponym_of(parse_selrestrs(selrestrs)); verbly::noun n = query.run().front(); - if ((rand() % 2 == 0) && (part.get_synrestrs().count("definite") == 0)) + if ((std::bernoulli_distribution(1.0/2.0)(rng)) && (part.get_synrestrs().count("definite") == 0)) { utter << verbly::token{"the"}; } else { @@ -322,7 +324,8 @@ class fill_blanks { case verbly::frame::part::type::literal_preposition: { - utter << verbly::token{part.get_choices()[rand() % part.get_choices().size()]}; + int ch_i = std::uniform_int_distribution(0, part.get_choices().size()-1)(rng); + utter << verbly::token{part.get_choices()[ch_i]}; break; } @@ -374,12 +377,12 @@ class fill_blanks { { verbly::token phrase; - if (rand() % 4 == 0) + if (std::bernoulli_distribution(1.0/4.0)(rng)) { phrase << verbly::token{verbly::token::fillin_type::adverb_phrase}; } - if (rand() % 2 == 0) + if (std::bernoulli_distribution(1.0/2.0)(rng)) { phrase << verbly::token{verbly::token::fillin_type::participle_phrase}; } else { @@ -429,21 +432,23 @@ class fill_blanks { 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"}; for (;;) { - std::cout << "Generating tweet" << std::endl; - - verbly::data database {"data.sqlite3"}; + std::cout << "Generating tweet..." << std::endl; fill_blanks yeah {database}; verbly::token action{ @@ -452,23 +457,24 @@ int main(int argc, char** argv) }; while (!action.is_complete()) { - yeah.visit(action); + yeah.visit(action, random_engine); } std::string result = action.compile(); result.resize(140); - std::string replyMsg; - if (twitter.statusUpdate(result)) + try + { + client.updateStatus(result); + + std::cout << "Tweeted!" << std::endl; + } catch (const twitter::twitter_error& e) { - twitter.getLastWebResponse(replyMsg); - std::cout << "Twitter message: " << replyMsg << std::endl; - } else { - twitter.getLastCurlError(replyMsg); - std::cout << "Curl error: " << replyMsg << std::endl; + std::cout << "Twitter error: " << e.what() << std::endl; } - std::cout << "Waiting" << std::endl; - sleep(60 * 60 * 2); + 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 965a320..1f898f3 160000 --- a/vendor/verbly +++ b/vendor/verbly @@ -1 +1 @@ -Subproject commit 965a3206df834f846f2c560438c80a707dcee4cb +Subproject commit 1f898f3bd66c29672275c2c884b17ba662ced626 -- cgit 1.4.1