2012-10-10 14:45:13 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-12-26 13:34:53 +01:00
|
|
|
Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2012-10-10 14:45:13 +02:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "musicbrainzcoverprovider.h"
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QXmlStreamReader>
|
2012-10-10 14:59:39 +02:00
|
|
|
#include <algorithm>
|
2012-10-25 16:32:19 +02:00
|
|
|
#include <functional>
|
2012-10-10 14:45:13 +02:00
|
|
|
|
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
|
2012-10-25 16:32:19 +02:00
|
|
|
using std::mem_fun;
|
|
|
|
|
2012-10-10 14:45:13 +02:00
|
|
|
namespace {
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
static const char* kReleaseSearchUrl = "https://musicbrainz.org/ws/2/release/";
|
2012-10-10 14:45:13 +02:00
|
|
|
static const char* kAlbumCoverUrl =
|
2014-01-28 12:57:05 +01:00
|
|
|
"https://coverartarchive.org/release/%1/front";
|
2012-10-10 14:45:13 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
MusicbrainzCoverProvider::MusicbrainzCoverProvider(QObject* parent)
|
2018-03-19 15:18:56 +01:00
|
|
|
: CoverProvider("MusicBrainz", true, parent),
|
2014-02-07 16:34:20 +01:00
|
|
|
network_(new NetworkAccessManager(this)) {}
|
2012-10-10 14:45:13 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool MusicbrainzCoverProvider::StartSearch(const QString& artist,
|
|
|
|
const QString& album, int id) {
|
2012-10-10 14:45:13 +02:00
|
|
|
// Find release information.
|
|
|
|
QUrl url(kReleaseSearchUrl);
|
|
|
|
QString query = QString("release:\"%1\" AND artist:\"%2\"")
|
2014-02-07 16:34:20 +01:00
|
|
|
.arg(album.trimmed().replace('"', "\\\""))
|
|
|
|
.arg(artist.trimmed().replace('"', "\\\""));
|
2015-04-11 22:52:31 +02:00
|
|
|
QUrlQuery url_query;
|
|
|
|
url_query.addQueryItem("query", query);
|
|
|
|
url_query.addQueryItem("limit", "5");
|
|
|
|
url.setQuery(url_query);
|
2012-10-10 14:45:13 +02:00
|
|
|
QNetworkRequest request(url);
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(request);
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(ReleaseSearchFinished(QNetworkReply*, int)), reply, id);
|
2012-10-11 12:24:41 +02:00
|
|
|
|
|
|
|
cover_names_[id] = QString("%1 - %2").arg(artist, album);
|
2012-10-10 14:45:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void MusicbrainzCoverProvider::ReleaseSearchFinished(QNetworkReply* reply,
|
|
|
|
int id) {
|
2012-10-10 14:45:13 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
QList<QString> releases;
|
|
|
|
|
|
|
|
QXmlStreamReader reader(reply);
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (type == QXmlStreamReader::StartElement && reader.name() == "release") {
|
2012-10-10 14:45:13 +02:00
|
|
|
QStringRef release_id = reader.attributes().value("id");
|
|
|
|
if (!release_id.isEmpty()) {
|
|
|
|
releases.append(release_id.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& release_id : releases) {
|
2012-10-10 14:45:13 +02:00
|
|
|
QUrl url(QString(kAlbumCoverUrl).arg(release_id));
|
|
|
|
QNetworkReply* reply = network_->head(QNetworkRequest(url));
|
|
|
|
image_checks_.insert(id, reply);
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this, SLOT(ImageCheckFinished(int)),
|
|
|
|
id);
|
2012-10-10 14:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicbrainzCoverProvider::ImageCheckFinished(int id) {
|
|
|
|
QList<QNetworkReply*> replies = image_checks_.values(id);
|
2014-02-07 16:34:20 +01:00
|
|
|
int finished_count = std::count_if(replies.constBegin(), replies.constEnd(),
|
|
|
|
mem_fun(&QNetworkReply::isFinished));
|
2012-10-10 14:45:13 +02:00
|
|
|
if (finished_count == replies.size()) {
|
2012-10-11 12:24:41 +02:00
|
|
|
QString cover_name = cover_names_.take(id);
|
2012-10-10 14:45:13 +02:00
|
|
|
QList<CoverSearchResult> results;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkReply* reply : replies) {
|
2012-10-10 14:45:13 +02:00
|
|
|
reply->deleteLater();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() <
|
|
|
|
400) {
|
2012-10-10 14:45:13 +02:00
|
|
|
CoverSearchResult result;
|
2012-10-11 12:24:41 +02:00
|
|
|
result.description = cover_name;
|
2012-10-10 14:45:13 +02:00
|
|
|
result.image_url = reply->url();
|
|
|
|
results.append(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image_checks_.remove(id);
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicbrainzCoverProvider::CancelSearch(int id) {
|
|
|
|
QList<QNetworkReply*> replies = image_checks_.values(id);
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkReply* reply : replies) {
|
2012-10-10 14:45:13 +02:00
|
|
|
reply->abort();
|
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
image_checks_.remove(id);
|
|
|
|
}
|