diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-12-04 20:00:43 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-12-04 20:00:43 -0500 |
commit | 4f88ef857af5a4356b2ea6c069416c9931d47ab2 (patch) | |
tree | 9980747eef80c262d052c432ff953281bca79da4 /blessed.cpp | |
parent | 5c73899b69991b0bc97aaf91543ccb1403c504f5 (diff) | |
download | blessed-4f88ef857af5a4356b2ea6c069416c9931d47ab2.tar.gz blessed-4f88ef857af5a4356b2ea6c069416c9931d47ab2.tar.bz2 blessed-4f88ef857af5a4356b2ea6c069416c9931d47ab2.zip |
Switched from twitcurl to libtwitter++
Also replaced usage of the C randomization interface with the C++ randomization interface, and replaced the UNIX-reliant sleep() call with the C++ thread interface. Also made some other style-related code changes.
Diffstat (limited to 'blessed.cpp')
-rw-r--r-- | blessed.cpp | 86 |
1 files changed, 45 insertions, 41 deletions
diff --git a/blessed.cpp b/blessed.cpp index faac3f8..69cc5f3 100644 --- a/blessed.cpp +++ b/blessed.cpp | |||
@@ -1,58 +1,60 @@ | |||
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> | 4 | #include <fstream> |
7 | #include <verbly.h> | 5 | #include <verbly.h> |
8 | #include <unistd.h> | 6 | #include <twitter.h> |
7 | #include <chrono> | ||
8 | #include <random> | ||
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 | std::random_device random_device; |
14 | std::mt19937 random_engine{random_device()}; | ||
13 | 15 | ||
14 | YAML::Node config = YAML::LoadFile("config.yml"); | 16 | YAML::Node config = YAML::LoadFile("config.yml"); |
15 | 17 | ||
16 | twitCurl twitter; | 18 | twitter::auth auth; |
17 | twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); | 19 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); |
18 | twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); | 20 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); |
19 | twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>()); | 21 | auth.setAccessKey(config["access_key"].as<std::string>()); |
20 | twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>()); | 22 | auth.setAccessSecret(config["access_secret"].as<std::string>()); |
23 | |||
24 | twitter::client client(auth); | ||
21 | 25 | ||
22 | verbly::data database {"data.sqlite3"}; | 26 | verbly::data database {"data.sqlite3"}; |
23 | 27 | ||
24 | std::vector<std::string> emojis; | 28 | std::vector<std::string> emojis; |
25 | std::ifstream emojifile("emojis.txt"); | ||
26 | if (!emojifile.is_open()) | ||
27 | { | ||
28 | std::cout << "Could not find emoji." << std::endl; | ||
29 | return -1; | ||
30 | } | ||
31 | |||
32 | for (;;) | ||
33 | { | 29 | { |
34 | std::string line; | 30 | std::ifstream emojifile("emojis.txt"); |
35 | if (!getline(emojifile, line)) | 31 | if (!emojifile.is_open()) |
36 | { | 32 | { |
37 | break; | 33 | std::cout << "Could not find emoji." << std::endl; |
34 | return -1; | ||
38 | } | 35 | } |
39 | 36 | ||
40 | if (line.back() == '\r') | 37 | std::string line; |
38 | while (getline(emojifile, line)) | ||
41 | { | 39 | { |
42 | line.pop_back(); | 40 | if (line.back() == '\r') |
43 | } | 41 | { |
42 | line.pop_back(); | ||
43 | } | ||
44 | 44 | ||
45 | emojis.push_back(line); | 45 | emojis.push_back(line); |
46 | } | ||
46 | } | 47 | } |
47 | 48 | ||
48 | emojifile.close(); | 49 | std::uniform_int_distribution<int> emojidist(0, emojis.size()-1); |
50 | std::bernoulli_distribution continuedist(1.0/2.0); | ||
49 | 51 | ||
50 | for (;;) | 52 | for (;;) |
51 | { | 53 | { |
52 | std::cout << "Generating tweet" << std::endl; | 54 | std::cout << "Generating tweet..." << std::endl; |
53 | 55 | ||
54 | std::string exclamation; | 56 | std::string exclamation; |
55 | for (;;) | 57 | while (exclamation.empty()) |
56 | { | 58 | { |
57 | verbly::verb v = database.verbs().random().limit(1).has_pronunciation().run().front(); | 59 | verbly::verb v = database.verbs().random().limit(1).has_pronunciation().run().front(); |
58 | auto rhyms = database.nouns().rhymes_with(v).random().limit(1).is_not_proper().run(); | 60 | auto rhyms = database.nouns().rhymes_with(v).random().limit(1).is_not_proper().run(); |
@@ -62,7 +64,6 @@ int main(int argc, char** argv) | |||
62 | if (n.base_form() != v.base_form()) | 64 | if (n.base_form() != v.base_form()) |
63 | { | 65 | { |
64 | exclamation = "god " + v.base_form() + " this " + n.base_form(); | 66 | exclamation = "god " + v.base_form() + " this " + n.base_form(); |
65 | break; | ||
66 | } | 67 | } |
67 | } | 68 | } |
68 | } | 69 | } |
@@ -73,10 +74,10 @@ int main(int argc, char** argv) | |||
73 | do | 74 | do |
74 | { | 75 | { |
75 | emn++; | 76 | emn++; |
76 | std::string em = emojis[rand() % emojis.size()]; | 77 | std::string em = emojis[emojidist(random_engine)]; |
77 | emojibef += em; | 78 | emojibef += em; |
78 | emojiaft = em + emojiaft; | 79 | emojiaft = em + emojiaft; |
79 | } while (rand() % 2 == 0); | 80 | } while (continuedist(random_engine)); |
80 | 81 | ||
81 | std::string result; | 82 | std::string result; |
82 | if (emn > 3) | 83 | if (emn > 3) |
@@ -85,19 +86,22 @@ int main(int argc, char** argv) | |||
85 | } else { | 86 | } else { |
86 | result = emojibef + " " + exclamation + " " + emojiaft; | 87 | result = emojibef + " " + exclamation + " " + emojiaft; |
87 | } | 88 | } |
89 | |||
88 | result.resize(140); | 90 | result.resize(140); |
91 | std::cout << result << std::endl; | ||
89 | 92 | ||
90 | std::string replyMsg; | 93 | try |
91 | if (twitter.statusUpdate(result)) | ||
92 | { | 94 | { |
93 | twitter.getLastWebResponse(replyMsg); | 95 | client.updateStatus(result); |
94 | std::cout << "Twitter message: " << replyMsg << std::endl; | 96 | |
95 | } else { | 97 | std::cout << "Tweeted!" << std::endl; |
96 | twitter.getLastCurlError(replyMsg); | 98 | } catch (const twitter::twitter_error& e) |
97 | std::cout << "Curl error: " << replyMsg << std::endl; | 99 | { |
100 | std::cout << "Twitter error: " << e.what() << std::endl; | ||
98 | } | 101 | } |
99 | 102 | ||
100 | std::cout << "Waiting" << std::endl; | 103 | std::cout << "Waiting..." << std::endl; |
101 | sleep(60 * 60); | 104 | |
105 | std::this_thread::sleep_for(std::chrono::hours(1)); | ||
102 | } | 106 | } |
103 | } | 107 | } |