diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-12-13 19:55:13 -0500 | 
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-12-13 19:55:13 -0500 | 
| commit | 46db0368fbee4cfba97178837e62f4469c4fa884 (patch) | |
| tree | 582d7c4589be8a1076826efc15bb2f14cadeff8c | |
| parent | 1f146743d10f29720b82ae6b9822fab21c188f73 (diff) | |
| download | sap-46db0368fbee4cfba97178837e62f4469c4fa884.tar.gz sap-46db0368fbee4cfba97178837e62f4469c4fa884.tar.bz2 sap-46db0368fbee4cfba97178837e62f4469c4fa884.zip | |
Bot now takes path to config file as argument
That config file now also has to contain the paths to the videos directory, the fonts directory, and to a list of corpuses. Note that the number of corpuses now no longer has to be exactly 2.
| -rw-r--r-- | sap.cpp | 101 | 
1 files changed, 50 insertions, 51 deletions
| diff --git a/sap.cpp b/sap.cpp index 56fb677..7e1412a 100644 --- a/sap.cpp +++ b/sap.cpp | |||
| @@ -106,28 +106,8 @@ int minHeightRequired(std::vector<std::string> words, Magick::Image& textimage) | |||
| 106 | return result - 5; | 106 | return result - 5; | 
| 107 | } | 107 | } | 
| 108 | 108 | ||
| 109 | void layoutText(Magick::Image& textimage, Magick::Image& shadowimage, int width, int height, std::string text) | 109 | void layoutText(Magick::Image& textimage, Magick::Image& shadowimage, int width, int height, std::string text, const std::vector<std::string>& fonts) | 
| 110 | { | 110 | { | 
| 111 | DIR* fontdir; | ||
| 112 | struct dirent* ent; | ||
| 113 | if ((fontdir = opendir("fonts")) == nullptr) | ||
| 114 | { | ||
| 115 | std::cout << "Couldn't find fonts." << std::endl; | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | std::vector<std::string> fonts; | ||
| 120 | while ((ent = readdir(fontdir)) != nullptr) | ||
| 121 | { | ||
| 122 | std::string dname(ent->d_name); | ||
| 123 | if ((dname.find(".otf") != std::string::npos) || (dname.find(".ttf") != std::string::npos)) | ||
| 124 | { | ||
| 125 | fonts.push_back(dname); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | closedir(fontdir); | ||
| 130 | |||
| 131 | textimage.fillColor(Magick::Color(MaxRGB, MaxRGB, MaxRGB, MaxRGB * 0.0)); | 111 | textimage.fillColor(Magick::Color(MaxRGB, MaxRGB, MaxRGB, MaxRGB * 0.0)); | 
| 132 | shadowimage.fillColor(Magick::Color(0, 0, 0, 0)); | 112 | shadowimage.fillColor(Magick::Color(0, 0, 0, 0)); | 
| 133 | shadowimage.strokeColor("black"); | 113 | shadowimage.strokeColor("black"); | 
| @@ -145,8 +125,8 @@ void layoutText(Magick::Image& textimage, Magick::Image& shadowimage, int width, | |||
| 145 | if (font.empty() || (rand() % 10 == 0)) | 125 | if (font.empty() || (rand() % 10 == 0)) | 
| 146 | { | 126 | { | 
| 147 | font = fonts[rand() % fonts.size()]; | 127 | font = fonts[rand() % fonts.size()]; | 
| 148 | textimage.font("fonts/" + font); | 128 | textimage.font(font); | 
| 149 | shadowimage.font("fonts/" + font); | 129 | shadowimage.font(font); | 
| 150 | } | 130 | } | 
| 151 | 131 | ||
| 152 | int size = rand() % (maxSize - minSize + 1) + minSize; | 132 | int size = rand() % (maxSize - minSize + 1) + minSize; | 
| @@ -273,8 +253,15 @@ int main(int argc, char** argv) | |||
| 273 | 253 | ||
| 274 | Magick::InitializeMagick(nullptr); | 254 | Magick::InitializeMagick(nullptr); | 
| 275 | av_register_all(); | 255 | av_register_all(); | 
| 256 | |||
| 257 | if (argc != 2) | ||
| 258 | { | ||
| 259 | std::cout << "usage: sap [configfile]" << std::endl; | ||
| 260 | return -1; | ||
| 261 | } | ||
| 276 | 262 | ||
| 277 | YAML::Node config = YAML::LoadFile("config.yml"); | 263 | std::string configfile(argv[1]); | 
| 264 | YAML::Node config = YAML::LoadFile(configfile); | ||
| 278 | 265 | ||
| 279 | twitter::auth auth; | 266 | twitter::auth auth; | 
| 280 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | 267 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | 
| @@ -284,44 +271,56 @@ int main(int argc, char** argv) | |||
| 284 | 271 | ||
| 285 | twitter::client client(auth); | 272 | twitter::client client(auth); | 
| 286 | 273 | ||
| 287 | std::ifstream infile("corpus1.txt"); | 274 | // Fonts | 
| 288 | std::string corpus; | 275 | std::vector<std::string> fonts; | 
| 289 | std::string line; | ||
| 290 | while (getline(infile, line)) | ||
| 291 | { | 276 | { | 
| 292 | if (line.back() == '\r') | 277 | std::string fontdirname = config["fonts"].as<std::string>(); | 
| 278 | DIR* fontdir; | ||
| 279 | struct dirent* ent; | ||
| 280 | if ((fontdir = opendir(fontdirname.c_str())) == nullptr) | ||
| 293 | { | 281 | { | 
| 294 | line.pop_back(); | 282 | std::cout << "Couldn't find fonts." << std::endl; | 
| 283 | return -2; | ||
| 295 | } | 284 | } | 
| 296 | 285 | ||
| 297 | corpus += line + " "; | 286 | while ((ent = readdir(fontdir)) != nullptr) | 
| 287 | { | ||
| 288 | std::string dname(ent->d_name); | ||
| 289 | if ((dname.find(".otf") != std::string::npos) || (dname.find(".ttf") != std::string::npos)) | ||
| 290 | { | ||
| 291 | fonts.push_back(fontdirname + "/" + dname); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | closedir(fontdir); | ||
| 298 | } | 296 | } | 
| 299 | 297 | ||
| 300 | infile.close(); | 298 | rawr kgramstats; | 
| 301 | 299 | for (const YAML::Node& corpusname : config["corpuses"]) | |
| 302 | std::ifstream infile2("corpus2.txt"); | ||
| 303 | std::string corpus2; | ||
| 304 | while (getline(infile2, line)) | ||
| 305 | { | 300 | { | 
| 306 | if (line.back() == '\r') | 301 | std::ifstream infile(corpusname.as<std::string>()); | 
| 302 | std::string corpus; | ||
| 303 | std::string line; | ||
| 304 | while (getline(infile, line)) | ||
| 307 | { | 305 | { | 
| 308 | line.pop_back(); | 306 | if (line.back() == '\r') | 
| 307 | { | ||
| 308 | line.pop_back(); | ||
| 309 | } | ||
| 310 | |||
| 311 | corpus += line + " "; | ||
| 309 | } | 312 | } | 
| 310 | 313 | ||
| 311 | corpus2 += line + " "; | 314 | kgramstats.addCorpus(corpus); | 
| 312 | } | 315 | } | 
| 313 | 316 | ||
| 314 | infile2.close(); | ||
| 315 | |||
| 316 | rawr kgramstats; | ||
| 317 | kgramstats.addCorpus(corpus); | ||
| 318 | kgramstats.addCorpus(corpus2); | ||
| 319 | kgramstats.compile(5); | 317 | kgramstats.compile(5); | 
| 320 | kgramstats.setMinCorpora(2); | 318 | kgramstats.setMinCorpora(config["corpuses"].size()); | 
| 321 | 319 | ||
| 320 | std::string videodirname = config["videos"].as<std::string>(); | ||
| 322 | DIR* videodir; | 321 | DIR* videodir; | 
| 323 | struct dirent* ent; | 322 | struct dirent* ent; | 
| 324 | if ((videodir = opendir("videos")) == nullptr) | 323 | if ((videodir = opendir(videodirname.c_str())) == nullptr) | 
| 325 | { | 324 | { | 
| 326 | std::cout << "Couldn't find videos." << std::endl; | 325 | std::cout << "Couldn't find videos." << std::endl; | 
| 327 | return -1; | 326 | return -1; | 
| @@ -341,7 +340,7 @@ int main(int argc, char** argv) | |||
| 341 | 340 | ||
| 342 | for (;;) | 341 | for (;;) | 
| 343 | { | 342 | { | 
| 344 | std::string video = "videos/" + videos[rand() % videos.size()]; | 343 | std::string video = videodirname + "/" + videos[rand() % videos.size()]; | 
| 345 | std::cout << "Opening " << video << std::endl; | 344 | std::cout << "Opening " << video << std::endl; | 
| 346 | 345 | ||
| 347 | AVFormatContext* format = nullptr; | 346 | AVFormatContext* format = nullptr; | 
| @@ -436,7 +435,7 @@ int main(int argc, char** argv) | |||
| 436 | std::string action = kgramstats.randomSentence(rand() % 15 + 5); | 435 | std::string action = kgramstats.randomSentence(rand() % 15 + 5); | 
| 437 | Magick::Image textimage(Magick::Geometry(width, height), "transparent"); | 436 | Magick::Image textimage(Magick::Geometry(width, height), "transparent"); | 
| 438 | Magick::Image shadowimage(Magick::Geometry(width, height), "transparent"); | 437 | Magick::Image shadowimage(Magick::Geometry(width, height), "transparent"); | 
| 439 | layoutText(textimage, shadowimage, width, height, action); | 438 | layoutText(textimage, shadowimage, width, height, action, fonts); | 
| 440 | image.composite(shadowimage, 0, 0, Magick::OverCompositeOp); | 439 | image.composite(shadowimage, 0, 0, Magick::OverCompositeOp); | 
| 441 | image.composite(textimage, 0, 0, Magick::OverCompositeOp); | 440 | image.composite(textimage, 0, 0, Magick::OverCompositeOp); | 
| 442 | 441 | ||
