Removed curl dependency and use wt's http client instead (used to fetch data from AcousticBrainz)

This commit is contained in:
emeric
2019-08-08 13:54:24 +02:00
parent 35b9b86df5
commit 8c9da5c0e2
4 changed files with 40 additions and 54 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ The Subsonic API is enabled by default.
## Installation
Here are the required packages to build LMS on Debian Stretch:
```sh
apt-get install g++ autoconf automake libboost-filesystem-dev libboost-system-dev libavcodec-dev libavutil-dev libavformat-dev libav-tools libmagick++-dev libpstreams-dev libconfig++-dev libpstreams-dev ffmpeg libtag1-dev libcurl4-openssl-dev
apt-get install g++ autoconf automake libboost-filesystem-dev libboost-system-dev libavcodec-dev libavutil-dev libavformat-dev libav-tools libmagick++-dev libpstreams-dev libconfig++-dev libpstreams-dev ffmpeg libtag1-dev
```
You also need wt4, that is not packaged yet on Debian. See [installation instructions](https://www.webtoolkit.eu/wt/doc/reference/html/InstallationUnix.html). You may need to build Wt4 in "Release" mode if you want to compile it natively on a Raspberry Pi3B+.
+1 -6
View File
@@ -16,7 +16,7 @@ fi
AC_SUBST(MAGICKXX_CFLAGS)
AC_SUBST(MAGICKXX_LIBS)
AC_CHECK_HEADERS([Wt/WApplication.h pstreams/pstream.h curl/curl.h],
AC_CHECK_HEADERS([Wt/WApplication.h pstreams/pstream.h],
[],
[AC_MSG_ERROR([Header not found or unusable !])])
@@ -81,11 +81,6 @@ AC_CHECK_LIB( [config++],
,
[AC_MSG_ERROR([libconfig++ not found!])])
AC_CHECK_LIB( [curl],
[curl_easy_init],
,
[AC_MSG_ERROR([libcurl not found!])])
AC_CONFIG_FILES([Makefile
src/Makefile
test/Makefile
+30 -39
View File
@@ -21,7 +21,9 @@
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <curl/curl.h>
#include <Wt/WIOService.h>
#include <Wt/Http/Client.h>
#include "main/Service.hpp"
#include "utils/Config.hpp"
@@ -31,63 +33,52 @@
namespace AcousticBrainz
{
static size_t writeToOStringStream(void *buffer, size_t size, size_t nmemb, void* ctx)
{
std::ostringstream& oss = *reinterpret_cast<std::ostringstream*>(ctx);
oss.write(reinterpret_cast<char*>(buffer), size * nmemb);
return size * nmemb;
}
static std::string
getJsonData(const std::string& mbid)
{
static const std::string defaultAPIURL = "https://acousticbrainz.org/api/v1/";
std::string data;
std::string url = getService<Config>()->getString("acousticbrainz-api-url", defaultAPIURL) + mbid + "/low-level";
const std::string url {getService<Config>()->getString("acousticbrainz-api-url", defaultAPIURL) + mbid + "/low-level"};
CURL *curl;
CURLcode res;
boost::asio::io_service ioService;
curl = curl_easy_init();
if (!curl)
Wt::Http::Client client {ioService};
client.setFollowRedirect(true);
client.setSslCertificateVerificationEnabled(true);
client.setMaximumResponseSize(256*1024);
if (!client.get(url))
{
LMS_LOG(SIMILARITY, ERROR) << "CURL init failed";
return data;
LMS_LOG(SIMILARITY, ERROR) << "Cannot perform a GET request to url '" << url << "'";
return {};
}
std::ostringstream oss;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToOStringStream);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
std::string response;
client.done().connect([&](Wt::AsioWrapper::error_code ec, const Wt::Http::Message &msg)
{
LMS_LOG(SIMILARITY, ERROR) << "CURL perform failed: " << curl_easy_strerror(res);
return data;
}
if (ec)
{
LMS_LOG(SIMILARITY, ERROR) << "GET request to url '" << url << "' failed: " << ec.message();
return;
}
curl_easy_cleanup(curl);
if (msg.status() != 200)
{
LMS_LOG(SIMILARITY, ERROR) << "GET request to url '" << url << "' failed: status = " << msg.status() << ", body = " << msg.body();
return;
}
data = std::move(oss.str());
response = msg.body();
});
return data;
ioService.run();
return response;
}
std::string
extractLowLevelFeatures(const std::string& mbid)
{
if (boost::filesystem::exists("/storage/emeric/lms-dev/features/" + mbid))
{
std::ifstream ifs{std::string{"/storage/emeric/lms-dev/features/" + mbid}.c_str()};
return std::string {std::istreambuf_iterator<char>{ifs}, std::istreambuf_iterator<char>{}};
}
return getJsonData(mbid);
}
@@ -160,22 +160,22 @@ FeaturesScannerAddon::fetchFeatures(Database::IdType trackId, const std::string&
std::map<std::string, double> features;
LMS_LOG(DBUPDATER, DEBUG) << "Fetching low level features for track '" << MBID << "'";
std::string data {AcousticBrainz::extractLowLevelFeatures(MBID)};
const std::string data {AcousticBrainz::extractLowLevelFeatures(MBID)};
if (data.empty())
{
LMS_LOG(DBUPDATER, ERROR) << "Track " << trackId << ", MBID = '" << MBID << "': cannot extract features using AcousticBrainz";
return false;
}
auto uniqueTransaction {_dbSession->createUniqueTransaction()};
{
auto uniqueTransaction {_dbSession->createUniqueTransaction()};
Wt::Dbo::ptr<Database::Track> track {Database::Track::getById(*_dbSession, trackId)};
if (!track)
return false;
Wt::Dbo::ptr<Database::Track> track {Database::Track::getById(*_dbSession, trackId)};
if (!track)
return false;
LMS_LOG(DBUPDATER, DEBUG) << "Successfully extracted AcousticBrainz lowlevel features for track '" << track->getPath().string() << "'";
Database::TrackFeatures::create(*_dbSession, track, data);
Database::TrackFeatures::create(*_dbSession, track, data);
}
return true;
}