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. --- nancy.cpp | 84 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 39 deletions(-) (limited to 'nancy.cpp') 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)); } } -- cgit 1.4.1