diff options
Diffstat (limited to 'ebooks.cpp')
| -rw-r--r-- | ebooks.cpp | 72 | 
1 files changed, 53 insertions, 19 deletions
| 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 | ||
| 14 | int main(int argc, char** args) | 14 | int 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; | 
