about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2016-05-20 17:42:33 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2016-05-20 17:42:33 -0400
commita670cc63d0becc0ac24357747aa63acfb30b6675 (patch)
tree94f7c4611512581cd0ab74bf9cbcab72258345c7
parentd458c695e526e37059786821704527501ca04042 (diff)
downloadrawr-ebooks-a670cc63d0becc0ac24357747aa63acfb30b6675.tar.gz
rawr-ebooks-a670cc63d0becc0ac24357747aa63acfb30b6675.tar.bz2
rawr-ebooks-a670cc63d0becc0ac24357747aa63acfb30b6675.zip
Added ability for rawr ebooks to respond to tweets
Now using libtwitter++ instead of twitcurl!
-rw-r--r--.gitmodules6
-rw-r--r--CMakeLists.txt34
-rw-r--r--ebooks.cpp72
m---------vendor/libtwittercpp0
m---------vendor/twitcurl0
5 files changed, 72 insertions, 40 deletions
diff --git a/.gitmodules b/.gitmodules index 581c545..78fd03a 100644 --- a/.gitmodules +++ b/.gitmodules
@@ -1,6 +1,6 @@
1[submodule "vendor/twitcurl"]
2 path = vendor/twitcurl
3 url = https://github.com/swatkat/twitcurl
4[submodule "vendor/yaml-cpp"] 1[submodule "vendor/yaml-cpp"]
5 path = vendor/yaml-cpp 2 path = vendor/yaml-cpp
6 url = https://github.com/jbeder/yaml-cpp 3 url = https://github.com/jbeder/yaml-cpp
4[submodule "vendor/libtwittercpp"]
5 path = vendor/libtwittercpp
6 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 @@
1cmake_minimum_required (VERSION 2.6) 1cmake_minimum_required (VERSION 3.1)
2project (rawr-ebooks) 2project (rawr-ebooks)
3 3
4find_package(PkgConfig) 4set(CMAKE_BUILD_TYPE Debug)
5pkg_check_modules(Curl libcurl)
6 5
7if (Curl_FOUND) 6add_subdirectory(vendor/libtwittercpp)
8 add_subdirectory(vendor/twitcurl/libtwitcurl) 7include_directories(vendor/libtwittercpp/src vendor/libtwittercpp/vendor/json/src vendor/libtwittercpp/vendor/curlcpp/include)
9 include_directories(vendor/twitcurl/libtwitcurl) 8
10 9add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL)
11 add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) 10include_directories(vendor/yaml-cpp/include)
12 include_directories(vendor/yaml-cpp/include) 11
13 12find_package(aspell REQUIRED)
14 add_executable(rawr-ebooks ebooks.cpp kgramstats.cpp freevars.cpp histogram.cpp prefix_search.cpp) 13include_directories(${ASPELL_INCLUDE_DIR})
15 set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD 11) 14
16 set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD_REQUIRED ON) 15add_executable(rawr-ebooks ebooks.cpp kgramstats.cpp freevars.cpp histogram.cpp prefix_search.cpp)
17 target_link_libraries(rawr-ebooks yaml-cpp twitcurl ${Curl_LIBRARIES} aspell) 16set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD 11)
18else (Curl_FOUND) 17set_property(TARGET rawr-ebooks PROPERTY CXX_STANDARD_REQUIRED ON)
19 message(STATUS "rawr-ebooks requires yaml-cpp and twitcurl; without these, we will only make rawr-gen") 18target_link_libraries(rawr-ebooks yaml-cpp twitter++ curlcpp curl ${ASPELL_LIBRARIES} pthread)
20endif (Curl_FOUND)
21 19
22add_executable(rawr-gen gen.cpp kgramstats.cpp freevars.cpp histogram.cpp prefix_search.cpp) 20add_executable(rawr-gen gen.cpp kgramstats.cpp freevars.cpp histogram.cpp prefix_search.cpp)
23set_property(TARGET rawr-gen PROPERTY CXX_STANDARD 11) 21set_property(TARGET rawr-gen PROPERTY CXX_STANDARD 11)
24set_property(TARGET rawr-gen PROPERTY CXX_STANDARD_REQUIRED ON) 22set_property(TARGET rawr-gen PROPERTY CXX_STANDARD_REQUIRED ON)
25target_link_libraries(rawr-gen aspell) 23target_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 @@
3#include <map> 3#include <map>
4#include "kgramstats.h" 4#include "kgramstats.h"
5#include <ctime> 5#include <ctime>
6#include <vector>
7#include <cstdlib> 6#include <cstdlib>
8#include <fstream> 7#include <fstream>
9#include <iostream> 8#include <iostream>
10#include <twitcurl.h> 9#include <twitter.h>
11#include <unistd.h>
12#include <yaml-cpp/yaml.h> 10#include <yaml-cpp/yaml.h>
11#include <thread>
12#include <chrono>
13 13
14int main(int argc, char** args) 14int main(int argc, char** args)
15{ 15{
16 srand(time(NULL)); 16 srand(time(NULL));
17 rand(); rand(); rand(); rand();
17 18
18 YAML::Node config = YAML::LoadFile("config.yml"); 19 YAML::Node config = YAML::LoadFile("config.yml");
19 int delay = config["delay"].as<int>(); 20 int delay = config["delay"].as<int>();
21
22 twitter::auth auth;
23 auth.setConsumerKey(config["consumer_key"].as<std::string>());
24 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
25 auth.setAccessKey(config["access_key"].as<std::string>());
26 auth.setAccessSecret(config["access_secret"].as<std::string>());
27
28 twitter::client client(auth);
20 29
21 std::ifstream infile(config["corpus"].as<std::string>().c_str()); 30 std::ifstream infile(config["corpus"].as<std::string>().c_str());
22 std::string corpus; 31 std::string corpus;
@@ -33,28 +42,53 @@ int main(int argc, char** args)
33 42
34 std::cout << "Preprocessing corpus..." << std::endl; 43 std::cout << "Preprocessing corpus..." << std::endl;
35 kgramstats* stats = new kgramstats(corpus, 4); 44 kgramstats* stats = new kgramstats(corpus, 4);
45 std::mutex stats_mutex;
46
47 client.setUserStreamNotifyCallback([&] (twitter::notification n) {
48 if (n.getType() == twitter::notification::type::tweet)
49 {
50 std::string original = n.getTweet().getText();
51 std::string canonical;
52 std::transform(std::begin(original), std::end(original), std::back_inserter(canonical), [] (char ch) {
53 return std::tolower(ch);
54 });
55
56 if (canonical.find("@rawr_ebooks") != std::string::npos)
57 {
58 std::string doc = "@" + n.getTweet().getAuthor().getScreenName() + " ";
59 {
60 std::lock_guard<std::mutex> stats_lock(stats_mutex);
61 doc += stats->randomSentence(140 - doc.length());
62 }
63
64 twitter::tweet tw;
65 twitter::response resp = client.updateStatus(doc, tw, n.getTweet());
66 if (resp != twitter::response::ok)
67 {
68 std::cout << "Twitter error while tweeting: " << resp << std::endl;
69 }
70 }
71 }
72 });
36 73
37 twitCurl twitter; 74 client.startUserStream();
38 twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); 75 std::this_thread::sleep_for(std::chrono::minutes(1));
39 twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>());
40 twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>());
41 twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>());
42 76
43 std::cout << "Generating..." << std::endl; 77 std::cout << "Generating..." << std::endl;
44 for (;;) 78 for (;;)
45 { 79 {
46 std::string doc = stats->randomSentence(140); 80 std::string doc;
47 std::string hi = doc; 81 {
48 hi.resize(140); 82 std::lock_guard<std::mutex> stats_lock(stats_mutex);
83 doc = stats->randomSentence(140);
84 }
85 doc.resize(140);
49 86
50 std::string replyMsg; 87 twitter::tweet tw;
51 if (twitter.statusUpdate(hi)) 88 twitter::response resp = client.updateStatus(doc, tw);
89 if (resp != twitter::response::ok)
52 { 90 {
53 twitter.getLastWebResponse(replyMsg); 91 std::cout << "Twitter error while tweeting: " << resp << std::endl;
54 std::cout << "Twitter message: " << replyMsg << std::endl;
55 } else {
56 twitter.getLastCurlError(replyMsg);
57 std::cout << "Curl error: " << replyMsg << std::endl;
58 } 92 }
59 93
60 int waitlen = rand() % delay; 94 int waitlen = rand() % delay;
@@ -86,7 +120,7 @@ int main(int argc, char** args)
86 std::cout << "Sleeping for " << (waitlen/60/60/24) << " days..." << std::endl; 120 std::cout << "Sleeping for " << (waitlen/60/60/24) << " days..." << std::endl;
87 } 121 }
88 122
89 sleep(waitlen); 123 std::this_thread::sleep_for(std::chrono::seconds(waitlen));
90 } 124 }
91 125
92 return 0; 126 return 0;
diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000
Subproject 77b6ba9a45714b0b91b9ad782877bce5ca92a25
diff --git a/vendor/twitcurl b/vendor/twitcurl deleted file mode 160000
Subproject 6659e86de7481e50977b7569c75138f7f69ad3c