diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-03-27 17:13:37 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-03-27 17:13:37 -0400 |
commit | af49d3435302118d036c512aef1def848fb88089 (patch) | |
tree | 5bdec9a528cc84e97b10dc6a62bbfc5b72037ecc /cadence.cpp | |
parent | 07a11ffc6a1d23815bca45321afa0f3c8661d96d (diff) | |
download | cadence-af49d3435302118d036c512aef1def848fb88089.tar.gz cadence-af49d3435302118d036c512aef1def848fb88089.tar.bz2 cadence-af49d3435302118d036c512aef1def848fb88089.zip |
Migrated to hkutil
Diffstat (limited to 'cadence.cpp')
-rw-r--r-- | cadence.cpp | 58 |
1 files changed, 14 insertions, 44 deletions
diff --git a/cadence.cpp b/cadence.cpp index 80e4a05..704f946 100644 --- a/cadence.cpp +++ b/cadence.cpp | |||
@@ -10,7 +10,8 @@ | |||
10 | #include <thread> | 10 | #include <thread> |
11 | #include <random> | 11 | #include <random> |
12 | #include <list> | 12 | #include <list> |
13 | #include "util.h" | 13 | #include <hkutil/string.h> |
14 | #include <hkutil/database.h> | ||
14 | 15 | ||
15 | const std::vector<std::string> moods = {"party", "chill", "crazy", "happy", "sad", "instrumental", "vocal"}; | 16 | const std::vector<std::string> moods = {"party", "chill", "crazy", "happy", "sad", "instrumental", "vocal"}; |
16 | 17 | ||
@@ -72,19 +73,9 @@ int main(int argc, char** argv) | |||
72 | std::mt19937 random_engine{random_device()}; | 73 | std::mt19937 random_engine{random_device()}; |
73 | 74 | ||
74 | // Connect to the AcousticBrainz data file. | 75 | // Connect to the AcousticBrainz data file. |
75 | sqlite3* ppdb = nullptr; | 76 | hatkirby::database abdb( |
76 | 77 | config["acoustic_datafile"].as<std::string>(), | |
77 | if (sqlite3_open_v2(config["acoustic_datafile"].as<std::string>().c_str(), &ppdb, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) | 78 | hatkirby::dbmode::read); |
78 | { | ||
79 | // We still have to free the resources allocated. In the event that | ||
80 | // allocation failed, ppdb will be null and sqlite3_close_v2 will just | ||
81 | // ignore it. | ||
82 | sqlite3_close_v2(ppdb); | ||
83 | |||
84 | std::cout << "Could not open acoustic datafile." << std::endl; | ||
85 | |||
86 | return 1; | ||
87 | } | ||
88 | 79 | ||
89 | for (;;) | 80 | for (;;) |
90 | { | 81 | { |
@@ -92,36 +83,15 @@ int main(int argc, char** argv) | |||
92 | 83 | ||
93 | std::string mood = moods[std::uniform_int_distribution<int>(0, moods.size()-1)(random_engine)]; | 84 | std::string mood = moods[std::uniform_int_distribution<int>(0, moods.size()-1)(random_engine)]; |
94 | 85 | ||
95 | std::string songTitle; | 86 | hatkirby::row song = |
96 | std::string songArtist; | 87 | abdb.queryFirst( |
97 | 88 | "SELECT title, artist FROM songs WHERE category = ? ORDER BY RANDOM() LIMIT 1", | |
98 | sqlite3_stmt* ppstmt; | 89 | { mood }); |
99 | std::string query = "SELECT title, artist FROM songs WHERE category = ? ORDER BY RANDOM() LIMIT 1"; | ||
100 | |||
101 | if (sqlite3_prepare_v2(ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
102 | { | ||
103 | std::cout << "Error reading from acoustic datafile:" << std::endl; | ||
104 | std::cout << sqlite3_errmsg(ppdb) << std::endl; | ||
105 | |||
106 | return 1; | ||
107 | } | ||
108 | |||
109 | sqlite3_bind_text(ppstmt, 1, mood.c_str(), mood.length(), SQLITE_TRANSIENT); | ||
110 | |||
111 | if (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
112 | { | ||
113 | songTitle = reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0)); | ||
114 | songArtist = reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 1)); | ||
115 | } else { | ||
116 | std::cout << "Error reading from acoustic datafile:" << std::endl; | ||
117 | std::cout << sqlite3_errmsg(ppdb) << std::endl; | ||
118 | |||
119 | return 1; | ||
120 | } | ||
121 | 90 | ||
122 | sqlite3_finalize(ppstmt); | 91 | std::string songTitle = mpark::get<std::string>(song[0]); |
92 | std::string songArtist = mpark::get<std::string>(song[1]); | ||
123 | 93 | ||
124 | std::string action = "{" + cadence::uppercase(mood) + "}"; | 94 | std::string action = "{" + hatkirby::uppercase(mood) + "}"; |
125 | int tknloc; | 95 | int tknloc; |
126 | while ((tknloc = action.find("{")) != std::string::npos) | 96 | while ((tknloc = action.find("{")) != std::string::npos) |
127 | { | 97 | { |
@@ -182,13 +152,13 @@ int main(int argc, char** argv) | |||
182 | }); | 152 | }); |
183 | } else if (isupper(token[0]) && !isupper(token[1])) | 153 | } else if (isupper(token[0]) && !isupper(token[1])) |
184 | { | 154 | { |
185 | auto words = cadence::split<std::list<std::string>>(result, " "); | 155 | auto words = hatkirby::split<std::list<std::string>>(result, " "); |
186 | for (auto& word : words) | 156 | for (auto& word : words) |
187 | { | 157 | { |
188 | word[0] = std::toupper(word[0]); | 158 | word[0] = std::toupper(word[0]); |
189 | } | 159 | } |
190 | 160 | ||
191 | finalresult = cadence::implode(std::begin(words), std::end(words), " "); | 161 | finalresult = hatkirby::implode(std::begin(words), std::end(words), " "); |
192 | } else { | 162 | } else { |
193 | finalresult = result; | 163 | finalresult = result; |
194 | } | 164 | } |