diff options
-rw-r--r-- | infinite.cpp | 83 | ||||
-rw-r--r-- | infinite.h | 31 | ||||
-rw-r--r-- | main.cpp | 33 | ||||
-rw-r--r-- | palette.cpp | 17 | ||||
-rw-r--r-- | palette.h | 20 | ||||
m--------- | vendor/verbly | 0 |
6 files changed, 145 insertions, 39 deletions
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 { | |||
432 | } | 432 | } |
433 | }; | 433 | }; |
434 | 434 | ||
435 | int main(int argc, char** argv) | 435 | infinite::infinite( |
436 | std::string configFile, | ||
437 | std::mt19937& rng) : | ||
438 | rng_(rng) | ||
436 | { | 439 | { |
437 | srand(time(NULL)); | 440 | // Load the config file. |
438 | rand(); rand(); rand(); rand(); | 441 | YAML::Node config = YAML::LoadFile(configFile); |
439 | |||
440 | Magick::InitializeMagick(nullptr); | ||
441 | |||
442 | if (argc != 2) | ||
443 | { | ||
444 | std::cout << "usage: infinite [configfile]" << std::endl; | ||
445 | return -1; | ||
446 | } | ||
447 | |||
448 | std::string configfile(argv[1]); | ||
449 | YAML::Node config = YAML::LoadFile(configfile); | ||
450 | 442 | ||
443 | // Set up the Twitter client. | ||
451 | twitter::auth auth; | 444 | twitter::auth auth; |
452 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | 445 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); |
453 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); | 446 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); |
454 | auth.setAccessKey(config["access_key"].as<std::string>()); | 447 | auth.setAccessKey(config["access_key"].as<std::string>()); |
455 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | 448 | auth.setAccessSecret(config["access_secret"].as<std::string>()); |
456 | 449 | ||
457 | twitter::client client(auth); | 450 | client_ = std::unique_ptr<twitter::client>(new twitter::client(auth)); |
451 | |||
452 | // Set up the verbly database. | ||
453 | database_ = std::unique_ptr<verbly::database>( | ||
454 | new verbly::database(config["verbly_datafile"].as<std::string>())); | ||
455 | |||
456 | // Set up the sentence generator. | ||
457 | generator_ = std::unique_ptr<sentence>(new sentence(*database_, rng_)); | ||
458 | 458 | ||
459 | // Parse forms file | 459 | // Parse forms file |
460 | std::map<std::string, std::vector<std::string>> groups; | ||
461 | { | 460 | { |
462 | std::ifstream datafile(config["forms_file"].as<std::string>()); | 461 | std::ifstream datafile(config["forms_file"].as<std::string>()); |
463 | if (!datafile.is_open()) | 462 | if (!datafile.is_open()) |
464 | { | 463 | { |
465 | std::cout << "Could not find forms file" << std::endl; | 464 | throw std::invalid_argument("Could not find forms file"); |
466 | return 1; | ||
467 | } | 465 | } |
468 | 466 | ||
469 | bool newgroup = true; | 467 | bool newgroup = true; |
@@ -485,7 +483,7 @@ int main(int argc, char** argv) | |||
485 | { | 483 | { |
486 | newgroup = true; | 484 | newgroup = true; |
487 | } else { | 485 | } else { |
488 | groups[curgroup].push_back(line); | 486 | groups_[curgroup].push_back(line); |
489 | } | 487 | } |
490 | } | 488 | } |
491 | } | 489 | } |
@@ -493,32 +491,37 @@ int main(int argc, char** argv) | |||
493 | 491 | ||
494 | // Read in fonts | 492 | // Read in fonts |
495 | std::string fontsdirname = config["fonts"].as<std::string>(); | 493 | std::string fontsdirname = config["fonts"].as<std::string>(); |
496 | std::vector<std::string> fonts; | 494 | DIR* fontdir; |
495 | struct dirent* ent; | ||
496 | |||
497 | if ((fontdir = opendir(fontsdirname.c_str())) == nullptr) | ||
497 | { | 498 | { |
498 | DIR* fontdir; | 499 | throw std::invalid_argument("Couldn't find fonts"); |
499 | struct dirent* ent; | 500 | } |
500 | if ((fontdir = opendir(fontsdirname.c_str())) == nullptr) | ||
501 | { | ||
502 | std::cout << "Couldn't find fonts." << std::endl; | ||
503 | return -1; | ||
504 | } | ||
505 | 501 | ||
506 | while ((ent = readdir(fontdir)) != nullptr) | 502 | while ((ent = readdir(fontdir)) != nullptr) |
503 | { | ||
504 | std::string dname(ent->d_name); | ||
505 | if ((dname.find(".otf") != std::string::npos) | ||
506 | || (dname.find(".ttf") != std::string::npos)) | ||
507 | { | 507 | { |
508 | std::string dname(ent->d_name); | 508 | fonts_.push_back(dname); |
509 | if ((dname.find(".otf") != std::string::npos) || (dname.find(".ttf") != std::string::npos)) | ||
510 | { | ||
511 | fonts.push_back(dname); | ||
512 | } | ||
513 | } | 509 | } |
514 | |||
515 | closedir(fontdir); | ||
516 | } | 510 | } |
517 | 511 | ||
512 | closedir(fontdir); | ||
513 | |||
518 | std::string colorsfile(config["colors"].as<std::string>()); | 514 | std::string colorsfile(config["colors"].as<std::string>()); |
519 | 515 | ||
520 | verbly::data database {config["verbly_datafile"].as<std::string>()}; | ||
521 | 516 | ||
517 | |||
518 | } | ||
519 | |||
520 | int main(int argc, char** argv) | ||
521 | { | ||
522 | |||
523 | |||
524 | |||
522 | for (;;) | 525 | for (;;) |
523 | { | 526 | { |
524 | // Generate the text | 527 | // Generate the text |
@@ -776,12 +779,14 @@ int main(int argc, char** argv) | |||
776 | 779 | ||
777 | textimage.annotate(towrite, Magick::CenterGravity); | 780 | textimage.annotate(towrite, Magick::CenterGravity); |
778 | textimage.opacity(((double)MaxRGB) * 0.8); | 781 | textimage.opacity(((double)MaxRGB) * 0.8); |
779 | image.composite(textimage, 0, 0, Magick::OverCompositeOp); | 782 | //image.composite(textimage, 0, 0, Magick::OverCompositeOp); |
780 | 783 | ||
781 | image.magick("jpg"); | 784 | image.magick("png"); |
782 | 785 | ||
783 | Magick::Blob outputimg; | 786 | Magick::Blob outputimg; |
784 | image.write(&outputimg); | 787 | //image.write(&outputimg); |
788 | image.write("output.png"); | ||
789 | return 1; | ||
785 | 790 | ||
786 | std::cout << "Generated image!" << std::endl << "Tweeting..." << std::endl; | 791 | std::cout << "Generated image!" << std::endl << "Tweeting..." << std::endl; |
787 | 792 | ||
diff --git a/infinite.h b/infinite.h new file mode 100644 index 0000000..5d447b5 --- /dev/null +++ b/infinite.h | |||
@@ -0,0 +1,31 @@ | |||
1 | #ifndef INFINITE_H_A2B995C7 | ||
2 | #define INFINITE_H_A2B995C7 | ||
3 | |||
4 | #include <random> | ||
5 | #include <string> | ||
6 | #include <twitter.h> | ||
7 | #include <verbly.h> | ||
8 | #include <memory> | ||
9 | #include <Magick++.h> | ||
10 | #include "sentence.h" | ||
11 | |||
12 | class infinite { | ||
13 | public: | ||
14 | |||
15 | infinite( | ||
16 | std::string configFile, | ||
17 | std::mt19937& rng); | ||
18 | |||
19 | void run() const; | ||
20 | |||
21 | private: | ||
22 | |||
23 | std::mt19937& rng_; | ||
24 | std::unique_ptr<verbly::database> database_; | ||
25 | std::unique_ptr<sentence> generator_; | ||
26 | std::unique_ptr<twitter::client> client_; | ||
27 | std::string groups_; | ||
28 | std::vector<std::string> fonts_; | ||
29 | }; | ||
30 | |||
31 | #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 @@ | |||
1 | #include "infinite.h" | ||
2 | |||
3 | int main(int argc, char** argv) | ||
4 | { | ||
5 | Magick::InitializeMagick(nullptr); | ||
6 | |||
7 | std::random_device randomDevice; | ||
8 | std::mt19937 rng(randomDevice()); | ||
9 | |||
10 | if (argc != 2) | ||
11 | { | ||
12 | std::cout << "usage: infinite [configfile]" << std::endl; | ||
13 | return -1; | ||
14 | } | ||
15 | |||
16 | std::string configfile(argv[1]); | ||
17 | |||
18 | try | ||
19 | { | ||
20 | infinite bot(configfile, rng); | ||
21 | |||
22 | try | ||
23 | { | ||
24 | bot.run(); | ||
25 | } catch (const std::exception& ex) | ||
26 | { | ||
27 | std::cout << "Error running bot: " << ex.what() << std::endl; | ||
28 | } | ||
29 | } catch (const std::exception& ex) | ||
30 | { | ||
31 | std::cout << "Error initializing bot: " << ex.what() << std::endl; | ||
32 | } | ||
33 | } | ||
diff --git a/palette.cpp b/palette.cpp new file mode 100644 index 0000000..019d956 --- /dev/null +++ b/palette.cpp | |||
@@ -0,0 +1,17 @@ | |||
1 | #include "palette.h" | ||
2 | |||
3 | palette::palette(std::string colors) | ||
4 | { | ||
5 | for (int i=0; i<256; i++) | ||
6 | { | ||
7 | std::string hexstr = colors.substr(i*6, 6); | ||
8 | gradient_.push_back(Color::fromHex(hexstr.c_str())); | ||
9 | } | ||
10 | } | ||
11 | |||
12 | Color palette::getShade(double c) const | ||
13 | { | ||
14 | size_t sc = std::min(static_cast<size_t>(floor(c * 256.0)), 255); | ||
15 | |||
16 | return gradient_.at(sc); | ||
17 | } | ||
diff --git a/palette.h b/palette.h new file mode 100644 index 0000000..15862a5 --- /dev/null +++ b/palette.h | |||
@@ -0,0 +1,20 @@ | |||
1 | #ifndef PALETTE_H_09303AB4 | ||
2 | #define PALETTE_H_09303AB4 | ||
3 | |||
4 | #include <string> | ||
5 | #include <vector> | ||
6 | #include "color.h" | ||
7 | |||
8 | class palette { | ||
9 | public: | ||
10 | |||
11 | explicit palette(std::string colors); | ||
12 | |||
13 | Color getShade(double c) const; | ||
14 | |||
15 | private: | ||
16 | |||
17 | std::vector<Color> gradient_; | ||
18 | }; | ||
19 | |||
20 | #endif /* end of include guard: PALETTE_H_09303AB4 */ | ||
diff --git a/vendor/verbly b/vendor/verbly | |||
Subproject 1f898f3bd66c29672275c2c884b17ba662ced62 | Subproject 84bae572d353b03ecb3498df83ba301a456b6c6 | ||