about summary refs log tree commit diff stats
path: root/cadence.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-03-28 16:08:46 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-03-28 16:08:46 -0400
commit10a402f32a6d443e959b2caed187bccf161c1645 (patch)
tree54da9d9a47bddc39f1a51eab4a20d36f0e4433a2 /cadence.cpp
parent251248adfab36b061eb8fdd08aa54b318462da65 (diff)
downloadcadence-10a402f32a6d443e959b2caed187bccf161c1645.tar.gz
cadence-10a402f32a6d443e959b2caed187bccf161c1645.tar.bz2
cadence-10a402f32a6d443e959b2caed187bccf161c1645.zip
Modernized bot
Diffstat (limited to 'cadence.cpp')
-rw-r--r--cadence.cpp73
1 files changed, 32 insertions, 41 deletions
diff --git a/cadence.cpp b/cadence.cpp index 97a74b7..feb02e1 100644 --- a/cadence.cpp +++ b/cadence.cpp
@@ -1,45 +1,26 @@
1#include "cadence.h"
1#include <stdexcept> 2#include <stdexcept>
2#include <vector>
3#include <string>
4#include <yaml-cpp/yaml.h> 3#include <yaml-cpp/yaml.h>
5#include <sqlite3.h>
6#include <iostream> 4#include <iostream>
7#include <fstream> 5#include <fstream>
8#include <twitter.h>
9#include <chrono> 6#include <chrono>
10#include <thread> 7#include <thread>
11#include <random>
12#include <list> 8#include <list>
13#include <hkutil/string.h> 9#include <hkutil/string.h>
14#include <hkutil/database.h>
15 10
16int main(int argc, char** argv) 11cadence::cadence(
12 std::string configFile,
13 std::mt19937& rng) :
14 rng_(rng)
17{ 15{
18 if (argc != 2) 16 // Load the config file.
19 { 17 YAML::Node config = YAML::LoadFile(configFile);
20 std::cout << "usage: cadence [configfile]" << std::endl;
21 return -1;
22 }
23
24 std::string configfile(argv[1]);
25 YAML::Node config = YAML::LoadFile(configfile);
26
27 // Set up the Twitter client.
28 twitter::auth auth;
29 auth.setConsumerKey(config["consumer_key"].as<std::string>());
30 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
31 auth.setAccessKey(config["access_key"].as<std::string>());
32 auth.setAccessSecret(config["access_secret"].as<std::string>());
33
34 twitter::client client(auth);
35 18
36 // Read in the forms file. 19 // Read in the forms file.
37 std::map<std::string, std::vector<std::string>> groups;
38 std::ifstream datafile(config["forms"].as<std::string>()); 20 std::ifstream datafile(config["forms"].as<std::string>());
39 if (!datafile.is_open()) 21 if (!datafile.is_open())
40 { 22 {
41 std::cout << "Could not find forms file." << std::endl; 23 throw std::invalid_argument("Could not find forms file");
42 return 1;
43 } 24 }
44 25
45 bool newgroup = true; 26 bool newgroup = true;
@@ -61,30 +42,40 @@ int main(int argc, char** argv)
61 { 42 {
62 newgroup = true; 43 newgroup = true;
63 } else { 44 } else {
64 groups[curgroup].push_back(line); 45 groups_[curgroup].push_back(line);
65 } 46 }
66 } 47 }
67 } 48 }
68 49
69 // Initialize the random number generator.
70 std::random_device random_device;
71 std::mt19937 random_engine{random_device()};
72
73 // Connect to the AcousticBrainz data file. 50 // Connect to the AcousticBrainz data file.
74 hatkirby::database abdb( 51 database_ =
75 config["acoustic_datafile"].as<std::string>(), 52 std::unique_ptr<hatkirby::database>(
76 hatkirby::dbmode::read); 53 new hatkirby::database(
54 config["acoustic_datafile"].as<std::string>(),
55 hatkirby::dbmode::read));
77 56
57 // Set up the Twitter client.
58 twitter::auth auth;
59 auth.setConsumerKey(config["consumer_key"].as<std::string>());
60 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
61 auth.setAccessKey(config["access_key"].as<std::string>());
62 auth.setAccessSecret(config["access_secret"].as<std::string>());
63
64 client_ = std::unique_ptr<twitter::client>(new twitter::client(auth));
65}
66
67void cadence::run() const
68{
78 for (;;) 69 for (;;)
79 { 70 {
80 std::cout << "Generating tweet..." << std::endl; 71 std::cout << "Generating tweet..." << std::endl;
81 72
82 hatkirby::row songRow = 73 hatkirby::row songRow =
83 abdb.queryFirst( 74 database_->queryFirst(
84 "SELECT song_id, title, artist FROM songs ORDER BY RANDOM() LIMIT 1"); 75 "SELECT song_id, title, artist FROM songs ORDER BY RANDOM() LIMIT 1");
85 76
86 hatkirby::row moodRow = 77 hatkirby::row moodRow =
87 abdb.queryFirst( 78 database_->queryFirst(
88 "SELECT mood FROM moods WHERE song_id = ? ORDER BY RANDOM() LIMIT 1", 79 "SELECT mood FROM moods WHERE song_id = ? ORDER BY RANDOM() LIMIT 1",
89 { mpark::get<int>(songRow[0]) }); 80 { mpark::get<int>(songRow[0]) });
90 81
@@ -118,11 +109,11 @@ int main(int argc, char** argv)
118 } else if (canontkn == "SONG") 109 } else if (canontkn == "SONG")
119 { 110 {
120 result = "\"" + songTitle + "\""; 111 result = "\"" + songTitle + "\"";
121 } else if (groups.count(canontkn)) { 112 } else if (groups_.count(canontkn)) {
122 auto& group = groups.at(canontkn); 113 auto& group = groups_.at(canontkn);
123 std::uniform_int_distribution<int> dist(0, group.size() - 1); 114 std::uniform_int_distribution<int> dist(0, group.size() - 1);
124 115
125 result = group[dist(random_engine)]; 116 result = group[dist(rng_)];
126 } else { 117 } else {
127 throw std::logic_error("No such form as " + canontkn); 118 throw std::logic_error("No such form as " + canontkn);
128 } 119 }
@@ -175,7 +166,7 @@ int main(int argc, char** argv)
175 { 166 {
176 try 167 try
177 { 168 {
178 client.updateStatus(action); 169 client_->updateStatus(action);
179 170
180 std::cout << action << std::endl; 171 std::cout << action << std::endl;
181 } catch (const twitter::twitter_error& e) 172 } catch (const twitter::twitter_error& e)