diff options
Diffstat (limited to 'nancy.cpp')
-rw-r--r-- | nancy.cpp | 84 |
1 files changed, 45 insertions, 39 deletions
diff --git a/nancy.cpp b/nancy.cpp index 6a89061..47f9c18 100644 --- a/nancy.cpp +++ b/nancy.cpp | |||
@@ -1,11 +1,12 @@ | |||
1 | #include <yaml-cpp/yaml.h> | 1 | #include <yaml-cpp/yaml.h> |
2 | #include <verbly.h> | ||
3 | #include <twitter.h> | ||
4 | #include <fstream> | ||
2 | #include <iostream> | 5 | #include <iostream> |
3 | #include <cstdlib> | ||
4 | #include <ctime> | ||
5 | #include <sstream> | 6 | #include <sstream> |
6 | #include <twitcurl.h> | 7 | #include <chrono> |
7 | #include <unistd.h> | 8 | #include <random> |
8 | #include <verbly.h> | 9 | #include <thread> |
9 | 10 | ||
10 | std::string capitalize(std::string input) | 11 | std::string capitalize(std::string input) |
11 | { | 12 | { |
@@ -33,28 +34,27 @@ std::string capitalize(std::string input) | |||
33 | 34 | ||
34 | int main(int argc, char** argv) | 35 | int main(int argc, char** argv) |
35 | { | 36 | { |
36 | srand(time(NULL)); | 37 | std::random_device random_device; |
37 | 38 | std::mt19937 random_engine{random_device()}; | |
38 | YAML::Node config = YAML::LoadFile("config.yml"); | ||
39 | 39 | ||
40 | // Forms | 40 | // Forms |
41 | std::vector<std::string> forms; | 41 | std::vector<std::string> forms; |
42 | std::ifstream formfile("forms.txt"); | ||
43 | if (formfile.is_open()) | ||
44 | { | 42 | { |
45 | while (!formfile.eof()) | 43 | std::ifstream formfile("forms.txt"); |
44 | if (formfile.is_open()) | ||
46 | { | 45 | { |
47 | std::string l; | 46 | while (!formfile.eof()) |
48 | getline(formfile, l); | ||
49 | if (l.back() == '\r') | ||
50 | { | 47 | { |
51 | l.pop_back(); | 48 | std::string l; |
52 | } | 49 | getline(formfile, l); |
50 | if (l.back() == '\r') | ||
51 | { | ||
52 | l.pop_back(); | ||
53 | } | ||
53 | 54 | ||
54 | forms.push_back(l); | 55 | forms.push_back(l); |
56 | } | ||
55 | } | 57 | } |
56 | |||
57 | formfile.close(); | ||
58 | } | 58 | } |
59 | 59 | ||
60 | if (forms.size() == 0) | 60 | if (forms.size() == 0) |
@@ -67,31 +67,36 @@ int main(int argc, char** argv) | |||
67 | // verbly | 67 | // verbly |
68 | verbly::data database("data.sqlite3"); | 68 | verbly::data database("data.sqlite3"); |
69 | 69 | ||
70 | 70 | ||
71 | twitCurl twitter; | 71 | YAML::Node config = YAML::LoadFile("config.yml"); |
72 | twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); | 72 | |
73 | twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); | 73 | twitter::auth auth; |
74 | twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>()); | 74 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); |
75 | twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>()); | 75 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); |
76 | auth.setAccessKey(config["access_key"].as<std::string>()); | ||
77 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | ||
78 | |||
79 | twitter::client client(auth); | ||
76 | 80 | ||
77 | for (;;) | 81 | for (;;) |
78 | { | 82 | { |
79 | std::cout << "Generating tweet" << std::endl; | 83 | std::cout << "Generating tweet..." << std::endl; |
80 | 84 | ||
81 | std::string form = forms[rand() % forms.size()]; | 85 | int form_i = std::uniform_int_distribution<int>(0, forms.size()-1)(random_engine); |
86 | std::string form = forms[form_i]; | ||
82 | 87 | ||
83 | // Adjectives | 88 | // Adjectives |
84 | int i; | 89 | int i; |
85 | while ((i = form.find("{adj}")) != std::string::npos) | 90 | while ((i = form.find("{adj}")) != std::string::npos) |
86 | { | 91 | { |
87 | verbly::adjective adj = database.adjectives().random(true).limit(1).run().front(); | 92 | verbly::adjective adj = database.adjectives().random().limit(1).run().front(); |
88 | form.replace(i, 5, capitalize(adj.base_form())); | 93 | form.replace(i, 5, capitalize(adj.base_form())); |
89 | } | 94 | } |
90 | 95 | ||
91 | // Nouns | 96 | // Nouns |
92 | while ((i = form.find("{noun}")) != std::string::npos) | 97 | while ((i = form.find("{noun}")) != std::string::npos) |
93 | { | 98 | { |
94 | verbly::noun n = database.nouns().is_not_proper(true).random(true).limit(1).run().front(); | 99 | verbly::noun n = database.nouns().is_not_proper().random().limit(1).run().front(); |
95 | form.replace(i, 6, capitalize(n.singular_form())); | 100 | form.replace(i, 6, capitalize(n.singular_form())); |
96 | } | 101 | } |
97 | 102 | ||
@@ -99,18 +104,19 @@ int main(int argc, char** argv) | |||
99 | { | 104 | { |
100 | continue; | 105 | continue; |
101 | } | 106 | } |
102 | 107 | ||
103 | std::string replyMsg; | 108 | try |
104 | if (twitter.statusUpdate(form)) | ||
105 | { | 109 | { |
106 | twitter.getLastWebResponse(replyMsg); | 110 | client.updateStatus(form); |
107 | std::cout << "Twitter message: " << replyMsg << std::endl; | 111 | |
108 | } else { | 112 | std::cout << "Tweeted!" << std::endl; |
109 | twitter.getLastCurlError(replyMsg); | 113 | } catch (const twitter::twitter_error& e) |
110 | std::cout << "Curl error: " << replyMsg << std::endl; | 114 | { |
115 | std::cout << "Twitter error: " << e.what() << std::endl; | ||
111 | } | 116 | } |
112 | 117 | ||
113 | std::cout << "Waiting" << std::endl; | 118 | std::cout << "Waiting..." << std::endl; |
114 | sleep(60 * 60 * 3); | 119 | |
120 | std::this_thread::sleep_for(std::chrono::hours(1)); | ||
115 | } | 121 | } |
116 | } | 122 | } |