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! --- .gitmodules | 6 ++--- CMakeLists.txt | 34 ++++++++++++------------- ebooks.cpp | 72 ++++++++++++++++++++++++++++++++++++++-------------- vendor/libtwittercpp | 1 + vendor/twitcurl | 1 - 5 files changed, 73 insertions(+), 41 deletions(-) create mode 160000 vendor/libtwittercpp delete mode 160000 vendor/twitcurl diff --git a/.gitmodules b/.gitmodules index 581c545..78fd03a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ -[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/libtwittercpp"] + path = vendor/libtwittercpp + url = https://github.com/hatkirby/libtwittercpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f43e46c..f5e1277 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,25 +1,23 @@ -cmake_minimum_required (VERSION 2.6) +cmake_minimum_required (VERSION 3.1) project (rawr-ebooks) -find_package(PkgConfig) -pkg_check_modules(Curl libcurl) +set(CMAKE_BUILD_TYPE Debug) -if (Curl_FOUND) - add_subdirectory(vendor/twitcurl/libtwitcurl) - include_directories(vendor/twitcurl/libtwitcurl) - - add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) - include_directories(vendor/yaml-cpp/include) - - add_executable(rawr-ebooks ebooks.cpp kgramstats.cpp freevars.cpp histogram.cpp prefix_search.cpp) - set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD 11) - set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD_REQUIRED ON) - target_link_libraries(rawr-ebooks yaml-cpp twitcurl ${Curl_LIBRARIES} aspell) -else (Curl_FOUND) - message(STATUS "rawr-ebooks requires yaml-cpp and twitcurl; without these, we will only make rawr-gen") -endif (Curl_FOUND) +add_subdirectory(vendor/libtwittercpp) +include_directories(vendor/libtwittercpp/src vendor/libtwittercpp/vendor/json/src vendor/libtwittercpp/vendor/curlcpp/include) + +add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) +include_directories(vendor/yaml-cpp/include) + +find_package(aspell REQUIRED) +include_directories(${ASPELL_INCLUDE_DIR}) + +add_executable(rawr-ebooks ebooks.cpp kgramstats.cpp freevars.cpp histogram.cpp prefix_search.cpp) +set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD 11) +set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD_REQUIRED ON) +target_link_libraries(rawr-ebooks yaml-cpp twitter++ curlcpp curl ${ASPELL_LIBRARIES} pthread) add_executable(rawr-gen gen.cpp kgramstats.cpp freevars.cpp histogram.cpp prefix_search.cpp) set_property(TARGET rawr-gen PROPERTY CXX_STANDARD 11) set_property(TARGET rawr-gen PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(rawr-gen aspell) +target_link_libraries(rawr-gen ${ASPELL_LIBRARIES}) 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; diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 index 0000000..77b6ba9 --- /dev/null +++ b/vendor/libtwittercpp @@ -0,0 +1 @@ +Subproject commit 77b6ba9a45714b0b91b9ad782877bce5ca92a258 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 -- cgit 1.4.1