From d141e89e7d3c7120ee163a0e01272fabe4cb5501 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 30 Dec 2010 20:57:48 +0100 Subject: Added a simple artist lookup from a database using SOCI. --- SConstruct | 3 +++ config.cpp | 7 ++++++- music.cpp | 37 +++++++++++++++++++++++++++++++------ music.h | 2 ++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/SConstruct b/SConstruct index baf9f5d..c2755c5 100644 --- a/SConstruct +++ b/SConstruct @@ -12,7 +12,10 @@ else: conf.CheckLib('boost_thread-mt') conf.CheckLib('boost_program_options-mt') conf.CheckLib('mp3lame') + conf.CheckLib('soci_core') env = conf.Finish() + # scons' CheckLib doesn't seem to find soci_core + env.Append(LIBS = ['soci_core']) if GetOption('release'): env.Append(CCFLAGS = ['-O2']) diff --git a/config.cpp b/config.cpp index 1fbcde5..9376f10 100644 --- a/config.cpp +++ b/config.cpp @@ -11,12 +11,17 @@ void config::init() { desc.add_options() ("audist.music_root", po::value(), "music root") ("audist.httpd_port", po::value()->default_value(8000), "httpd port") + ("audist.database", po::value(), "database string") ; std::ifstream is("audist.conf", std::ios::in); po::store(po::parse_config_file(is, desc, true), vm); po::notify(vm); if(!vm.count("audist.music_root")) { - throw std::runtime_error("audist.music_root music be specified in audist.conf"); + throw std::runtime_error("audist.music_root must be specified in audist.conf"); + } + + if(!vm.count("audist.database")) { + throw std::runtime_error("audist.database must be specified in audist.conf"); } } diff --git a/music.cpp b/music.cpp index 9b840bb..1651d53 100644 --- a/music.cpp +++ b/music.cpp @@ -2,23 +2,30 @@ #include "decoder.h" #include "encoder.h" #include "transcode.h" +#include "tag.h" +#include "config.h" #include #include #include +#include -namespace music { +fs::path music::root_directory; -fs::path root_directory; - -void init(std::string root) { +void music::init(std::string root) { // remove trailing slash if(boost::algorithm::ends_with(root, "/")) root = root.substr(0, root.size()-1); root_directory = root; + + // find_artist test + std::vector ml = find_artist("a"); + for(std::vector::iterator it = ml.begin(); it != ml.end(); it++) { + std::cout << "result: " << (*it)->path << std::endl; + } } -MusicListing::p get(const std::string path) { +MusicListing::p music::get(const std::string path) { // prefix path with our root_directory fs::path p = root_directory / path; @@ -38,7 +45,21 @@ MusicListing::p get(const std::string path) { return MusicListing::p(); } -}; // namespace music +std::vector music::find_artist(const std::string artist) { + soci::session sql(config::vm["audist.database"].as()); + + soci::rowset rs = (sql.prepare << "SELECT file_name FROM tracks WHERE artist_id IN (SELECT id FROM artists WHERE name ILIKE :name)", + soci::use("%"+artist+"%")); + + std::vector results; + for(soci::rowset::const_iterator it = rs.begin(); it != rs.end(); it++) { + boost::shared_ptr ml(new MusicTrack(*it)); + results.push_back(ml); + } + std::cout << "results size: " << results.size() << std::endl; + + return results; +} void MusicDirectory::render(HTTPRequest& req, HTTPResponse& res) { res.add_header("content-type", "text/html"); @@ -75,6 +96,10 @@ MusicDirectory::MusicDirectory(const fs::path root) { void MusicTrack::render(HTTPRequest& req, HTTPResponse& res) { res.add_header("content-type", "application/octet-stream"); + // tag test + Tag *t = new ID3Tag(path.string()); + delete t; + fs::ifstream is(path, std::ios::binary | std::ios::in); if(req.query.find("decoder") != req.query.end() && req.query.find("encoder") != req.query.end()) { diff --git a/music.h b/music.h index 7f1c813..effc32c 100644 --- a/music.h +++ b/music.h @@ -33,8 +33,10 @@ class MusicDirectory : public MusicListing { }; namespace music { + extern fs::path root_directory; void init(std::string root); MusicListing::p get(const std::string path); + std::vector find_artist(const std::string artist); }; #endif -- cgit v1.2.3