diff options
| -rw-r--r-- | cadence.cpp | 54 | ||||
| -rw-r--r-- | generator/database.cpp | 14 | ||||
| -rw-r--r-- | generator/database.h | 2 | ||||
| -rw-r--r-- | generator/field.h | 12 | ||||
| -rw-r--r-- | generator/generator.cpp | 56 | ||||
| -rw-r--r-- | generator/generator.h | 36 | ||||
| -rw-r--r-- | generator/main.cpp | 2 | ||||
| -rw-r--r-- | generator/mood.cpp | 42 | ||||
| -rw-r--r-- | generator/mood.h | 22 | ||||
| -rw-r--r-- | generator/progress.h | 16 | ||||
| -rw-r--r-- | util.h | 24 |
11 files changed, 140 insertions, 140 deletions
| diff --git a/cadence.cpp b/cadence.cpp index c91ca4a..80e4a05 100644 --- a/cadence.cpp +++ b/cadence.cpp | |||
| @@ -24,16 +24,16 @@ int main(int argc, char** argv) | |||
| 24 | 24 | ||
| 25 | std::string configfile(argv[1]); | 25 | std::string configfile(argv[1]); |
| 26 | YAML::Node config = YAML::LoadFile(configfile); | 26 | YAML::Node config = YAML::LoadFile(configfile); |
| 27 | 27 | ||
| 28 | // Set up the Twitter client. | 28 | // Set up the Twitter client. |
| 29 | twitter::auth auth; | 29 | twitter::auth auth; |
| 30 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); | 30 | auth.setConsumerKey(config["consumer_key"].as<std::string>()); |
| 31 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); | 31 | auth.setConsumerSecret(config["consumer_secret"].as<std::string>()); |
| 32 | auth.setAccessKey(config["access_key"].as<std::string>()); | 32 | auth.setAccessKey(config["access_key"].as<std::string>()); |
| 33 | auth.setAccessSecret(config["access_secret"].as<std::string>()); | 33 | auth.setAccessSecret(config["access_secret"].as<std::string>()); |
| 34 | 34 | ||
| 35 | twitter::client client(auth); | 35 | twitter::client client(auth); |
| 36 | 36 | ||
| 37 | // Read in the forms file. | 37 | // Read in the forms file. |
| 38 | std::map<std::string, std::vector<std::string>> groups; | 38 | std::map<std::string, std::vector<std::string>> groups; |
| 39 | std::ifstream datafile(config["forms"].as<std::string>()); | 39 | std::ifstream datafile(config["forms"].as<std::string>()); |
| @@ -42,7 +42,7 @@ int main(int argc, char** argv) | |||
| 42 | std::cout << "Could not find forms file." << std::endl; | 42 | std::cout << "Could not find forms file." << std::endl; |
| 43 | return 1; | 43 | return 1; |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | bool newgroup = true; | 46 | bool newgroup = true; |
| 47 | std::string line; | 47 | std::string line; |
| 48 | std::string curgroup; | 48 | std::string curgroup; |
| @@ -52,7 +52,7 @@ int main(int argc, char** argv) | |||
| 52 | { | 52 | { |
| 53 | line.pop_back(); | 53 | line.pop_back(); |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | if (newgroup) | 56 | if (newgroup) |
| 57 | { | 57 | { |
| 58 | curgroup = line; | 58 | curgroup = line; |
| @@ -70,10 +70,10 @@ int main(int argc, char** argv) | |||
| 70 | // Initialize the random number generator. | 70 | // Initialize the random number generator. |
| 71 | std::random_device random_device; | 71 | std::random_device random_device; |
| 72 | std::mt19937 random_engine{random_device()}; | 72 | std::mt19937 random_engine{random_device()}; |
| 73 | 73 | ||
| 74 | // Connect to the AcousticBrainz data file. | 74 | // Connect to the AcousticBrainz data file. |
| 75 | sqlite3* ppdb = nullptr; | 75 | sqlite3* ppdb = nullptr; |
| 76 | 76 | ||
| 77 | if (sqlite3_open_v2(config["acoustic_datafile"].as<std::string>().c_str(), &ppdb, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) | 77 | if (sqlite3_open_v2(config["acoustic_datafile"].as<std::string>().c_str(), &ppdb, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) |
| 78 | { | 78 | { |
| 79 | // We still have to free the resources allocated. In the event that | 79 | // We still have to free the resources allocated. In the event that |
| @@ -82,19 +82,19 @@ int main(int argc, char** argv) | |||
| 82 | sqlite3_close_v2(ppdb); | 82 | sqlite3_close_v2(ppdb); |
| 83 | 83 | ||
| 84 | std::cout << "Could not open acoustic datafile." << std::endl; | 84 | std::cout << "Could not open acoustic datafile." << std::endl; |
| 85 | 85 | ||
| 86 | return 1; | 86 | return 1; |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | for (;;) | 89 | for (;;) |
| 90 | { | 90 | { |
| 91 | std::cout << "Generating tweet..." << std::endl; | 91 | std::cout << "Generating tweet..." << std::endl; |
| 92 | 92 | ||
| 93 | std::string mood = moods[std::uniform_int_distribution<int>(0, moods.size()-1)(random_engine)]; | 93 | std::string mood = moods[std::uniform_int_distribution<int>(0, moods.size()-1)(random_engine)]; |
| 94 | 94 | ||
| 95 | std::string songTitle; | 95 | std::string songTitle; |
| 96 | std::string songArtist; | 96 | std::string songArtist; |
| 97 | 97 | ||
| 98 | sqlite3_stmt* ppstmt; | 98 | sqlite3_stmt* ppstmt; |
| 99 | std::string query = "SELECT title, artist FROM songs WHERE category = ? ORDER BY RANDOM() LIMIT 1"; | 99 | std::string query = "SELECT title, artist FROM songs WHERE category = ? ORDER BY RANDOM() LIMIT 1"; |
| 100 | 100 | ||
| @@ -102,12 +102,12 @@ int main(int argc, char** argv) | |||
| 102 | { | 102 | { |
| 103 | std::cout << "Error reading from acoustic datafile:" << std::endl; | 103 | std::cout << "Error reading from acoustic datafile:" << std::endl; |
| 104 | std::cout << sqlite3_errmsg(ppdb) << std::endl; | 104 | std::cout << sqlite3_errmsg(ppdb) << std::endl; |
| 105 | 105 | ||
| 106 | return 1; | 106 | return 1; |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | sqlite3_bind_text(ppstmt, 1, mood.c_str(), mood.length(), SQLITE_TRANSIENT); | 109 | sqlite3_bind_text(ppstmt, 1, mood.c_str(), mood.length(), SQLITE_TRANSIENT); |
| 110 | 110 | ||
| 111 | if (sqlite3_step(ppstmt) == SQLITE_ROW) | 111 | if (sqlite3_step(ppstmt) == SQLITE_ROW) |
| 112 | { | 112 | { |
| 113 | songTitle = reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0)); | 113 | songTitle = reinterpret_cast<const char*>(sqlite3_column_blob(ppstmt, 0)); |
| @@ -115,10 +115,10 @@ int main(int argc, char** argv) | |||
| 115 | } else { | 115 | } else { |
| 116 | std::cout << "Error reading from acoustic datafile:" << std::endl; | 116 | std::cout << "Error reading from acoustic datafile:" << std::endl; |
| 117 | std::cout << sqlite3_errmsg(ppdb) << std::endl; | 117 | std::cout << sqlite3_errmsg(ppdb) << std::endl; |
| 118 | 118 | ||
| 119 | return 1; | 119 | return 1; |
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | sqlite3_finalize(ppstmt); | 122 | sqlite3_finalize(ppstmt); |
| 123 | 123 | ||
| 124 | std::string action = "{" + cadence::uppercase(mood) + "}"; | 124 | std::string action = "{" + cadence::uppercase(mood) + "}"; |
| @@ -133,12 +133,12 @@ int main(int argc, char** argv) | |||
| 133 | modifier = token.substr(modloc+1); | 133 | modifier = token.substr(modloc+1); |
| 134 | token = token.substr(0, modloc); | 134 | token = token.substr(0, modloc); |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | std::string canontkn; | 137 | std::string canontkn; |
| 138 | std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) { | 138 | std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) { |
| 139 | return std::toupper(ch); | 139 | return std::toupper(ch); |
| 140 | }); | 140 | }); |
| 141 | 141 | ||
| 142 | std::string result; | 142 | std::string result; |
| 143 | if (canontkn == "\\N") | 143 | if (canontkn == "\\N") |
| 144 | { | 144 | { |
| @@ -157,10 +157,10 @@ int main(int argc, char** argv) | |||
| 157 | } else { | 157 | } else { |
| 158 | std::cout << "Badly formatted forms file:" << std::endl; | 158 | std::cout << "Badly formatted forms file:" << std::endl; |
| 159 | std::cout << "No such form as " + canontkn << std::endl; | 159 | std::cout << "No such form as " + canontkn << std::endl; |
| 160 | 160 | ||
| 161 | return 1; | 161 | return 1; |
| 162 | } | 162 | } |
| 163 | 163 | ||
| 164 | if (modifier == "indefinite") | 164 | if (modifier == "indefinite") |
| 165 | { | 165 | { |
| 166 | if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1]))) | 166 | if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1]))) |
| @@ -173,7 +173,7 @@ int main(int argc, char** argv) | |||
| 173 | result = "a " + result; | 173 | result = "a " + result; |
| 174 | } | 174 | } |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | std::string finalresult; | 177 | std::string finalresult; |
| 178 | if (islower(token[0])) | 178 | if (islower(token[0])) |
| 179 | { | 179 | { |
| @@ -187,21 +187,21 @@ int main(int argc, char** argv) | |||
| 187 | { | 187 | { |
| 188 | word[0] = std::toupper(word[0]); | 188 | word[0] = std::toupper(word[0]); |
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | finalresult = cadence::implode(std::begin(words), std::end(words), " "); | 191 | finalresult = cadence::implode(std::begin(words), std::end(words), " "); |
| 192 | } else { | 192 | } else { |
| 193 | finalresult = result; | 193 | finalresult = result; |
| 194 | } | 194 | } |
| 195 | 195 | ||
| 196 | action.replace(tknloc, action.find("}")-tknloc+1, finalresult); | 196 | action.replace(tknloc, action.find("}")-tknloc+1, finalresult); |
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | if (action.size() <= 140) | 199 | if (action.size() <= 140) |
| 200 | { | 200 | { |
| 201 | try | 201 | try |
| 202 | { | 202 | { |
| 203 | client.updateStatus(action); | 203 | client.updateStatus(action); |
| 204 | 204 | ||
| 205 | std::cout << action << std::endl; | 205 | std::cout << action << std::endl; |
| 206 | } catch (const twitter::twitter_error& e) | 206 | } catch (const twitter::twitter_error& e) |
| 207 | { | 207 | { |
| @@ -209,9 +209,9 @@ int main(int argc, char** argv) | |||
| 209 | } | 209 | } |
| 210 | 210 | ||
| 211 | std::cout << "Waiting..." << std::endl; | 211 | std::cout << "Waiting..." << std::endl; |
| 212 | 212 | ||
| 213 | std::this_thread::sleep_for(std::chrono::hours(1)); | 213 | std::this_thread::sleep_for(std::chrono::hours(1)); |
| 214 | 214 | ||
| 215 | std::cout << std::endl; | 215 | std::cout << std::endl; |
| 216 | } else { | 216 | } else { |
| 217 | std::cout << "Tweet was too long, regenerating." << std::endl; | 217 | std::cout << "Tweet was too long, regenerating." << std::endl; |
| diff --git a/generator/database.cpp b/generator/database.cpp index f326f46..b46a0d1 100644 --- a/generator/database.cpp +++ b/generator/database.cpp | |||
| @@ -73,19 +73,19 @@ namespace cadence { | |||
| 73 | { | 73 | { |
| 74 | sqlite3_close_v2(ppdb_); | 74 | sqlite3_close_v2(ppdb_); |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | void database::runQuery(std::string query) | 77 | void database::runQuery(std::string query) |
| 78 | { | 78 | { |
| 79 | // This can only happen when doing bad things with move semantics. | 79 | // This can only happen when doing bad things with move semantics. |
| 80 | assert(ppdb_ != nullptr); | 80 | assert(ppdb_ != nullptr); |
| 81 | 81 | ||
| 82 | sqlite3_stmt* ppstmt; | 82 | sqlite3_stmt* ppstmt; |
| 83 | 83 | ||
| 84 | if (sqlite3_prepare_v2(ppdb_, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | 84 | if (sqlite3_prepare_v2(ppdb_, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) |
| 85 | { | 85 | { |
| 86 | throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); | 86 | throw sqlite3_error("Error writing to database", sqlite3_errmsg(ppdb_)); |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | int result = sqlite3_step(ppstmt); | 89 | int result = sqlite3_step(ppstmt); |
| 90 | sqlite3_finalize(ppstmt); | 90 | sqlite3_finalize(ppstmt); |
| 91 | 91 | ||
| @@ -99,10 +99,10 @@ namespace cadence { | |||
| 99 | { | 99 | { |
| 100 | // This can only happen when doing bad things with move semantics. | 100 | // This can only happen when doing bad things with move semantics. |
| 101 | assert(ppdb_ != nullptr); | 101 | assert(ppdb_ != nullptr); |
| 102 | 102 | ||
| 103 | // This shouldn't happen. | 103 | // This shouldn't happen. |
| 104 | assert(!fields.empty()); | 104 | assert(!fields.empty()); |
| 105 | 105 | ||
| 106 | std::list<std::string> fieldNames; | 106 | std::list<std::string> fieldNames; |
| 107 | std::list<std::string> qs; | 107 | std::list<std::string> qs; |
| 108 | for (field& f : fields) | 108 | for (field& f : fields) |
| @@ -110,7 +110,7 @@ namespace cadence { | |||
| 110 | fieldNames.push_back(f.getName()); | 110 | fieldNames.push_back(f.getName()); |
| 111 | qs.push_back("?"); | 111 | qs.push_back("?"); |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | std::ostringstream query; | 114 | std::ostringstream query; |
| 115 | query << "INSERT INTO "; | 115 | query << "INSERT INTO "; |
| 116 | query << table; | 116 | query << table; |
| @@ -119,7 +119,7 @@ namespace cadence { | |||
| 119 | query << ") VALUES ("; | 119 | query << ") VALUES ("; |
| 120 | query << implode(std::begin(qs), std::end(qs), ", "); | 120 | query << implode(std::begin(qs), std::end(qs), ", "); |
| 121 | query << ")"; | 121 | query << ")"; |
| 122 | 122 | ||
| 123 | std::string query_str = query.str(); | 123 | std::string query_str = query.str(); |
| 124 | 124 | ||
| 125 | sqlite3_stmt* ppstmt; | 125 | sqlite3_stmt* ppstmt; |
| diff --git a/generator/database.h b/generator/database.h index 7aa8dfa..e4f3ba2 100644 --- a/generator/database.h +++ b/generator/database.h | |||
| @@ -52,7 +52,7 @@ namespace cadence { | |||
| 52 | ~database(); | 52 | ~database(); |
| 53 | 53 | ||
| 54 | // Actions | 54 | // Actions |
| 55 | 55 | ||
| 56 | void runQuery(std::string query); | 56 | void runQuery(std::string query); |
| 57 | 57 | ||
| 58 | void insertIntoTable(std::string table, std::list<field> fields); | 58 | void insertIntoTable(std::string table, std::list<field> fields); |
| diff --git a/generator/field.h b/generator/field.h index 9d6ed9a..836c079 100644 --- a/generator/field.h +++ b/generator/field.h | |||
| @@ -32,27 +32,27 @@ namespace cadence { | |||
| 32 | ~field(); | 32 | ~field(); |
| 33 | 33 | ||
| 34 | // Generic accessors | 34 | // Generic accessors |
| 35 | 35 | ||
| 36 | type getType() const | 36 | type getType() const |
| 37 | { | 37 | { |
| 38 | return type_; | 38 | return type_; |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | std::string getName() const | 41 | std::string getName() const |
| 42 | { | 42 | { |
| 43 | return name_; | 43 | return name_; |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | // Integer | 46 | // Integer |
| 47 | 47 | ||
| 48 | field(std::string name, int arg); | 48 | field(std::string name, int arg); |
| 49 | 49 | ||
| 50 | int getInteger() const; | 50 | int getInteger() const; |
| 51 | 51 | ||
| 52 | // String | 52 | // String |
| 53 | 53 | ||
| 54 | field(std::string name, std::string arg); | 54 | field(std::string name, std::string arg); |
| 55 | 55 | ||
| 56 | std::string getString() const; | 56 | std::string getString() const; |
| 57 | 57 | ||
| 58 | private: | 58 | private: |
| diff --git a/generator/generator.cpp b/generator/generator.cpp index eb2750c..54f5d69 100644 --- a/generator/generator.cpp +++ b/generator/generator.cpp | |||
| @@ -11,7 +11,7 @@ | |||
| 11 | 11 | ||
| 12 | namespace cadence { | 12 | namespace cadence { |
| 13 | namespace generator { | 13 | namespace generator { |
| 14 | 14 | ||
| 15 | generator::generator( | 15 | generator::generator( |
| 16 | std::string inputpath, | 16 | std::string inputpath, |
| 17 | std::string outputpath) : | 17 | std::string outputpath) : |
| @@ -23,23 +23,23 @@ namespace cadence { | |||
| 23 | { | 23 | { |
| 24 | inputpath_ += '/'; | 24 | inputpath_ += '/'; |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | inputpath_ += "highlevel/"; | 27 | inputpath_ += "highlevel/"; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | void generator::run() | 30 | void generator::run() |
| 31 | { | 31 | { |
| 32 | // Creates the datafile. | 32 | // Creates the datafile. |
| 33 | writeSchema(); | 33 | writeSchema(); |
| 34 | 34 | ||
| 35 | // Scans the AcousticBrainz data dump and generates a list of all of the | 35 | // Scans the AcousticBrainz data dump and generates a list of all of the |
| 36 | // files in the dump. | 36 | // files in the dump. |
| 37 | scanDirectories(); | 37 | scanDirectories(); |
| 38 | 38 | ||
| 39 | // Parses each data file and enters it into the database. | 39 | // Parses each data file and enters it into the database. |
| 40 | parseData(); | 40 | parseData(); |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | void generator::writeSchema() | 43 | void generator::writeSchema() |
| 44 | { | 44 | { |
| 45 | std::ifstream file("schema.sql"); | 45 | std::ifstream file("schema.sql"); |
| @@ -47,7 +47,7 @@ namespace cadence { | |||
| 47 | { | 47 | { |
| 48 | throw std::invalid_argument("Could not find database schema"); | 48 | throw std::invalid_argument("Could not find database schema"); |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | std::ostringstream schemaBuilder; | 51 | std::ostringstream schemaBuilder; |
| 52 | std::string line; | 52 | std::string line; |
| 53 | while (std::getline(file, line)) | 53 | while (std::getline(file, line)) |
| @@ -56,10 +56,10 @@ namespace cadence { | |||
| 56 | { | 56 | { |
| 57 | line.pop_back(); | 57 | line.pop_back(); |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | schemaBuilder << line; | 60 | schemaBuilder << line; |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | std::string schema = schemaBuilder.str(); | 63 | std::string schema = schemaBuilder.str(); |
| 64 | auto queries = split<std::list<std::string>>(schema, ";"); | 64 | auto queries = split<std::list<std::string>>(schema, ";"); |
| 65 | progress ppgs("Writing database schema...", queries.size()); | 65 | progress ppgs("Writing database schema...", queries.size()); |
| @@ -69,73 +69,73 @@ namespace cadence { | |||
| 69 | { | 69 | { |
| 70 | db_.runQuery(query); | 70 | db_.runQuery(query); |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | ppgs.update(); | 73 | ppgs.update(); |
| 74 | } | 74 | } |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | void generator::scanDirectories() | 77 | void generator::scanDirectories() |
| 78 | { | 78 | { |
| 79 | std::cout << "Scanning AcousticBrainz dump..." << std::endl; | 79 | std::cout << "Scanning AcousticBrainz dump..." << std::endl; |
| 80 | 80 | ||
| 81 | DIR* topdir; | 81 | DIR* topdir; |
| 82 | if ((topdir = opendir(inputpath_.c_str())) == nullptr) | 82 | if ((topdir = opendir(inputpath_.c_str())) == nullptr) |
| 83 | { | 83 | { |
| 84 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); | 84 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | struct dirent* topent; | 87 | struct dirent* topent; |
| 88 | while ((topent = readdir(topdir)) != nullptr) | 88 | while ((topent = readdir(topdir)) != nullptr) |
| 89 | { | 89 | { |
| 90 | if (topent->d_name[0] != '.') | 90 | if (topent->d_name[0] != '.') |
| 91 | { | 91 | { |
| 92 | std::string directory = inputpath_ + topent->d_name + "/"; | 92 | std::string directory = inputpath_ + topent->d_name + "/"; |
| 93 | 93 | ||
| 94 | DIR* subdir; | 94 | DIR* subdir; |
| 95 | if ((subdir = opendir(directory.c_str())) == nullptr) | 95 | if ((subdir = opendir(directory.c_str())) == nullptr) |
| 96 | { | 96 | { |
| 97 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); | 97 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); |
| 98 | } | 98 | } |
| 99 | 99 | ||
| 100 | struct dirent* subent; | 100 | struct dirent* subent; |
| 101 | while ((subent = readdir(subdir)) != nullptr) | 101 | while ((subent = readdir(subdir)) != nullptr) |
| 102 | { | 102 | { |
| 103 | if (subent->d_name[0] != '.') | 103 | if (subent->d_name[0] != '.') |
| 104 | { | 104 | { |
| 105 | std::string subdirectory = directory + subent->d_name + "/"; | 105 | std::string subdirectory = directory + subent->d_name + "/"; |
| 106 | 106 | ||
| 107 | DIR* subsubdir; | 107 | DIR* subsubdir; |
| 108 | if ((subsubdir = opendir(subdirectory.c_str())) == nullptr) | 108 | if ((subsubdir = opendir(subdirectory.c_str())) == nullptr) |
| 109 | { | 109 | { |
| 110 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); | 110 | throw std::invalid_argument("Invalid AcousticBrainz data directory"); |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | struct dirent* subsubent; | 113 | struct dirent* subsubent; |
| 114 | while ((subsubent = readdir(subsubdir)) != nullptr) | 114 | while ((subsubent = readdir(subsubdir)) != nullptr) |
| 115 | { | 115 | { |
| 116 | if (subsubent->d_name[0] != '.') | 116 | if (subsubent->d_name[0] != '.') |
| 117 | { | 117 | { |
| 118 | std::string datafile = subdirectory + subsubent->d_name; | 118 | std::string datafile = subdirectory + subsubent->d_name; |
| 119 | 119 | ||
| 120 | datafiles_.push_back(datafile); | 120 | datafiles_.push_back(datafile); |
| 121 | } | 121 | } |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | closedir(subsubdir); | 124 | closedir(subsubdir); |
| 125 | } | 125 | } |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | closedir(subdir); | 128 | closedir(subdir); |
| 129 | } | 129 | } |
| 130 | } | 130 | } |
| 131 | 131 | ||
| 132 | closedir(topdir); | 132 | closedir(topdir); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | void generator::parseData() | 135 | void generator::parseData() |
| 136 | { | 136 | { |
| 137 | progress ppgs("Parsing AcousticBrainz data files...", datafiles_.size()); | 137 | progress ppgs("Parsing AcousticBrainz data files...", datafiles_.size()); |
| 138 | 138 | ||
| 139 | for (std::string datafile : datafiles_) | 139 | for (std::string datafile : datafiles_) |
| 140 | { | 140 | { |
| 141 | ppgs.update(); | 141 | ppgs.update(); |
| @@ -145,7 +145,7 @@ namespace cadence { | |||
| 145 | std::ifstream dataStream(datafile); | 145 | std::ifstream dataStream(datafile); |
| 146 | dataStream >> jsonData; | 146 | dataStream >> jsonData; |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | try | 149 | try |
| 150 | { | 150 | { |
| 151 | std::vector<mood> moods; | 151 | std::vector<mood> moods; |
| @@ -158,16 +158,16 @@ namespace cadence { | |||
| 158 | moods.emplace_back(mood::type::relaxed, jsonData["highlevel"]["mood_relaxed"]["all"]["relaxed"]); | 158 | moods.emplace_back(mood::type::relaxed, jsonData["highlevel"]["mood_relaxed"]["all"]["relaxed"]); |
| 159 | moods.emplace_back(mood::type::sad, jsonData["highlevel"]["mood_sad"]["all"]["sad"]); | 159 | moods.emplace_back(mood::type::sad, jsonData["highlevel"]["mood_sad"]["all"]["sad"]); |
| 160 | moods.emplace_back(mood::type::instrumental, jsonData["highlevel"]["voice_instrumental"]["all"]["instrumental"]); | 160 | moods.emplace_back(mood::type::instrumental, jsonData["highlevel"]["voice_instrumental"]["all"]["instrumental"]); |
| 161 | 161 | ||
| 162 | std::sort(std::begin(moods), std::end(moods), [] (const mood& left, const mood& right) -> bool { | 162 | std::sort(std::begin(moods), std::end(moods), [] (const mood& left, const mood& right) -> bool { |
| 163 | return left.getProbability() > right.getProbability(); | 163 | return left.getProbability() > right.getProbability(); |
| 164 | }); | 164 | }); |
| 165 | 165 | ||
| 166 | std::list<field> fields; | 166 | std::list<field> fields; |
| 167 | fields.emplace_back("title", jsonData["metadata"]["tags"]["title"][0].get<std::string>()); | 167 | fields.emplace_back("title", jsonData["metadata"]["tags"]["title"][0].get<std::string>()); |
| 168 | fields.emplace_back("artist", jsonData["metadata"]["tags"]["artist"][0].get<std::string>()); | 168 | fields.emplace_back("artist", jsonData["metadata"]["tags"]["artist"][0].get<std::string>()); |
| 169 | fields.emplace_back("category", moods.front().getCategory()); | 169 | fields.emplace_back("category", moods.front().getCategory()); |
| 170 | 170 | ||
| 171 | db_.insertIntoTable("songs", std::move(fields)); | 171 | db_.insertIntoTable("songs", std::move(fields)); |
| 172 | } catch (const std::domain_error& ex) | 172 | } catch (const std::domain_error& ex) |
| 173 | { | 173 | { |
| @@ -175,6 +175,6 @@ namespace cadence { | |||
| 175 | } | 175 | } |
| 176 | } | 176 | } |
| 177 | } | 177 | } |
| 178 | 178 | ||
| 179 | }; | 179 | }; |
| 180 | }; | 180 | }; |
| diff --git a/generator/generator.h b/generator/generator.h index d911c61..dff8eeb 100644 --- a/generator/generator.h +++ b/generator/generator.h | |||
| @@ -7,44 +7,44 @@ | |||
| 7 | 7 | ||
| 8 | namespace cadence { | 8 | namespace cadence { |
| 9 | namespace generator { | 9 | namespace generator { |
| 10 | 10 | ||
| 11 | class generator { | 11 | class generator { |
| 12 | public: | 12 | public: |
| 13 | 13 | ||
| 14 | // Constructor | 14 | // Constructor |
| 15 | 15 | ||
| 16 | generator( | 16 | generator( |
| 17 | std::string inputpath, | 17 | std::string inputpath, |
| 18 | std::string outputpath); | 18 | std::string outputpath); |
| 19 | 19 | ||
| 20 | // Action | 20 | // Action |
| 21 | 21 | ||
| 22 | void run(); | 22 | void run(); |
| 23 | 23 | ||
| 24 | // Subroutines | 24 | // Subroutines |
| 25 | 25 | ||
| 26 | void writeSchema(); | 26 | void writeSchema(); |
| 27 | 27 | ||
| 28 | void scanDirectories(); | 28 | void scanDirectories(); |
| 29 | 29 | ||
| 30 | void parseData(); | 30 | void parseData(); |
| 31 | 31 | ||
| 32 | private: | 32 | private: |
| 33 | 33 | ||
| 34 | // Input | 34 | // Input |
| 35 | 35 | ||
| 36 | std::string inputpath_; | 36 | std::string inputpath_; |
| 37 | 37 | ||
| 38 | // Output | 38 | // Output |
| 39 | 39 | ||
| 40 | database db_; | 40 | database db_; |
| 41 | 41 | ||
| 42 | // Cache | 42 | // Cache |
| 43 | 43 | ||
| 44 | std::list<std::string> datafiles_; | 44 | std::list<std::string> datafiles_; |
| 45 | 45 | ||
| 46 | }; | 46 | }; |
| 47 | 47 | ||
| 48 | }; | 48 | }; |
| 49 | }; | 49 | }; |
| 50 | 50 | ||
| diff --git a/generator/main.cpp b/generator/main.cpp index 5a3d2dd..318770e 100644 --- a/generator/main.cpp +++ b/generator/main.cpp | |||
| @@ -16,7 +16,7 @@ int main(int argc, char** argv) | |||
| 16 | try | 16 | try |
| 17 | { | 17 | { |
| 18 | cadence::generator::generator app(argv[1], argv[2]); | 18 | cadence::generator::generator app(argv[1], argv[2]); |
| 19 | 19 | ||
| 20 | try | 20 | try |
| 21 | { | 21 | { |
| 22 | app.run(); | 22 | app.run(); |
| diff --git a/generator/mood.cpp b/generator/mood.cpp index 9f53dce..b74211c 100644 --- a/generator/mood.cpp +++ b/generator/mood.cpp | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | namespace cadence { | 3 | namespace cadence { |
| 4 | namespace generator { | 4 | namespace generator { |
| 5 | 5 | ||
| 6 | // The categories are: | 6 | // The categories are: |
| 7 | // - party (+danceable, -acoustic, +electronic, +party) = ~.21 | 7 | // - party (+danceable, -acoustic, +electronic, +party) = ~.21 |
| 8 | // - chill (-danceable, +acoustic, -aggressive, -electronic, -party, +relaxed) = ~.49 | 8 | // - chill (-danceable, +acoustic, -aggressive, -electronic, -party, +relaxed) = ~.49 |
| @@ -11,7 +11,7 @@ namespace cadence { | |||
| 11 | // - sad (-happy, +sad) = ~.02 | 11 | // - sad (-happy, +sad) = ~.02 |
| 12 | // - instrumental (+instrumental) = ~.12 | 12 | // - instrumental (+instrumental) = ~.12 |
| 13 | // - vocal (-instrumental) = ~.10 | 13 | // - vocal (-instrumental) = ~.10 |
| 14 | 14 | ||
| 15 | mood::mood(type t, double prob) : type_(t) | 15 | mood::mood(type t, double prob) : type_(t) |
| 16 | { | 16 | { |
| 17 | if (prob >= 0.5) | 17 | if (prob >= 0.5) |
| @@ -22,73 +22,73 @@ namespace cadence { | |||
| 22 | probability_ = 1.0 - prob; | 22 | probability_ = 1.0 - prob; |
| 23 | positive_ = false; | 23 | positive_ = false; |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | switch (t) | 26 | switch (t) |
| 27 | { | 27 | { |
| 28 | case type::danceable: | 28 | case type::danceable: |
| 29 | { | 29 | { |
| 30 | category_ = (positive_ ? "party" : "chill"); | 30 | category_ = (positive_ ? "party" : "chill"); |
| 31 | 31 | ||
| 32 | break; | 32 | break; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | case type::acoustic: | 35 | case type::acoustic: |
| 36 | { | 36 | { |
| 37 | category_ = (positive_ ? "chill" : "party"); | 37 | category_ = (positive_ ? "chill" : "party"); |
| 38 | 38 | ||
| 39 | break; | 39 | break; |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | case type::aggressive: | 42 | case type::aggressive: |
| 43 | { | 43 | { |
| 44 | category_ = (positive_ ? "crazy" : "chill"); | 44 | category_ = (positive_ ? "crazy" : "chill"); |
| 45 | 45 | ||
| 46 | break; | 46 | break; |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | case type::electronic: | 49 | case type::electronic: |
| 50 | { | 50 | { |
| 51 | category_ = (positive_ ? "party" : "chill"); | 51 | category_ = (positive_ ? "party" : "chill"); |
| 52 | 52 | ||
| 53 | break; | 53 | break; |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | case type::happy: | 56 | case type::happy: |
| 57 | { | 57 | { |
| 58 | category_ = (positive_ ? "happy" : "sad"); | 58 | category_ = (positive_ ? "happy" : "sad"); |
| 59 | 59 | ||
| 60 | break; | 60 | break; |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | case type::party: | 63 | case type::party: |
| 64 | { | 64 | { |
| 65 | category_ = (positive_ ? "party" : "chill"); | 65 | category_ = (positive_ ? "party" : "chill"); |
| 66 | 66 | ||
| 67 | break; | 67 | break; |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | case type::relaxed: | 70 | case type::relaxed: |
| 71 | { | 71 | { |
| 72 | category_ = (positive_ ? "chill" : "crazy"); | 72 | category_ = (positive_ ? "chill" : "crazy"); |
| 73 | 73 | ||
| 74 | break; | 74 | break; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | case type::sad: | 77 | case type::sad: |
| 78 | { | 78 | { |
| 79 | category_ = (positive_ ? "sad" : "happy"); | 79 | category_ = (positive_ ? "sad" : "happy"); |
| 80 | 80 | ||
| 81 | break; | 81 | break; |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | case type::instrumental: | 84 | case type::instrumental: |
| 85 | { | 85 | { |
| 86 | category_ = (positive_ ? "instrumental" : "vocal"); | 86 | category_ = (positive_ ? "instrumental" : "vocal"); |
| 87 | 87 | ||
| 88 | break; | 88 | break; |
| 89 | } | 89 | } |
| 90 | } | 90 | } |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | }; | 93 | }; |
| 94 | }; | 94 | }; |
| diff --git a/generator/mood.h b/generator/mood.h index df5d28f..c36e5ee 100644 --- a/generator/mood.h +++ b/generator/mood.h | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | namespace cadence { | 6 | namespace cadence { |
| 7 | namespace generator { | 7 | namespace generator { |
| 8 | 8 | ||
| 9 | class mood { | 9 | class mood { |
| 10 | public: | 10 | public: |
| 11 | enum class type { | 11 | enum class type { |
| @@ -19,41 +19,41 @@ namespace cadence { | |||
| 19 | sad, | 19 | sad, |
| 20 | instrumental | 20 | instrumental |
| 21 | }; | 21 | }; |
| 22 | 22 | ||
| 23 | // Constructor | 23 | // Constructor |
| 24 | 24 | ||
| 25 | mood(type t, double prob); | 25 | mood(type t, double prob); |
| 26 | 26 | ||
| 27 | // Accessors | 27 | // Accessors |
| 28 | 28 | ||
| 29 | type getType() const | 29 | type getType() const |
| 30 | { | 30 | { |
| 31 | return type_; | 31 | return type_; |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | double getProbability() const | 34 | double getProbability() const |
| 35 | { | 35 | { |
| 36 | return probability_; | 36 | return probability_; |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | bool getPositive() const | 39 | bool getPositive() const |
| 40 | { | 40 | { |
| 41 | return positive_; | 41 | return positive_; |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | std::string getCategory() const | 44 | std::string getCategory() const |
| 45 | { | 45 | { |
| 46 | return category_; | 46 | return category_; |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | private: | 49 | private: |
| 50 | type type_; | 50 | type type_; |
| 51 | double probability_; | 51 | double probability_; |
| 52 | bool positive_; | 52 | bool positive_; |
| 53 | std::string category_; | 53 | std::string category_; |
| 54 | 54 | ||
| 55 | }; | 55 | }; |
| 56 | 56 | ||
| 57 | }; | 57 | }; |
| 58 | }; | 58 | }; |
| 59 | 59 | ||
| diff --git a/generator/progress.h b/generator/progress.h index f42f701..e5cc13d 100644 --- a/generator/progress.h +++ b/generator/progress.h | |||
| @@ -5,20 +5,20 @@ | |||
| 5 | 5 | ||
| 6 | namespace cadence { | 6 | namespace cadence { |
| 7 | namespace generator { | 7 | namespace generator { |
| 8 | 8 | ||
| 9 | class progress { | 9 | class progress { |
| 10 | private: | 10 | private: |
| 11 | std::string message; | 11 | std::string message; |
| 12 | int total; | 12 | int total; |
| 13 | int cur = 0; | 13 | int cur = 0; |
| 14 | int lprint = 0; | 14 | int lprint = 0; |
| 15 | 15 | ||
| 16 | public: | 16 | public: |
| 17 | progress(std::string message, int total) : message(message), total(total) | 17 | progress(std::string message, int total) : message(message), total(total) |
| 18 | { | 18 | { |
| 19 | std::cout << message << " 0%" << std::flush; | 19 | std::cout << message << " 0%" << std::flush; |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | void update(int val) | 22 | void update(int val) |
| 23 | { | 23 | { |
| 24 | if (val <= total) | 24 | if (val <= total) |
| @@ -27,29 +27,29 @@ namespace cadence { | |||
| 27 | } else { | 27 | } else { |
| 28 | cur = total; | 28 | cur = total; |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | int pp = cur * 100 / total; | 31 | int pp = cur * 100 / total; |
| 32 | if (pp != lprint) | 32 | if (pp != lprint) |
| 33 | { | 33 | { |
| 34 | lprint = pp; | 34 | lprint = pp; |
| 35 | 35 | ||
| 36 | std::cout << "\b\b\b\b" << std::right; | 36 | std::cout << "\b\b\b\b" << std::right; |
| 37 | std::cout.width(3); | 37 | std::cout.width(3); |
| 38 | std::cout << pp << "%" << std::flush; | 38 | std::cout << pp << "%" << std::flush; |
| 39 | } | 39 | } |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | void update() | 42 | void update() |
| 43 | { | 43 | { |
| 44 | update(cur+1); | 44 | update(cur+1); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | ~progress() | 47 | ~progress() |
| 48 | { | 48 | { |
| 49 | std::cout << "\b\b\b\b100%" << std::endl; | 49 | std::cout << "\b\b\b\b100%" << std::endl; |
| 50 | } | 50 | } |
| 51 | }; | 51 | }; |
| 52 | 52 | ||
| 53 | }; | 53 | }; |
| 54 | }; | 54 | }; |
| 55 | 55 | ||
| diff --git a/util.h b/util.h index 8a476da..5d16649 100644 --- a/util.h +++ b/util.h | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | #include <sstream> | 8 | #include <sstream> |
| 9 | 9 | ||
| 10 | namespace cadence { | 10 | namespace cadence { |
| 11 | 11 | ||
| 12 | inline std::string uppercase(std::string in) | 12 | inline std::string uppercase(std::string in) |
| 13 | { | 13 | { |
| 14 | std::string result; | 14 | std::string result; |
| @@ -16,28 +16,28 @@ namespace cadence { | |||
| 16 | { | 16 | { |
| 17 | return std::toupper(ch); | 17 | return std::toupper(ch); |
| 18 | }); | 18 | }); |
| 19 | 19 | ||
| 20 | return result; | 20 | return result; |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | template <class InputIterator> | 23 | template <class InputIterator> |
| 24 | std::string implode(InputIterator first, InputIterator last, std::string delimiter) | 24 | std::string implode(InputIterator first, InputIterator last, std::string delimiter) |
| 25 | { | 25 | { |
| 26 | std::stringstream result; | 26 | std::stringstream result; |
| 27 | 27 | ||
| 28 | for (InputIterator it = first; it != last; it++) | 28 | for (InputIterator it = first; it != last; it++) |
| 29 | { | 29 | { |
| 30 | if (it != first) | 30 | if (it != first) |
| 31 | { | 31 | { |
| 32 | result << delimiter; | 32 | result << delimiter; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | result << *it; | 35 | result << *it; |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | return result.str(); | 38 | return result.str(); |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | template <class OutputIterator> | 41 | template <class OutputIterator> |
| 42 | void split(std::string input, std::string delimiter, OutputIterator out) | 42 | void split(std::string input, std::string delimiter, OutputIterator out) |
| 43 | { | 43 | { |
| @@ -48,12 +48,12 @@ namespace cadence { | |||
| 48 | { | 48 | { |
| 49 | *out = input; | 49 | *out = input; |
| 50 | out++; | 50 | out++; |
| 51 | 51 | ||
| 52 | input = ""; | 52 | input = ""; |
| 53 | } else { | 53 | } else { |
| 54 | *out = input.substr(0, divider); | 54 | *out = input.substr(0, divider); |
| 55 | out++; | 55 | out++; |
| 56 | 56 | ||
| 57 | input = input.substr(divider+delimiter.length()); | 57 | input = input.substr(divider+delimiter.length()); |
| 58 | } | 58 | } |
| 59 | } | 59 | } |
| @@ -63,12 +63,12 @@ namespace cadence { | |||
| 63 | Container split(std::string input, std::string delimiter) | 63 | Container split(std::string input, std::string delimiter) |
| 64 | { | 64 | { |
| 65 | Container result; | 65 | Container result; |
| 66 | 66 | ||
| 67 | split(input, delimiter, std::back_inserter(result)); | 67 | split(input, delimiter, std::back_inserter(result)); |
| 68 | 68 | ||
| 69 | return result; | 69 | return result; |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | }; | 72 | }; |
| 73 | 73 | ||
| 74 | #endif /* end of include guard: UTIL_H_CED7A66D */ | 74 | #endif /* end of include guard: UTIL_H_CED7A66D */ |
