From 0baed246b6f287e1c03519536011f34e87d19920 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 15 Aug 2018 18:45:29 -0400 Subject: Started to modernize bot (this is completely broken) --- infinite.cpp | 83 +++++++++++++++++++++++++++++++---------------------------- infinite.h | 31 ++++++++++++++++++++++ main.cpp | 33 ++++++++++++++++++++++++ palette.cpp | 17 ++++++++++++ palette.h | 20 ++++++++++++++ vendor/verbly | 2 +- 6 files changed, 146 insertions(+), 40 deletions(-) create mode 100644 infinite.h create mode 100644 main.cpp create mode 100644 palette.cpp create mode 100644 palette.h diff --git a/infinite.cpp b/infinite.cpp index 37c4d72..1bf5c8e 100644 --- a/infinite.cpp +++ b/infinite.cpp @@ -432,38 +432,36 @@ class fill_blanks { } }; -int main(int argc, char** argv) +infinite::infinite( + std::string configFile, + std::mt19937& rng) : + rng_(rng) { - srand(time(NULL)); - rand(); rand(); rand(); rand(); - - Magick::InitializeMagick(nullptr); - - if (argc != 2) - { - std::cout << "usage: infinite [configfile]" << std::endl; - return -1; - } - - std::string configfile(argv[1]); - YAML::Node config = YAML::LoadFile(configfile); + // Load the config file. + YAML::Node config = YAML::LoadFile(configFile); + // Set up the Twitter client. twitter::auth auth; auth.setConsumerKey(config["consumer_key"].as()); auth.setConsumerSecret(config["consumer_secret"].as()); auth.setAccessKey(config["access_key"].as()); auth.setAccessSecret(config["access_secret"].as()); - twitter::client client(auth); + client_ = std::unique_ptr(new twitter::client(auth)); + + // Set up the verbly database. + database_ = std::unique_ptr( + new verbly::database(config["verbly_datafile"].as())); + + // Set up the sentence generator. + generator_ = std::unique_ptr(new sentence(*database_, rng_)); // Parse forms file - std::map> groups; { std::ifstream datafile(config["forms_file"].as()); if (!datafile.is_open()) { - std::cout << "Could not find forms file" << std::endl; - return 1; + throw std::invalid_argument("Could not find forms file"); } bool newgroup = true; @@ -485,7 +483,7 @@ int main(int argc, char** argv) { newgroup = true; } else { - groups[curgroup].push_back(line); + groups_[curgroup].push_back(line); } } } @@ -493,32 +491,37 @@ int main(int argc, char** argv) // Read in fonts std::string fontsdirname = config["fonts"].as(); - std::vector fonts; + DIR* fontdir; + struct dirent* ent; + + if ((fontdir = opendir(fontsdirname.c_str())) == nullptr) { - DIR* fontdir; - struct dirent* ent; - if ((fontdir = opendir(fontsdirname.c_str())) == nullptr) - { - std::cout << "Couldn't find fonts." << std::endl; - return -1; - } + throw std::invalid_argument("Couldn't find fonts"); + } - while ((ent = readdir(fontdir)) != nullptr) + while ((ent = readdir(fontdir)) != nullptr) + { + std::string dname(ent->d_name); + if ((dname.find(".otf") != std::string::npos) + || (dname.find(".ttf") != std::string::npos)) { - std::string dname(ent->d_name); - if ((dname.find(".otf") != std::string::npos) || (dname.find(".ttf") != std::string::npos)) - { - fonts.push_back(dname); - } + fonts_.push_back(dname); } - - closedir(fontdir); } + closedir(fontdir); + std::string colorsfile(config["colors"].as()); - verbly::data database {config["verbly_datafile"].as()}; + +} + +int main(int argc, char** argv) +{ + + + for (;;) { // Generate the text @@ -776,12 +779,14 @@ int main(int argc, char** argv) textimage.annotate(towrite, Magick::CenterGravity); textimage.opacity(((double)MaxRGB) * 0.8); - image.composite(textimage, 0, 0, Magick::OverCompositeOp); + //image.composite(textimage, 0, 0, Magick::OverCompositeOp); - image.magick("jpg"); + image.magick("png"); Magick::Blob outputimg; - image.write(&outputimg); + //image.write(&outputimg); + image.write("output.png"); + return 1; std::cout << "Generated image!" << std::endl << "Tweeting..." << std::endl; diff --git a/infinite.h b/infinite.h new file mode 100644 index 0000000..5d447b5 --- /dev/null +++ b/infinite.h @@ -0,0 +1,31 @@ +#ifndef INFINITE_H_A2B995C7 +#define INFINITE_H_A2B995C7 + +#include +#include +#include +#include +#include +#include +#include "sentence.h" + +class infinite { +public: + + infinite( + std::string configFile, + std::mt19937& rng); + + void run() const; + +private: + + std::mt19937& rng_; + std::unique_ptr database_; + std::unique_ptr generator_; + std::unique_ptr client_; + std::string groups_; + std::vector fonts_; +}; + +#endif /* end of include guard: INFINITE_H_A2B995C7 */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..7f2dfa0 --- /dev/null +++ b/main.cpp @@ -0,0 +1,33 @@ +#include "infinite.h" + +int main(int argc, char** argv) +{ + Magick::InitializeMagick(nullptr); + + std::random_device randomDevice; + std::mt19937 rng(randomDevice()); + + if (argc != 2) + { + std::cout << "usage: infinite [configfile]" << std::endl; + return -1; + } + + std::string configfile(argv[1]); + + try + { + infinite bot(configfile, rng); + + try + { + bot.run(); + } catch (const std::exception& ex) + { + std::cout << "Error running bot: " << ex.what() << std::endl; + } + } catch (const std::exception& ex) + { + std::cout << "Error initializing bot: " << ex.what() << std::endl; + } +} diff --git a/palette.cpp b/palette.cpp new file mode 100644 index 0000000..019d956 --- /dev/null +++ b/palette.cpp @@ -0,0 +1,17 @@ +#include "palette.h" + +palette::palette(std::string colors) +{ + for (int i=0; i<256; i++) + { + std::string hexstr = colors.substr(i*6, 6); + gradient_.push_back(Color::fromHex(hexstr.c_str())); + } +} + +Color palette::getShade(double c) const +{ + size_t sc = std::min(static_cast(floor(c * 256.0)), 255); + + return gradient_.at(sc); +} diff --git a/palette.h b/palette.h new file mode 100644 index 0000000..15862a5 --- /dev/null +++ b/palette.h @@ -0,0 +1,20 @@ +#ifndef PALETTE_H_09303AB4 +#define PALETTE_H_09303AB4 + +#include +#include +#include "color.h" + +class palette { +public: + + explicit palette(std::string colors); + + Color getShade(double c) const; + +private: + + std::vector gradient_; +}; + +#endif /* end of include guard: PALETTE_H_09303AB4 */ diff --git a/vendor/verbly b/vendor/verbly index 1f898f3..84bae57 160000 --- a/vendor/verbly +++ b/vendor/verbly @@ -1 +1 @@ -Subproject commit 1f898f3bd66c29672275c2c884b17ba662ced626 +Subproject commit 84bae572d353b03ecb3498df83ba301a456b6c6f -- cgit 1.4.1