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