diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-10-20 08:58:20 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-10-20 08:58:20 -0400 |
commit | 9ab9e5fcbc1534111d74c688bf7ad6d6ad46ff2b (patch) | |
tree | aae2c4d60588f32c94c7367df0b35664e468f86b /chemist.cpp | |
parent | 4dade1b6f4f2b37a7ae9e6b1f7ca28e880fd7098 (diff) | |
download | chemist-9ab9e5fcbc1534111d74c688bf7ad6d6ad46ff2b.tar.gz chemist-9ab9e5fcbc1534111d74c688bf7ad6d6ad46ff2b.tar.bz2 chemist-9ab9e5fcbc1534111d74c688bf7ad6d6ad46ff2b.zip |
Switched from twitcurl to libtwitter++
Also updated verbly, switched from C random interface to C++ random interface, and reworked the datafile to give the generated tweets a different feel.
Diffstat (limited to 'chemist.cpp')
-rw-r--r-- | chemist.cpp | 63 |
1 files changed, 39 insertions, 24 deletions
diff --git a/chemist.cpp b/chemist.cpp index fbf83a8..8b00d12 100644 --- a/chemist.cpp +++ b/chemist.cpp | |||
@@ -1,23 +1,24 @@ | |||
1 | #include <yaml-cpp/yaml.h> | 1 | #include <yaml-cpp/yaml.h> |
2 | #include <iostream> | 2 | #include <iostream> |
3 | #include <cstdlib> | ||
4 | #include <ctime> | ||
5 | #include <sstream> | 3 | #include <sstream> |
6 | #include <twitcurl.h> | ||
7 | #include <verbly.h> | 4 | #include <verbly.h> |
8 | #include <unistd.h> | 5 | #include <fstream> |
6 | #include <twitter.h> | ||
7 | #include <random> | ||
8 | #include <chrono> | ||
9 | #include <thread> | ||
9 | 10 | ||
10 | int main(int argc, char** argv) | 11 | int main(int argc, char** argv) |
11 | { | 12 | { |
12 | srand(time(NULL)); | ||
13 | |||
14 | YAML::Node config = YAML::LoadFile("config.yml"); | 13 | YAML::Node config = YAML::LoadFile("config.yml"); |
15 | 14 | ||
16 | twitCurl twitter; | 15 | twitter::auth auth; |
17 | twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); | 16 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); |
18 | twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); | 17 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); |
19 | twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>()); | 18 | auth.setAccessKey(config["access_key"].as<std::string>()); |
20 | twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>()); | 19 | auth.setAccessSecret(config["access_secret"].as<std::string>()); |
20 | |||
21 | twitter::client client(auth); | ||
21 | 22 | ||
22 | std::map<std::string, std::vector<std::string>> groups; | 23 | std::map<std::string, std::vector<std::string>> groups; |
23 | std::ifstream datafile("data.txt"); | 24 | std::ifstream datafile("data.txt"); |
@@ -51,6 +52,9 @@ int main(int argc, char** argv) | |||
51 | } | 52 | } |
52 | } | 53 | } |
53 | 54 | ||
55 | std::random_device random_device; | ||
56 | std::mt19937 random_engine{random_device()}; | ||
57 | |||
54 | verbly::data database {"data.sqlite3"}; | 58 | verbly::data database {"data.sqlite3"}; |
55 | for (;;) | 59 | for (;;) |
56 | { | 60 | { |
@@ -77,6 +81,9 @@ int main(int argc, char** argv) | |||
77 | if (canontkn == "NOUN") | 81 | if (canontkn == "NOUN") |
78 | { | 82 | { |
79 | result = database.nouns().is_not_proper().random().limit(1).with_complexity(1).run().front().singular_form(); | 83 | result = database.nouns().is_not_proper().random().limit(1).with_complexity(1).run().front().singular_form(); |
84 | } else if (canontkn == "ATTRIBUTE") | ||
85 | { | ||
86 | result = database.nouns().random().limit(1).full_hyponym_of(database.nouns().with_wnid(100024264).limit(1).run().front()).run().front().singular_form(); | ||
80 | } else if (canontkn == "ADJECTIVE") | 87 | } else if (canontkn == "ADJECTIVE") |
81 | { | 88 | { |
82 | result = database.adjectives().with_complexity(1).random().limit(1).run().front().base_form(); | 89 | result = database.adjectives().with_complexity(1).random().limit(1).run().front().base_form(); |
@@ -85,7 +92,9 @@ int main(int argc, char** argv) | |||
85 | result = database.verbs().random().limit(1).run().front().ing_form(); | 92 | result = database.verbs().random().limit(1).run().front().ing_form(); |
86 | } else if (canontkn == "YEAR") | 93 | } else if (canontkn == "YEAR") |
87 | { | 94 | { |
88 | result = std::to_string(rand() % 100 + 1916); | 95 | std::uniform_int_distribution<int> yeardist(1916,2015); |
96 | int year = yeardist(random_engine); | ||
97 | result = std::to_string(year); | ||
89 | } else if (canontkn == "REGION") | 98 | } else if (canontkn == "REGION") |
90 | { | 99 | { |
91 | auto hem1 = database.nouns().with_singular_form("eastern hemisphere").limit(1).run().front(); | 100 | auto hem1 = database.nouns().with_singular_form("eastern hemisphere").limit(1).run().front(); |
@@ -103,9 +112,14 @@ int main(int argc, char** argv) | |||
103 | { | 112 | { |
104 | auto bp = database.nouns().with_singular_form("body part").limit(1).run().front(); | 113 | auto bp = database.nouns().with_singular_form("body part").limit(1).run().front(); |
105 | result = database.nouns().full_hyponym_of({bp}).with_complexity(1).random().limit(1).run().front().singular_form(); | 114 | result = database.nouns().full_hyponym_of({bp}).with_complexity(1).random().limit(1).run().front().singular_form(); |
115 | } else if (canontkn == "\\N") | ||
116 | { | ||
117 | result = "\n"; | ||
106 | } else { | 118 | } else { |
107 | auto group = groups[canontkn]; | 119 | auto group = groups[canontkn]; |
108 | result = group[rand() % group.size()]; | 120 | std::uniform_int_distribution<int> groupdist(0, group.size()-1); |
121 | int groupind = groupdist(random_engine); | ||
122 | result = group[groupind]; | ||
109 | } | 123 | } |
110 | 124 | ||
111 | if (modifier == "indefinite") | 125 | if (modifier == "indefinite") |
@@ -144,18 +158,19 @@ int main(int argc, char** argv) | |||
144 | } | 158 | } |
145 | 159 | ||
146 | action.resize(140); | 160 | action.resize(140); |
147 | 161 | ||
148 | std::string replyMsg; | 162 | try |
149 | if (twitter.statusUpdate(action)) | ||
150 | { | 163 | { |
151 | twitter.getLastWebResponse(replyMsg); | 164 | client.updateStatus(action); |
152 | std::cout << "Twitter message: " << replyMsg << std::endl; | 165 | |
153 | } else { | 166 | std::cout << "Tweeted!" << std::endl; |
154 | twitter.getLastCurlError(replyMsg); | 167 | } catch (const twitter::twitter_error& e) |
155 | std::cout << "Curl error: " << replyMsg << std::endl; | 168 | { |
169 | std::cout << "Twitter error: " << e.what() << std::endl; | ||
156 | } | 170 | } |
157 | 171 | ||
158 | std::cout << "Waiting" << std::endl; | 172 | std::cout << "Waiting..." << std::endl; |
159 | sleep(60 * 60); | 173 | |
174 | std::this_thread::sleep_for(std::chrono::hours(1)); | ||
160 | } | 175 | } |
161 | } | 176 | } |