about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--.gitmodules6
-rw-r--r--CMakeLists.txt13
-rw-r--r--nancy.cpp84
m---------vendor/libtwittercpp0
m---------vendor/twitcurl0
m---------vendor/verbly0
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 @@
1cmake_minimum_required (VERSION 2.6) 1cmake_minimum_required (VERSION 3.1)
2project (nancy) 2project (nancy)
3 3
4set(CMAKE_BUILD_TYPE Debug) 4add_subdirectory(vendor/libtwittercpp)
5
6add_subdirectory(vendor/twitcurl/libtwitcurl)
7add_subdirectory(vendor/verbly) 5add_subdirectory(vendor/verbly)
8add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL) 6add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL)
9 7
10find_package(PkgConfig) 8include_directories(vendor/yaml-cpp/include vendor/libtwittercpp/src vendor/verbly/lib)
11pkg_check_modules(sqlite3 sqlite3 REQUIRED)
12
13include_directories(vendor/yaml-cpp/include vendor/twitcurl/libtwitcurl vendor/verbly/lib)
14add_executable(nancy nancy.cpp) 9add_executable(nancy nancy.cpp)
15set_property(TARGET nancy PROPERTY CXX_STANDARD 11) 10set_property(TARGET nancy PROPERTY CXX_STANDARD 11)
16set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON) 11set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON)
17target_link_libraries(nancy yaml-cpp twitcurl curl verbly ${sqlite3_LIBRARIES}) 12target_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
10std::string capitalize(std::string input) 11std::string capitalize(std::string input)
11{ 12{
@@ -33,28 +34,27 @@ std::string capitalize(std::string input)
33 34
34int main(int argc, char** argv) 35int 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 // Twitter 70 // Twitter
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