about summary refs log tree commit diff stats
path: root/generator/generator.cpp
blob: eb2750c0144ea4583d65286357d63673670d80cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "generator.h"
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <dirent.h>
#include <json.hpp>
#include "progress.h"
#include "field.h"
#include "../util.h"
#include "mood.h"

namespace cadence {
  namespace generator {
    
    generator::generator(
      std::string inputpath,
      std::string outputpath) :
        inputpath_(inputpath),
        db_(outputpath)
    {
      // Add directory separator to input path
      if ((inputpath_.back() != '/') && (inputpath_.back() != '\\'))
      {
        inputpath_ += '/';
      }
      
      inputpath_ += "highlevel/";
    }
    
    void generator::run()
    {
      // Creates the datafile.
      writeSchema();
      
      // Scans the AcousticBrainz data dump and generates a list of all of the
      // files in the dump.
      scanDirectories();
      
      // Parses each data file and enters it into the database.
      parseData();
    }
    
    void generator::writeSchema()
    {
      std::ifstream file("schema.sql");
      if (!file)
      {
        throw std::invalid_argument("Could not find database schema");
      }
  
      std::ostringstream schemaBuilder;
      std::string line;
      while (std::getline(file, line))
      {
        if (line.back() == '\r')
        {
          line.pop_back();
        }
      
        schemaBuilder << line;
      }
      
      std::string schema = schemaBuilder.str();
      auto queries = split<std::list<std::string>>(schema, ";");
      progress ppgs("Writing database schema...", queries.size());
      for (std::string query : queries)
      {
        if (!queries.empty())
        {
          db_.runQuery(query);
        }
        
        ppgs.update();
      }
    }
    
    void generator::scanDirectories()
    {
      std::cout << "Scanning AcousticBrainz dump..." << std::endl;
  
      DIR* topdir;
      if ((topdir = opendir(inputpath_.c_str())) == nullptr)
      {
        throw std::invalid_argument("Invalid AcousticBrainz data directory");
      }
  
      struct dirent* topent;
      while ((topent = readdir(topdir)) != nullptr)
      {
        if (topent->d_name[0] != '.')
        {
          std::string directory = inputpath_ + topent->d_name + "/";
          
          DIR* subdir;
          if ((subdir = opendir(directory.c_str())) == nullptr)
          {
            throw std::invalid_argument("Invalid AcousticBrainz data directory");
          }
        
          struct dirent* subent;
          while ((subent = readdir(subdir)) != nullptr)
          {
            if (subent->d_name[0] != '.')
            {
              std::string subdirectory = directory + subent->d_name + "/";
            
              DIR* subsubdir;
              if ((subsubdir = opendir(subdirectory.c_str())) == nullptr)
              {
                throw std::invalid_argument("Invalid AcousticBrainz data directory");
              }
        
              struct dirent* subsubent;
              while ((subsubent = readdir(subsubdir)) != nullptr)
              {
                if (subsubent->d_name[0] != '.')
                {
                  std::string datafile = subdirectory + subsubent->d_name;
            
                  datafiles_.push_back(datafile);
                }
              }
          
              closedir(subsubdir);
            }
          }
        
          closedir(subdir);
        }
      }
      
      closedir(topdir);
    }
    
    void generator::parseData()
    {
      progress ppgs("Parsing AcousticBrainz data files...", datafiles_.size());
      
      for (std::string datafile : datafiles_)
      {
        ppgs.update();

        nlohmann::json jsonData;
        {
          std::ifstream dataStream(datafile);
          dataStream >> jsonData;
        }
        
        try
        {
          std::vector<mood> moods;
          moods.emplace_back(mood::type::danceable, jsonData["highlevel"]["danceability"]["all"]["danceable"]);
          moods.emplace_back(mood::type::acoustic, jsonData["highlevel"]["mood_acoustic"]["all"]["acoustic"]);
          moods.emplace_back(mood::type::aggressive, jsonData["highlevel"]["mood_aggressive"]["all"]["aggressive"]);
          moods.emplace_back(mood::type::electronic, jsonData["highlevel"]["mood_electronic"]["all"]["electronic"]);
          moods.emplace_back(mood::type::happy, jsonData["highlevel"]["mood_happy"]["all"]["happy"]);
          moods.emplace_back(mood::type::party, jsonData["highlevel"]["mood_party"]["all"]["party"]);
          moods.emplace_back(mood::type::relaxed, jsonData["highlevel"]["mood_relaxed"]["all"]["relaxed"]);
          moods.emplace_back(mood::type::sad, jsonData["highlevel"]["mood_sad"]["all"]["sad"]);
          moods.emplace_back(mood::type::instrumental, jsonData["highlevel"]["voice_instrumental"]["all"]["instrumental"]);
        
          std::sort(std::begin(moods), std::end(moods), [] (const mood& left, const mood& right) -> bool {
            return left.getProbability() > right.getProbability();
          });
        
          std::list<field> fields;
          fields.emplace_back("title", jsonData["metadata"]["tags"]["title"][0].get<std::string>());
          fields.emplace_back("artist", jsonData["metadata"]["tags"]["artist"][0].get<std::string>());
          fields.emplace_back("category", moods.front().getCategory());
        
          db_.insertIntoTable("songs", std::move(fields));
        } catch (const std::domain_error& ex)
        {
          // Weird data. Ignore silently.
        }
      }
    }
    
  };
};