diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | CMakeLists.txt | 18 | ||||
| -rw-r--r-- | forms.txt | 16 | ||||
| -rw-r--r-- | nancy.cpp | 141 | ||||
| m--------- | vendor/twitcurl | 0 |
6 files changed, 179 insertions, 0 deletions
| diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d3ed4c --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1 @@ | |||
| config.yml | |||
| diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..037e2c6 --- /dev/null +++ b/.gitmodules | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | [submodule "vendor/twitcurl"] | ||
| 2 | path = vendor/twitcurl | ||
| 3 | url = https://github.com/swatkat/twitcurl | ||
| diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..307a777 --- /dev/null +++ b/CMakeLists.txt | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | cmake_minimum_required (VERSION 2.6) | ||
| 2 | project (nancy) | ||
| 3 | |||
| 4 | set(CMAKE_BUILD_TYPE Debug) | ||
| 5 | |||
| 6 | add_subdirectory(vendor/twitcurl/libtwitcurl) | ||
| 7 | |||
| 8 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") | ||
| 9 | |||
| 10 | find_package(PkgConfig) | ||
| 11 | pkg_check_modules(YamlCpp yaml-cpp REQUIRED) | ||
| 12 | find_package(Mysql REQUIRED) | ||
| 13 | |||
| 14 | include_directories(vendor/twitcurl/libtwitcurl ${MYSQL_INCLUDE_DIR}) | ||
| 15 | add_executable(nancy nancy.cpp) | ||
| 16 | set_property(TARGET nancy PROPERTY CXX_STANDARD 11) | ||
| 17 | set_property(TARGET nancy PROPERTY CXX_STANDARD_REQUIRED ON) | ||
| 18 | target_link_libraries(nancy ${YamlCpp_LIBRARIES} ${MYSQL_LIBRARIES} twitcurl curl) | ||
| diff --git a/forms.txt b/forms.txt new file mode 100644 index 0000000..307935d --- /dev/null +++ b/forms.txt | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | Nancy Drew and the Hidden {noun} | ||
| 2 | Nancy Drew and the Secret of the {noun} | ||
| 3 | Nancy Drew and the Secret of the {adj} {noun} | ||
| 4 | Nancy Drew and the Mystery of the {noun} | ||
| 5 | Nancy Drew and the Mystery of the {adj} {noun} | ||
| 6 | Nancy Drew on {adj} Island | ||
| 7 | Nancy Drew and the {adj} {noun} | ||
| 8 | Nancy Drew and the Missing {noun} | ||
| 9 | Nancy Drew and the {adj} Holiday | ||
| 10 | Nancy Drew and the Quest of the {adj} {noun} | ||
| 11 | Nancy Drew and the Quest of the {noun} | ||
| 12 | Nancy Drew and the Clue in the {adj} {noun} | ||
| 13 | Nancy Drew and the Clue in the {noun} | ||
| 14 | Nancy Drew and the {noun}'s Secret | ||
| 15 | Nancy Drew and the Phantom of {adj} {noun} | ||
| 16 | Nancy Drew and the Strange {noun} \ No newline at end of file | ||
| diff --git a/nancy.cpp b/nancy.cpp new file mode 100644 index 0000000..3e40be1 --- /dev/null +++ b/nancy.cpp | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | #include <yaml-cpp/yaml.h> | ||
| 2 | #include <iostream> | ||
| 3 | #include <mysql/mysql.h> | ||
| 4 | #include <cstdlib> | ||
| 5 | #include <ctime> | ||
| 6 | #include <sstream> | ||
| 7 | #include <twitcurl.h> | ||
| 8 | |||
| 9 | int db_error(MYSQL* driver, const char* error) | ||
| 10 | { | ||
| 11 | std::cout << error << ": " << mysql_error(driver) << std::endl; | ||
| 12 | return 1; | ||
| 13 | } | ||
| 14 | |||
| 15 | int main(int argc, char** argv) | ||
| 16 | { | ||
| 17 | srand(time(NULL)); | ||
| 18 | |||
| 19 | YAML::Node config = YAML::LoadFile("config.yml"); | ||
| 20 | const char* host = config["host"].as<std::string>().c_str(); | ||
| 21 | const char* user = config["user"].as<std::string>().c_str(); | ||
| 22 | const char* pass = config["pass"].as<std::string>().c_str(); | ||
| 23 | const char* db = config["db"].as<std::string>().c_str(); | ||
| 24 | |||
| 25 | // Forms | ||
| 26 | std::vector<std::string> forms; | ||
| 27 | std::ifstream formfile("forms.txt"); | ||
| 28 | if (formfile.is_open()) | ||
| 29 | { | ||
| 30 | while (!formfile.eof()) | ||
| 31 | { | ||
| 32 | std::string l; | ||
| 33 | getline(formfile, l); | ||
| 34 | if (l.back() == '\r') | ||
| 35 | { | ||
| 36 | l.pop_back(); | ||
| 37 | } | ||
| 38 | |||
| 39 | forms.push_back(l); | ||
| 40 | } | ||
| 41 | |||
| 42 | formfile.close(); | ||
| 43 | } | ||
| 44 | |||
| 45 | if (forms.size() == 0) | ||
| 46 | { | ||
| 47 | std::cout << "No forms found... check forms.txt." << std::endl; | ||
| 48 | |||
| 49 | return 2; | ||
| 50 | } | ||
| 51 | |||
| 52 | // WordNet data | ||
| 53 | MYSQL* driver = mysql_init(NULL); | ||
| 54 | if (!mysql_real_connect(driver, host, user, pass, db, 0, NULL, 0)) | ||
| 55 | { | ||
| 56 | return db_error(driver, "Error connecting to database"); | ||
| 57 | } | ||
| 58 | |||
| 59 | twitCurl twitter; | ||
| 60 | twitter.getOAuth().setConsumerKey(config["consumer_key"].as<std::string>()); | ||
| 61 | twitter.getOAuth().setConsumerSecret(config["consumer_secret"].as<std::string>()); | ||
| 62 | twitter.getOAuth().setOAuthTokenKey(config["access_key"].as<std::string>()); | ||
| 63 | twitter.getOAuth().setOAuthTokenSecret(config["access_secret"].as<std::string>()); | ||
| 64 | |||
| 65 | for (;;) | ||
| 66 | { | ||
| 67 | std::cout << "Generating tweet" << std::endl; | ||
| 68 | |||
| 69 | std::string form = forms[rand() % forms.size()]; | ||
| 70 | |||
| 71 | // Adjectives | ||
| 72 | int i; | ||
| 73 | while ((i = form.find("{adj}")) != std::string::npos) | ||
| 74 | { | ||
| 75 | const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'a' OR ss_type = 's' ORDER BY RAND() LIMIT 1"; | ||
| 76 | if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); | ||
| 77 | MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed"); | ||
| 78 | MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed"); | ||
| 79 | std::string adj {getword3[0]}; | ||
| 80 | mysql_free_result(getword2); | ||
| 81 | |||
| 82 | adj[0] = toupper(adj[0]); | ||
| 83 | |||
| 84 | int j; | ||
| 85 | while ((j = adj.find("_")) != std::string::npos) | ||
| 86 | { | ||
| 87 | adj[j] = ' '; | ||
| 88 | adj[j+1] = toupper(adj[j+1]); | ||
| 89 | } | ||
| 90 | |||
| 91 | if (adj[adj.size()-1] == ')') | ||
| 92 | { | ||
| 93 | adj.resize(adj.size()-3); | ||
| 94 | } | ||
| 95 | |||
| 96 | form.replace(i, 5, adj); | ||
| 97 | } | ||
| 98 | |||
| 99 | // Nouns | ||
| 100 | while ((i = form.find("{noun}")) != std::string::npos) | ||
| 101 | { | ||
| 102 | const char* getword = "SELECT word FROM wn_synset WHERE ss_type = 'n' ORDER BY RAND() LIMIT 1"; | ||
| 103 | if (mysql_query(driver, getword)) return db_error(driver, "Query failed"); | ||
| 104 | MYSQL_RES* getword2 = mysql_use_result(driver); if (getword2 == NULL) return db_error(driver, "Query failed"); | ||
| 105 | MYSQL_ROW getword3 = mysql_fetch_row(getword2); if (getword3 == NULL) return db_error(driver, "Query failed"); | ||
| 106 | std::string noun {getword3[0]}; | ||
| 107 | mysql_free_result(getword2); | ||
| 108 | |||
| 109 | noun[0] = toupper(noun[0]); | ||
| 110 | |||
| 111 | int j; | ||
| 112 | while ((j = noun.find("_")) != std::string::npos) | ||
| 113 | { | ||
| 114 | noun[j] = ' '; | ||
| 115 | noun[j+1] = toupper(noun[j+1]); | ||
| 116 | } | ||
| 117 | |||
| 118 | form.replace(i, 6, noun); | ||
| 119 | } | ||
| 120 | |||
| 121 | if (form.size() > 140) | ||
| 122 | { | ||
| 123 | continue; | ||
| 124 | } | ||
| 125 | |||
| 126 | std::string replyMsg; | ||
| 127 | if (twitter.statusUpdate(form)) | ||
| 128 | { | ||
| 129 | twitter.getLastWebResponse(replyMsg); | ||
| 130 | std::cout << "Twitter message: " << replyMsg << std::endl; | ||
| 131 | } else { | ||
| 132 | twitter.getLastCurlError(replyMsg); | ||
| 133 | std::cout << "Curl error: " << replyMsg << std::endl; | ||
| 134 | } | ||
| 135 | |||
| 136 | std::cout << "Waiting" << std::endl; | ||
| 137 | sleep(60 * 60 * 3); | ||
| 138 | } | ||
| 139 | |||
| 140 | mysql_close(driver); | ||
| 141 | } | ||
| diff --git a/vendor/twitcurl b/vendor/twitcurl new file mode 160000 | |||
| Subproject 6659e86de7481e50977b7569c75138f7f69ad3c | |||
