diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-11-27 16:49:48 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-11-27 16:49:48 -0500 |
commit | 4aedd681a0ddf511f3bb4254e2960fce7f400b04 (patch) | |
tree | eaf93841c277e38054e5df8b255fb5f53c4c3be4 | |
parent | b5628b64e540a5a5088338b6b839e99bb40a85d8 (diff) | |
download | nancy-4aedd681a0ddf511f3bb4254e2960fce7f400b04.tar.gz nancy-4aedd681a0ddf511f3bb4254e2960fce7f400b04.tar.bz2 nancy-4aedd681a0ddf511f3bb4254e2960fce7f400b04.zip |
Switched from twitcurl to libtwitter++
Also updated verbly to remove dependence on json submodule, replaced uses of the C random API with the C++ random API, and replaced the UNIX-dependent sleep call with the sleep functionality in the C++ standard library.
-rw-r--r-- | .gitmodules | 6 | ||||
-rw-r--r-- | CMakeLists.txt | 13 | ||||
-rw-r--r-- | nancy.cpp | 84 | ||||
m--------- | vendor/libtwittercpp | 0 | ||||
m--------- | vendor/twitcurl | 0 | ||||
m--------- | vendor/verbly | 0 |
6 files changed, 52 insertions, 51 deletions
diff --git a/.gitmodules b/.gitmodules index 253a6d8..4f93bea 100644 --- a/.gitmodules +++ b/.gitmodules | |||
@@ -1,9 +1,9 @@ | |||
1 | [submodule "vendor/twitcurl"] | ||
2 | path = vendor/twitcurl | ||
3 | url = https://github.com/swatkat/twitcurl | ||
4 | [submodule "vendor/yaml-cpp"] | 1 | [submodule "vendor/yaml-cpp"] |
5 | path = vendor/yaml-cpp | 2 | path = vendor/yaml-cpp |
6 | url = https://github.com/jbeder/yaml-cpp | 3 | url = https://github.com/jbeder/yaml-cpp |
7 | [submodule "vendor/verbly"] | 4 | [submodule "vendor/verbly"] |
8 | path = vendor/verbly | 5 | path = vendor/verbly |
9 | url = https://github.com/hatkirby/verbly | 6 | url = https://github.com/hatkirby/verbly |
7 | [submodule "vendor/libtwittercpp"] | ||
8 | path = vendor/libtwittercpp | ||
9 | url = https://github.com/hatkirby/libtwittercpp | ||
diff --git a/CMakeLists.txt b/CMakeLists.txt index d57c96a..088e566 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -1,17 +1,12 @@ | |||
1 | cmake_minimum_required (VERSION 2.6) | 1 | cmake_minimum_required (VERSION 3.1) |
2 | project (nancy) | 2 | project (nancy) |
3 | 3 | ||
4 | set(CMAKE_BUILD_TYPE Debug) | 4 | add_subdirectory(vendor/libtwittercpp) |
5 | |||
6 | add_subdirectory(vendor/twitcurl/libtwitcurl) | ||
7 | add_subdirectory(vendor/verbly) | 5 | add_subdirectory(vendor/verbly) |
8 | add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) | 6 | add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) |
9 | 7 | ||
10 | find_package(PkgConfig) | 8 | include_directories(vendor/yaml-cpp/include vendor/libtwittercpp/src vendor/verbly/lib) |
11 | pkg_check_modules(sqlite3 sqlite3 REQUIRED) | ||
12 | |||
13 | include_directories(vendor/yaml-cpp/include vendor/twitcurl/libtwitcurl vendor/verbly/lib) | ||
14 | add_executable(nancy nancy.cpp) | 9 | add_executable(nancy nancy.cpp) |
15 | set_property(TARGET nancy PROPERTY CXX_STANDARD 11) | 10 | set_property(TARGET nancy PROPERTY CXX_STANDARD 11) |
16 | set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON) | 11 | set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON) |
17 | target_link_libraries(nancy yaml-cpp twitcurl curl verbly ${sqlite3_LIBRARIES}) | 12 | target_link_libraries(nancy yaml-cpp twitter++ verbly) |
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 | } |
diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 | |||
Subproject d90a1e74c77ba67f25a812609fd49d479bc464d | |||
diff --git a/vendor/twitcurl b/vendor/twitcurl deleted file mode 160000 | |||
Subproject 6659e86de7481e50977b7569c75138f7f69ad3c | |||
diff --git a/vendor/verbly b/vendor/verbly | |||
Subproject 02c187fd3141203024b6f359ec714c0b804583c | Subproject 1f898f3bd66c29672275c2c884b17ba662ced62 | ||