From 10a402f32a6d443e959b2caed187bccf161c1645 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 28 Mar 2018 16:08:46 -0400 Subject: Modernized bot --- CMakeLists.txt | 2 +- cadence.cpp | 73 +++++++++++++++++++++++++--------------------------------- cadence.h | 30 ++++++++++++++++++++++++ main.cpp | 32 +++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 42 deletions(-) create mode 100644 cadence.h create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a64833d..34827c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ include_directories( ${yaml-cpp_INCLUDE_DIRS} vendor/hkutil) -add_executable(cadence cadence.cpp) +add_executable(cadence cadence.cpp main.cpp) set_property(TARGET cadence PROPERTY CXX_STANDARD 11) set_property(TARGET cadence PROPERTY CXX_STANDARD_REQUIRED ON) target_link_libraries(cadence ${sqlite3_LIBRARIES} twitter++ ${yaml-cpp_LIBRARIES}) diff --git a/cadence.cpp b/cadence.cpp index 97a74b7..feb02e1 100644 --- a/cadence.cpp +++ b/cadence.cpp @@ -1,45 +1,26 @@ +#include "cadence.h" #include -#include -#include #include -#include #include #include -#include #include #include -#include #include #include -#include -int main(int argc, char** argv) +cadence::cadence( + std::string configFile, + std::mt19937& rng) : + rng_(rng) { - if (argc != 2) - { - std::cout << "usage: cadence [configfile]" << std::endl; - return -1; - } - - std::string configfile(argv[1]); - 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); + // Load the config file. + YAML::Node config = YAML::LoadFile(configFile); // Read in the forms file. - std::map> groups; std::ifstream datafile(config["forms"].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; @@ -61,30 +42,40 @@ int main(int argc, char** argv) { newgroup = true; } else { - groups[curgroup].push_back(line); + groups_[curgroup].push_back(line); } } } - // Initialize the random number generator. - std::random_device random_device; - std::mt19937 random_engine{random_device()}; - // Connect to the AcousticBrainz data file. - hatkirby::database abdb( - config["acoustic_datafile"].as(), - hatkirby::dbmode::read); + database_ = + std::unique_ptr( + new hatkirby::database( + config["acoustic_datafile"].as(), + hatkirby::dbmode::read)); + // 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()); + + client_ = std::unique_ptr(new twitter::client(auth)); +} + +void cadence::run() const +{ for (;;) { std::cout << "Generating tweet..." << std::endl; hatkirby::row songRow = - abdb.queryFirst( + database_->queryFirst( "SELECT song_id, title, artist FROM songs ORDER BY RANDOM() LIMIT 1"); hatkirby::row moodRow = - abdb.queryFirst( + database_->queryFirst( "SELECT mood FROM moods WHERE song_id = ? ORDER BY RANDOM() LIMIT 1", { mpark::get(songRow[0]) }); @@ -118,11 +109,11 @@ int main(int argc, char** argv) } else if (canontkn == "SONG") { result = "\"" + songTitle + "\""; - } else if (groups.count(canontkn)) { - auto& group = groups.at(canontkn); + } else if (groups_.count(canontkn)) { + auto& group = groups_.at(canontkn); std::uniform_int_distribution dist(0, group.size() - 1); - result = group[dist(random_engine)]; + result = group[dist(rng_)]; } else { throw std::logic_error("No such form as " + canontkn); } @@ -175,7 +166,7 @@ int main(int argc, char** argv) { try { - client.updateStatus(action); + client_->updateStatus(action); std::cout << action << std::endl; } catch (const twitter::twitter_error& e) diff --git a/cadence.h b/cadence.h new file mode 100644 index 0000000..408c258 --- /dev/null +++ b/cadence.h @@ -0,0 +1,30 @@ +#ifndef CADENCE_H_D4AE737D +#define CADENCE_H_D4AE737D + +#include +#include +#include +#include +#include +#include + +class cadence { +public: + + cadence( + std::string configFile, + std::mt19937& rng); + + void run() const; + +private: + + std::mt19937& rng_; + std::map> groups_; + std::unique_ptr database_; + std::unique_ptr client_; + +}; + + +#endif /* end of include guard: CADENCE_H_D4AE737D */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fe12ea5 --- /dev/null +++ b/main.cpp @@ -0,0 +1,32 @@ +#include "cadence.h" +#include + +int main(int argc, char** argv) +{ + std::random_device randomDevice; + std::mt19937 rng(randomDevice()); + + if (argc != 2) + { + std::cout << "usage: cadence [configfile]" << std::endl; + return -1; + } + + std::string configfile(argv[1]); + + try + { + cadence 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; + } +} -- cgit 1.4.1