diff options
Diffstat (limited to 'generator/generator.cpp')
-rw-r--r-- | generator/generator.cpp | 56 |
1 files changed, 28 insertions, 28 deletions
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 | }; |