diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-09-05 09:37:21 -0400 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-09-05 09:37:21 -0400 |
| commit | 4b2fd438e3a788e7ff07cc1f49fd0d91d7d1e068 (patch) | |
| tree | dcb65197e01fb02c85d655eda3a380eb8210b04e /yandere.cpp | |
| parent | 4dade1b6f4f2b37a7ae9e6b1f7ca28e880fd7098 (diff) | |
| download | yandere-4b2fd438e3a788e7ff07cc1f49fd0d91d7d1e068.tar.gz yandere-4b2fd438e3a788e7ff07cc1f49fd0d91d7d1e068.tar.bz2 yandere-4b2fd438e3a788e7ff07cc1f49fd0d91d7d1e068.zip | |
Forked chemist to make a simpler text substitution bot
Diffstat (limited to 'yandere.cpp')
| -rw-r--r-- | yandere.cpp | 194 |
1 files changed, 194 insertions, 0 deletions
| diff --git a/yandere.cpp b/yandere.cpp new file mode 100644 index 0000000..01b0dca --- /dev/null +++ b/yandere.cpp | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | #include <yaml-cpp/yaml.h> | ||
| 2 | #include <iostream> | ||
| 3 | #include <cstdlib> | ||
| 4 | #include <ctime> | ||
| 5 | #include <sstream> | ||
| 6 | #include <fstream> | ||
| 7 | #include <twitter.h> | ||
| 8 | #include <chrono> | ||
| 9 | #include <thread> | ||
| 10 | |||
| 11 | template <class InputIterator> | ||
| 12 | std::string implode(InputIterator first, InputIterator last, std::string delimiter) | ||
| 13 | { | ||
| 14 | std::stringstream result; | ||
| 15 | |||
| 16 | for (InputIterator it = first; it != last; it++) | ||
| 17 | { | ||
| 18 | if (it != first) | ||
| 19 | { | ||
| 20 | result << delimiter; | ||
| 21 | } | ||
| 22 | |||
| 23 | result << *it; | ||
| 24 | } | ||
| 25 | |||
| 26 | return result.str(); | ||
| 27 | } | ||
| 28 | |||
| 29 | template <class Container> | ||
| 30 | Container split(std::string input, std::string delimiter) | ||
| 31 | { | ||
| 32 | Container result; | ||
| 33 | |||
| 34 | while (!input.empty()) | ||
| 35 | { | ||
| 36 | int divider = input.find(delimiter); | ||
| 37 | if (divider == std::string::npos) | ||
| 38 | { | ||
| 39 | result.push_back(input); | ||
| 40 | |||
| 41 | input = ""; | ||
| 42 | } else { | ||
| 43 | result.push_back(input.substr(0, divider)); | ||
| 44 | |||
| 45 | input = input.substr(divider+delimiter.length()); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | return result; | ||
| 50 | } | ||
| 51 | |||
| 52 | int main(int argc, char** argv) | ||
| 53 | { | ||
| 54 | srand(time(NULL)); | ||
| 55 | rand(); rand(); rand(); rand(); | ||
| 56 | |||
| 57 | YAML::Node config = YAML::LoadFile("config.yml"); | ||
| 58 | |||
| 59 | twitter::auth auth; | ||
| 60 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | ||
| 61 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); | ||
| 62 | auth.setAccessKey(config["access_key"].as<std::string>()); | ||
| 63 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | ||
| 64 | |||
| 65 | twitter::client client(auth); | ||
| 66 | |||
| 67 | std::map<std::string, std::vector<std::string>> groups; | ||
| 68 | std::ifstream datafile("data.txt"); | ||
| 69 | if (!datafile.is_open()) | ||
| 70 | { | ||
| 71 | std::cout << "Could not find data.txt" << std::endl; | ||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | bool newgroup = true; | ||
| 76 | std::string line; | ||
| 77 | std::string curgroup; | ||
| 78 | while (getline(datafile, line)) | ||
| 79 | { | ||
| 80 | if (line.back() == '\r') | ||
| 81 | { | ||
| 82 | line.pop_back(); | ||
| 83 | } | ||
| 84 | |||
| 85 | if (newgroup) | ||
| 86 | { | ||
| 87 | curgroup = line; | ||
| 88 | newgroup = false; | ||
| 89 | } else { | ||
| 90 | if (line.empty()) | ||
| 91 | { | ||
| 92 | newgroup = true; | ||
| 93 | } else { | ||
| 94 | groups[curgroup].push_back(line); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | for (;;) | ||
| 100 | { | ||
| 101 | std::cout << "Generating tweet" << std::endl; | ||
| 102 | std::string action = "{MAIN}"; | ||
| 103 | int tknloc; | ||
| 104 | while ((tknloc = action.find("{")) != std::string::npos) | ||
| 105 | { | ||
| 106 | std::string token = action.substr(tknloc+1, action.find("}")-tknloc-1); | ||
| 107 | std::string modifier; | ||
| 108 | int modloc; | ||
| 109 | if ((modloc = token.find(":")) != std::string::npos) | ||
| 110 | { | ||
| 111 | modifier = token.substr(modloc+1); | ||
| 112 | token = token.substr(0, modloc); | ||
| 113 | } | ||
| 114 | |||
| 115 | int eqloc; | ||
| 116 | std::string eqvarname; | ||
| 117 | if ((eqloc = token.find("=")) != std::string::npos) | ||
| 118 | { | ||
| 119 | eqvarname = token.substr(0, eqloc); | ||
| 120 | token = token.substr(eqloc+1); | ||
| 121 | } | ||
| 122 | |||
| 123 | std::string canontkn; | ||
| 124 | std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) { | ||
| 125 | return std::toupper(ch); | ||
| 126 | }); | ||
| 127 | |||
| 128 | std::string result; | ||
| 129 | if (canontkn == "\\N") | ||
| 130 | { | ||
| 131 | result = "\n"; | ||
| 132 | } else { | ||
| 133 | auto group = groups[canontkn]; | ||
| 134 | result = group[rand() % group.size()]; | ||
| 135 | } | ||
| 136 | |||
| 137 | if (!eqvarname.empty()) | ||
| 138 | { | ||
| 139 | groups[eqvarname].push_back(result); | ||
| 140 | } | ||
| 141 | |||
| 142 | if (modifier == "indefinite") | ||
| 143 | { | ||
| 144 | if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1]))) | ||
| 145 | { | ||
| 146 | result = "an " + result; | ||
| 147 | } else if ((result[0] == 'a') || (result[0] == 'e') || (result[0] == 'i') || (result[0] == 'o') || (result[0] == 'u')) | ||
| 148 | { | ||
| 149 | result = "an " + result; | ||
| 150 | } else { | ||
| 151 | result = "a " + result; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | std::string finalresult; | ||
| 156 | if (islower(token[0])) | ||
| 157 | { | ||
| 158 | std::transform(std::begin(result), std::end(result), std::back_inserter(finalresult), [] (char ch) { | ||
| 159 | return std::tolower(ch); | ||
| 160 | }); | ||
| 161 | } else if (isupper(token[0]) && !isupper(token[1])) | ||
| 162 | { | ||
| 163 | auto words = split<std::list<std::string>>(result, " "); | ||
| 164 | for (auto& word : words) | ||
| 165 | { | ||
| 166 | word[0] = std::toupper(word[0]); | ||
| 167 | } | ||
| 168 | |||
| 169 | finalresult = implode(std::begin(words), std::end(words), " "); | ||
| 170 | } else { | ||
| 171 | finalresult = result; | ||
| 172 | } | ||
| 173 | |||
| 174 | action.replace(tknloc, action.find("}")-tknloc+1, finalresult); | ||
| 175 | } | ||
| 176 | |||
| 177 | action.resize(140); | ||
| 178 | |||
| 179 | try | ||
| 180 | { | ||
| 181 | client.updateStatus(action); | ||
| 182 | } catch (const twitter::twitter_error& e) | ||
| 183 | { | ||
| 184 | std::cout << "Twitter error: " << e.what() << std::endl; | ||
| 185 | } | ||
| 186 | |||
| 187 | std::cout << action << std::endl; | ||
| 188 | std::cout << "Waiting" << std::endl; | ||
| 189 | |||
| 190 | std::this_thread::sleep_for(std::chrono::hours(1)); | ||
| 191 | |||
| 192 | std::cout << std::endl; | ||
| 193 | } | ||
| 194 | } | ||
