diff options
Diffstat (limited to 'insult.cpp')
| -rw-r--r-- | insult.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
| diff --git a/insult.cpp b/insult.cpp new file mode 100644 index 0000000..2612d4e --- /dev/null +++ b/insult.cpp | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | #include <yaml-cpp/yaml.h> | ||
| 2 | #include <iostream> | ||
| 3 | #include <sstream> | ||
| 4 | #include <verbly.h> | ||
| 5 | #include <fstream> | ||
| 6 | #include <twitter.h> | ||
| 7 | #include <random> | ||
| 8 | #include <chrono> | ||
| 9 | #include <thread> | ||
| 10 | #include "patterner.h" | ||
| 11 | |||
| 12 | int main(int argc, char** argv) | ||
| 13 | { | ||
| 14 | if (argc != 2) | ||
| 15 | { | ||
| 16 | std::cout << "usage: insult [configfile]" << std::endl; | ||
| 17 | return -1; | ||
| 18 | } | ||
| 19 | |||
| 20 | std::string configfile(argv[1]); | ||
| 21 | YAML::Node config = YAML::LoadFile(configfile); | ||
| 22 | |||
| 23 | twitter::auth auth; | ||
| 24 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | ||
| 25 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); | ||
| 26 | auth.setAccessKey(config["access_key"].as<std::string>()); | ||
| 27 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | ||
| 28 | |||
| 29 | twitter::client client(auth); | ||
| 30 | |||
| 31 | std::random_device randomDevice; | ||
| 32 | std::mt19937 rng(randomDevice()); | ||
| 33 | |||
| 34 | try | ||
| 35 | { | ||
| 36 | verbly::database database(config["verbly_datafile"].as<std::string>()); | ||
| 37 | patterner pgen(config["forms_file"].as<std::string>(), database, rng); | ||
| 38 | |||
| 39 | std::cout << "Starting streaming..." << std::endl; | ||
| 40 | |||
| 41 | twitter::stream userStream(client, [&pgen, &client] | ||
| 42 | (const twitter::notification& n) { | ||
| 43 | if (n.getType() == twitter::notification::type::tweet) | ||
| 44 | { | ||
| 45 | if ((!n.getTweet().isRetweet()) | ||
| 46 | && (n.getTweet().getAuthor() != client.getUser())) | ||
| 47 | { | ||
| 48 | std::string original = n.getTweet().getText(); | ||
| 49 | std::string canonical; | ||
| 50 | |||
| 51 | std::transform(std::begin(original), std::end(original), | ||
| 52 | std::back_inserter(canonical), [] (char ch) | ||
| 53 | { | ||
| 54 | return std::tolower(ch); | ||
| 55 | }); | ||
| 56 | |||
| 57 | if (canonical.find("@teammeanies") != std::string::npos) | ||
| 58 | { | ||
| 59 | std::string doc = | ||
| 60 | n.getTweet().generateReplyPrefill(client.getUser()); | ||
| 61 | |||
| 62 | doc += pgen.generate(); | ||
| 63 | doc.resize(140); | ||
| 64 | |||
| 65 | try | ||
| 66 | { | ||
| 67 | client.replyToTweet(doc, n.getTweet()); | ||
| 68 | } catch (const twitter::twitter_error& error) | ||
| 69 | { | ||
| 70 | std::cout << "Twitter error while tweeting: " | ||
| 71 | << error.what() << std::endl; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | }); | ||
| 77 | |||
| 78 | std::this_thread::sleep_for(std::chrono::minutes(1)); | ||
| 79 | |||
| 80 | for (;;) | ||
| 81 | { | ||
| 82 | std::cout << "Generating tweet..." << std::endl; | ||
| 83 | |||
| 84 | std::string action = pgen.generate(); | ||
| 85 | action.resize(140); | ||
| 86 | |||
| 87 | std::cout << action << std::endl; | ||
| 88 | |||
| 89 | try | ||
| 90 | { | ||
| 91 | client.updateStatus(action); | ||
| 92 | |||
| 93 | std::cout << "Tweeted!" << std::endl; | ||
| 94 | } catch (const twitter::twitter_error& e) | ||
| 95 | { | ||
| 96 | std::cout << "Twitter error: " << e.what() << std::endl; | ||
| 97 | } | ||
| 98 | |||
| 99 | std::cout << "Waiting..." << std::endl; | ||
| 100 | |||
| 101 | std::this_thread::sleep_for(std::chrono::hours(1)); | ||
| 102 | } | ||
| 103 | } catch (std::invalid_argument& e) | ||
| 104 | { | ||
| 105 | std::cout << e.what() << std::endl; | ||
| 106 | return -1; | ||
| 107 | } | ||
| 108 | } | ||
