From a670cc63d0becc0ac24357747aa63acfb30b6675 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 20 May 2016 17:42:33 -0400 Subject: Added ability for rawr ebooks to respond to tweets Now using libtwitter++ instead of twitcurl! --- ebooks.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 19 deletions(-) (limited to 'ebooks.cpp') diff --git a/ebooks.cpp b/ebooks.cpp index b586d63..ebb944a 100644 --- a/ebooks.cpp +++ b/ebooks.cpp @@ -3,20 +3,29 @@ #include #include "kgramstats.h" #include -#include #include #include #include -#include -#include +#include #include +#include +#include int main(int argc, char** args) { srand(time(NULL)); + rand(); rand(); rand(); rand(); YAML::Node config = YAML::LoadFile("config.yml"); int delay = config["delay"].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); std::ifstream infile(config["corpus"].as().c_str()); std::string corpus; @@ -33,28 +42,53 @@ int main(int argc, char** args) std::cout << "Preprocessing corpus..." << std::endl; kgramstats* stats = new kgramstats(corpus, 4); + std::mutex stats_mutex; + + client.setUserStreamNotifyCallback([&] (twitter::notification n) { + if (n.getType() == twitter::notification::type::tweet) + { + std::string original = n.getTweet().getText(); + std::string canonical; + std::transform(std::begin(original), std::end(original), std::back_inserter(canonical), [] (char ch) { + return std::tolower(ch); + }); + + if (canonical.find("@rawr_ebooks") != std::string::npos) + { + std::string doc = "@" + n.getTweet().getAuthor().getScreenName() + " "; + { + std::lock_guard stats_lock(stats_mutex); + doc += stats->randomSentence(140 - doc.length()); + } + + twitter::tweet tw; + twitter::response resp = client.updateStatus(doc, tw, n.getTweet()); + if (resp != twitter::response::ok) + { + std::cout << "Twitter error while tweeting: " << resp << std::endl; + } + } + } + }); - 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()); + client.startUserStream(); + std::this_thread::sleep_for(std::chrono::minutes(1)); std::cout << "Generating..." << std::endl; for (;;) { - std::string doc = stats->randomSentence(140); - std::string hi = doc; - hi.resize(140); + std::string doc; + { + std::lock_guard stats_lock(stats_mutex); + doc = stats->randomSentence(140); + } + doc.resize(140); - std::string replyMsg; - if (twitter.statusUpdate(hi)) + twitter::tweet tw; + twitter::response resp = client.updateStatus(doc, tw); + if (resp != twitter::response::ok) { - 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 while tweeting: " << resp << std::endl; } int waitlen = rand() % delay; @@ -86,7 +120,7 @@ int main(int argc, char** args) std::cout << "Sleeping for " << (waitlen/60/60/24) << " days..." << std::endl; } - sleep(waitlen); + std::this_thread::sleep_for(std::chrono::seconds(waitlen)); } return 0; -- cgit 1.4.1