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 | |
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.
-rw-r--r-- | .gitmodules | 6 | ||||
-rw-r--r-- | CMakeLists.txt | 9 | ||||
-rw-r--r-- | blessed.cpp | 86 | ||||
m--------- | vendor/libtwittercpp | 0 | ||||
m--------- | vendor/twitcurl | 0 |
5 files changed, 51 insertions, 50 deletions
diff --git a/.gitmodules b/.gitmodules index 08a42de..79aa7f0 100644 --- a/.gitmodules +++ b/.gitmodules | |||
@@ -1,9 +1,9 @@ | |||
1 | [submodule "vendor/verbly"] | 1 | [submodule "vendor/verbly"] |
2 | path = vendor/verbly | 2 | path = vendor/verbly |
3 | url = https://github.com/hatkirby/verbly | 3 | url = https://github.com/hatkirby/verbly |
4 | [submodule "vendor/twitcurl"] | ||
5 | path = vendor/twitcurl | ||
6 | url = https://github.com/swatkat/twitcurl | ||
7 | [submodule "vendor/yaml-cpp"] | 4 | [submodule "vendor/yaml-cpp"] |
8 | path = vendor/yaml-cpp | 5 | path = vendor/yaml-cpp |
9 | url = https://github.com/jbeder/yaml-cpp | 6 | url = https://github.com/jbeder/yaml-cpp |
7 | [submodule "vendor/libtwittercpp"] | ||
8 | path = vendor/libtwittercpp | ||
9 | url = https://github.com/hatkirby/libtwittercpp | ||
diff --git a/CMakeLists.txt b/CMakeLists.txt index be6cc89..793ae7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -1,15 +1,12 @@ | |||
1 | cmake_minimum_required (VERSION 3.1) | 1 | cmake_minimum_required (VERSION 3.1) |
2 | project (blessed) | 2 | project (blessed) |
3 | 3 | ||
4 | find_package(PkgConfig) | 4 | add_subdirectory(vendor/libtwittercpp) |
5 | pkg_check_modules(sqlite3 sqlite3 REQUIRED) | ||
6 | |||
7 | add_subdirectory(vendor/twitcurl/libtwitcurl) | ||
8 | add_subdirectory(vendor/verbly) | 5 | add_subdirectory(vendor/verbly) |
9 | add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) | 6 | add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) |
10 | 7 | ||
11 | include_directories(vendor/twitcurl/libtwitcurl ${sqlite3_INCLUDE_DIR} vendor/verbly/lib vendor/yaml-cpp/include) | 8 | include_directories(vendor/libtwittercpp/src vendor/verbly/lib vendor/yaml-cpp/include) |
12 | add_executable(blessed blessed.cpp) | 9 | add_executable(blessed blessed.cpp) |
13 | set_property(TARGET blessed PROPERTY CXX_STANDARD 11) | 10 | set_property(TARGET blessed PROPERTY CXX_STANDARD 11) |
14 | set_property(TARGET blessed PROPERTY CXX_STANDARD_REQUIRED ON) | 11 | set_property(TARGET blessed PROPERTY CXX_STANDARD_REQUIRED ON) |
15 | target_link_libraries(blessed ${sqlite3_LIBRARIES} yaml-cpp twitcurl curl verbly) | 12 | target_link_libraries(blessed twitter++ yaml-cpp verbly) |
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 | } |
diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 | |||
Subproject d783c17151a98466e304b1e5f33bfca0be885fd | |||
diff --git a/vendor/twitcurl b/vendor/twitcurl deleted file mode 160000 | |||
Subproject 6659e86de7481e50977b7569c75138f7f69ad3c | |||