Bring back album cover fetching from discogs. (#6028)

* Bring back cover fetching from discogs.
This commit is contained in:
Jonas Kvinge 2018-03-19 15:18:56 +01:00 committed by John Maguire
parent 55a39a9609
commit 804168edc7
14 changed files with 451 additions and 12 deletions

View File

@ -107,6 +107,7 @@ set(SOURCES
covers/albumcoverfetchersearch.cpp
covers/albumcoverloader.cpp
covers/amazoncoverprovider.cpp
covers/discogscoverprovider.cpp
covers/coverexportrunnable.cpp
covers/coverprovider.cpp
covers/coverproviders.cpp
@ -430,6 +431,7 @@ set(HEADERS
covers/albumcoverfetchersearch.h
covers/albumcoverloader.h
covers/amazoncoverprovider.h
covers/discogscoverprovider.h
covers/coverexportrunnable.h
covers/coverprovider.h
covers/coverproviders.h

View File

@ -33,6 +33,7 @@
#include "covers/amazoncoverprovider.h"
#include "covers/coverproviders.h"
#include "covers/currentartloader.h"
#include "covers/discogscoverprovider.h"
#include "covers/musicbrainzcoverprovider.h"
#include "devices/devicemanager.h"
#include "globalsearch/globalsearch.h"
@ -43,8 +44,8 @@
#include "internet/podcasts/podcastdeleter.h"
#include "internet/podcasts/podcastdownloader.h"
#include "internet/podcasts/podcastupdater.h"
#include "library/librarybackend.h"
#include "library/library.h"
#include "library/librarybackend.h"
#include "moodbar/moodbarcontroller.h"
#include "moodbar/moodbarloader.h"
#include "networkremote/networkremote.h"
@ -100,6 +101,7 @@ class ApplicationImpl {
// Initialize the repository of cover providers.
cover_providers->AddProvider(new AmazonCoverProvider);
cover_providers->AddProvider(new MusicbrainzCoverProvider);
cover_providers->AddProvider(new DiscogsCoverProvider);
#ifdef HAVE_LIBLASTFM
cover_providers->AddProvider(new LastFmCoverProvider(app));
#endif

View File

@ -40,12 +40,14 @@ AlbumCoverFetcher::AlbumCoverFetcher(CoverProviders* cover_providers,
}
quint64 AlbumCoverFetcher::FetchAlbumCover(const QString& artist,
const QString& album) {
const QString& album,
bool fetchall) {
CoverSearchRequest request;
request.artist = artist;
request.album = album;
request.search = false;
request.id = next_id_++;
request.fetchall = fetchall;
AddRequest(request);
return request.id;
@ -58,6 +60,7 @@ quint64 AlbumCoverFetcher::SearchForCovers(const QString& artist,
request.album = album;
request.search = true;
request.id = next_id_++;
request.fetchall = false;
AddRequest(request);
return request.id;

View File

@ -51,6 +51,9 @@ struct CoverSearchRequest {
// is this only a search request or should we also fetch the first
// cover that's found?
bool search;
// is the request part of fetchall (fetching all missing covers)
bool fetchall;
};
// This structure represents a single result of some album's cover search
@ -88,7 +91,8 @@ class AlbumCoverFetcher : public QObject {
static const int kMaxConcurrentRequests;
quint64 SearchForCovers(const QString& artist, const QString& album);
quint64 FetchAlbumCover(const QString& artist, const QString& album);
quint64 FetchAlbumCover(const QString& artist, const QString& album,
bool fetchall);
void Clear();

View File

@ -63,6 +63,11 @@ void AlbumCoverFetcherSearch::TerminateSearch() {
void AlbumCoverFetcherSearch::Start(CoverProviders* cover_providers) {
for (CoverProvider* provider : cover_providers->List()) {
// Skip provider if it does not have fetchall set, and we are doing fetchall
// - "Fetch Missing Covers".
if (!provider->fetchall() && request_.fetchall) {
continue;
}
connect(provider, SIGNAL(SearchFinished(int, QList<CoverSearchResult>)),
SLOT(ProviderSearchFinished(int, QList<CoverSearchResult>)));
const int id = cover_providers->NextId();

View File

@ -40,7 +40,7 @@ const char* AmazonCoverProvider::kUrl = "http://ecs.amazonaws.com/onca/xml";
const char* AmazonCoverProvider::kAssociateTag = "clemmusiplay-20";
AmazonCoverProvider::AmazonCoverProvider(QObject* parent)
: CoverProvider("Amazon", parent),
: CoverProvider("Amazon", true, parent),
network_(new NetworkAccessManager(this)) {}
bool AmazonCoverProvider::StartSearch(const QString& artist,

View File

@ -19,5 +19,6 @@
#include "coverprovider.h"
CoverProvider::CoverProvider(const QString& name, QObject* parent)
: QObject(parent), name_(name) {}
CoverProvider::CoverProvider(const QString& name, const bool& fetchall,
QObject* parent)
: QObject(parent), name_(name), fetchall_(fetchall) {}

View File

@ -35,10 +35,12 @@ class CoverProvider : public QObject {
Q_OBJECT
public:
explicit CoverProvider(const QString& name, QObject* parent);
explicit CoverProvider(const QString& name, const bool& fetchall,
QObject* parent);
// A name (very short description) of this provider, like "last.fm".
QString name() const { return name_; }
bool fetchall() const { return fetchall_; }
// Starts searching for covers matching the given query text. Returns true
// if the query has been started, or false if an error occurred. The provider
@ -53,6 +55,7 @@ class CoverProvider : public QObject {
private:
QString name_;
bool fetchall_;
};
#endif // COVERS_COVERPROVIDER_H_

View File

@ -0,0 +1,323 @@
/* This file is part of Clementine.
Copyright 2012, Martin Björklund <mbj4668@gmail.com>
Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
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 <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QList>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPair>
#include <QString>
#include <QStringList>
#include <QUrlQuery>
#include <QVariant>
#include "discogscoverprovider.h"
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/utilities.h"
const char* DiscogsCoverProvider::kUrlSearch =
"https://api.discogs.com/database/search";
const char* DiscogsCoverProvider::kUrlReleases =
"https://api.discogs.com/releases";
const char* DiscogsCoverProvider::kAccessKeyB64 =
"YVR4Yk5JTnlmUkhFY0pTaldid2c=";
const char* DiscogsCoverProvider::kSecretKeyB64 =
"QkJNb2tMVXVUVFhSRWRUVmZDc0ZGamZmSWRjdHZRVno=";
DiscogsCoverProvider::DiscogsCoverProvider(QObject* parent)
: CoverProvider("Discogs", false, parent),
network_(new NetworkAccessManager(this)) {}
bool DiscogsCoverProvider::StartSearch(const QString& artist,
const QString& album, int s_id) {
DiscogsCoverSearchContext* s_ctx = new DiscogsCoverSearchContext;
s_ctx->id = s_id;
s_ctx->artist = artist;
s_ctx->album = album;
s_ctx->r_count = 0;
requests_search_.insert(s_id, s_ctx);
SendSearchRequest(s_ctx);
return true;
}
void DiscogsCoverProvider::CancelSearch(int id) {
delete requests_search_.take(id);
}
bool DiscogsCoverProvider::StartRelease(DiscogsCoverSearchContext* s_ctx,
int r_id, QString resource_url) {
DiscogsCoverReleaseContext* r_ctx = new DiscogsCoverReleaseContext;
s_ctx->r_count++;
r_ctx->id = r_id;
r_ctx->resource_url = resource_url;
r_ctx->s_id = s_ctx->id;
requests_release_.insert(r_id, r_ctx);
SendReleaseRequest(s_ctx, r_ctx);
return true;
}
void DiscogsCoverProvider::SendSearchRequest(DiscogsCoverSearchContext* s_ctx) {
typedef QPair<QString, QString> Arg;
typedef QList<Arg> ArgList;
typedef QPair<QByteArray, QByteArray> EncodedArg;
typedef QList<EncodedArg> EncodedArgList;
ArgList args =
ArgList() << Arg("key", QByteArray::fromBase64(kAccessKeyB64))
<< Arg("secret", QByteArray::fromBase64(kSecretKeyB64));
args.append(Arg("type", "release"));
if (!s_ctx->artist.isEmpty()) {
args.append(Arg("artist", s_ctx->artist.toLower()));
}
if (!s_ctx->album.isEmpty()) {
args.append(Arg("release_title", s_ctx->album.toLower()));
}
QUrlQuery url_query;
QUrl url(kUrlSearch);
QStringList query_items;
// Encode the arguments
for (const Arg& arg : args) {
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first),
QUrl::toPercentEncoding(arg.second));
query_items << QString(encoded_arg.first + "=" + encoded_arg.second);
url_query.addQueryItem(encoded_arg.first, encoded_arg.second);
}
// Sign the request
const QByteArray data_to_sign =
QString("GET\n%1\n%2\n%3")
.arg(url.host(), url.path(), query_items.join("&"))
.toUtf8();
const QByteArray signature(Utilities::HmacSha256(
QByteArray::fromBase64(kSecretKeyB64), data_to_sign));
// Add the signature to the request
url_query.addQueryItem("Signature",
QUrl::toPercentEncoding(signature.toBase64()));
url.setQuery(url_query);
QNetworkReply* reply = network_->get(QNetworkRequest(url));
NewClosure(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
SLOT(SearchRequestError(QNetworkReply::NetworkError,
QNetworkReply*, int)),
reply, s_ctx->id);
NewClosure(reply, SIGNAL(finished()), this,
SLOT(HandleSearchReply(QNetworkReply*, int)), reply, s_ctx->id);
}
void DiscogsCoverProvider::SendReleaseRequest(
DiscogsCoverSearchContext* s_ctx, DiscogsCoverReleaseContext* r_ctx) {
typedef QPair<QString, QString> Arg;
typedef QList<Arg> ArgList;
typedef QPair<QByteArray, QByteArray> EncodedArg;
typedef QList<EncodedArg> EncodedArgList;
QUrlQuery url_query;
QStringList query_items;
ArgList args =
ArgList() << Arg("key", QByteArray::fromBase64(kAccessKeyB64))
<< Arg("secret", QByteArray::fromBase64(kSecretKeyB64));
// Encode the arguments
for (const Arg& arg : args) {
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first),
QUrl::toPercentEncoding(arg.second));
query_items << QString(encoded_arg.first + "=" + encoded_arg.second);
url_query.addQueryItem(encoded_arg.first, encoded_arg.second);
}
QUrl url(r_ctx->resource_url);
// Sign the request
const QByteArray data_to_sign =
QString("GET\n%1\n%2\n%3")
.arg(url.host(), url.path(), query_items.join("&"))
.toUtf8();
const QByteArray signature(Utilities::HmacSha256(
QByteArray::fromBase64(kSecretKeyB64), data_to_sign));
// Add the signature to the request
url_query.addQueryItem("Signature",
QUrl::toPercentEncoding(signature.toBase64()));
url.setQuery(url_query);
QNetworkReply* reply = network_->get(QNetworkRequest(url));
NewClosure(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
SLOT(ReleaseRequestError(QNetworkReply::NetworkError,
QNetworkReply*, int, int)),
reply, s_ctx->id, r_ctx->id);
NewClosure(reply, SIGNAL(finished()), this,
SLOT(HandleReleaseReply(QNetworkReply*, int, int)), reply,
s_ctx->id, r_ctx->id);
}
void DiscogsCoverProvider::HandleSearchReply(QNetworkReply* reply, int s_id) {
reply->deleteLater();
if (!requests_search_.contains(s_id)) return;
DiscogsCoverSearchContext* s_ctx = requests_search_.value(s_id);
QJsonDocument json_doc = QJsonDocument::fromJson(reply->readAll());
if ((json_doc.isNull()) || (!json_doc.isObject())) {
qLog(Error) << "Discogs: Failed to create JSON doc.";
EndSearch(s_ctx);
return;
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
qLog(Error) << "Discogs: Failed to create JSON object.";
EndSearch(s_ctx);
return;
}
QVariantMap reply_map = json_obj.toVariantMap();
if (!reply_map.contains("results")) {
EndSearch(s_ctx);
return;
}
QVariantList results = reply_map["results"].toList();
int i = 0;
for (const QVariant& result : results) {
QVariantMap result_map = result.toMap();
if ((result_map.contains("id")) && (result_map.contains("resource_url"))) {
int r_id = result_map["id"].toInt();
QString title = result_map["title"].toString();
QString resource_url = result_map["resource_url"].toString();
if (resource_url.isEmpty()) continue;
StartRelease(s_ctx, r_id, resource_url);
i++;
}
}
if (i <= 0) EndSearch(s_ctx);
}
void DiscogsCoverProvider::HandleReleaseReply(QNetworkReply* reply, int s_id,
int r_id) {
reply->deleteLater();
if (!requests_release_.contains(r_id)) return;
DiscogsCoverReleaseContext* r_ctx = requests_release_.value(r_id);
if (!requests_search_.contains(s_id)) {
EndSearch(r_ctx);
return;
}
DiscogsCoverSearchContext* s_ctx = requests_search_.value(s_id);
QJsonDocument json_doc = QJsonDocument::fromJson(reply->readAll());
if ((json_doc.isNull()) || (!json_doc.isObject())) {
qLog(Error) << "Discogs: Failed to create JSON doc.";
EndSearch(s_ctx, r_ctx);
return;
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
qLog(Error) << "Discogs: JSON object is empty.";
EndSearch(s_ctx, r_ctx);
return;
}
QVariantMap reply_map = json_obj.toVariantMap();
if (!reply_map.contains("images")) {
EndSearch(s_ctx, r_ctx);
return;
}
QVariantList results = reply_map["images"].toList();
for (const QVariant& result : results) {
QVariantMap result_map = result.toMap();
CoverSearchResult cover_result;
cover_result.description = s_ctx->title;
if (result_map.contains("type")) {
QString type = result_map["type"].toString();
if (type != "primary") continue;
}
if (result_map.contains("resource_url")) {
cover_result.image_url = QUrl(result_map["resource_url"].toString());
}
if (cover_result.image_url.isEmpty()) continue;
s_ctx->results.append(cover_result);
}
EndSearch(s_ctx, r_ctx);
}
void DiscogsCoverProvider::SearchRequestError(QNetworkReply::NetworkError error,
QNetworkReply* reply, int s_id) {
if (!requests_search_.contains(s_id)) return;
DiscogsCoverSearchContext* s_ctx = requests_search_.value(s_id);
EndSearch(s_ctx);
}
void DiscogsCoverProvider::ReleaseRequestError(
QNetworkReply::NetworkError error, QNetworkReply* reply, int s_id,
int r_id) {
if (!requests_release_.contains(r_id)) return;
DiscogsCoverReleaseContext* r_ctx = requests_release_.value(r_id);
if (!requests_search_.contains(s_id)) {
EndSearch(r_ctx);
return;
}
DiscogsCoverSearchContext* s_ctx = requests_search_.value(s_id);
EndSearch(s_ctx, r_ctx);
}
void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext* s_ctx,
DiscogsCoverReleaseContext* r_ctx) {
delete requests_release_.take(r_ctx->id);
s_ctx->r_count--;
if (s_ctx->r_count <= 0) EndSearch(s_ctx);
}
void DiscogsCoverProvider::EndSearch(DiscogsCoverSearchContext* s_ctx) {
requests_search_.remove(s_ctx->id);
emit SearchFinished(s_ctx->id, s_ctx->results);
delete s_ctx;
}
void DiscogsCoverProvider::EndSearch(DiscogsCoverReleaseContext* r_ctx) {
delete requests_release_.take(r_ctx->id);
}

View File

@ -0,0 +1,94 @@
/* This file is part of Clementine.
Copyright 2012, Martin Björklund <mbj4668@gmail.com>
Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
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/>.
*/
#ifndef COVERS_DISCOGSCOVERPROVIDER_H
#define COVERS_DISCOGSCOVERPROVIDER_H
#include <QNetworkReply>
#include <QXmlStreamReader>
#include "coverprovider.h"
class QNetworkAccessManager;
// This struct represents a single search request. It identifies and describes
// the request.
struct DiscogsCoverSearchContext {
// the unique request identifier
int id;
// the search query
QString artist;
QString album;
QString title;
int r_count;
CoverSearchResults results;
};
Q_DECLARE_METATYPE(DiscogsCoverSearchContext)
// This struct represents a single release request. It identifies and describes
// the request.
struct DiscogsCoverReleaseContext {
int id; // the unique request identifier
int s_id; // the search request identifier
QString resource_url;
};
Q_DECLARE_METATYPE(DiscogsCoverReleaseContext)
class DiscogsCoverProvider : public CoverProvider {
Q_OBJECT
public:
explicit DiscogsCoverProvider(QObject* parent = nullptr);
bool StartSearch(const QString& artist, const QString& album, int s_id);
void CancelSearch(int id);
private slots:
void SearchRequestError(QNetworkReply::NetworkError error,
QNetworkReply* reply, int s_id);
void ReleaseRequestError(QNetworkReply::NetworkError error,
QNetworkReply* reply, int s_id, int r_id);
void HandleSearchReply(QNetworkReply* reply, int s_id);
void HandleReleaseReply(QNetworkReply* reply, int s_id, int r_id);
private:
static const char* kUrlSearch;
static const char* kUrlReleases;
static const char* kAccessKeyB64;
static const char* kSecretKeyB64;
QNetworkAccessManager* network_;
QHash<int, DiscogsCoverSearchContext*> requests_search_;
QHash<int, DiscogsCoverReleaseContext*> requests_release_;
bool StartRelease(DiscogsCoverSearchContext* s_ctx, int r_id,
QString resource_url);
void SendSearchRequest(DiscogsCoverSearchContext* s_ctx);
void SendReleaseRequest(DiscogsCoverSearchContext* s_ctx,
DiscogsCoverReleaseContext* r_ctx);
void EndSearch(DiscogsCoverSearchContext* s_ctx,
DiscogsCoverReleaseContext* r_ctx);
void EndSearch(DiscogsCoverSearchContext* s_ctx);
void EndSearch(DiscogsCoverReleaseContext* r_ctx);
};
#endif // COVERS_DISCOGSCOVERPROVIDER_H

View File

@ -29,7 +29,7 @@
#include "internet/lastfm/lastfmcompat.h"
LastFmCoverProvider::LastFmCoverProvider(QObject* parent)
: CoverProvider("last.fm", parent) {}
: CoverProvider("last.fm", true, parent) {}
bool LastFmCoverProvider::StartSearch(const QString& artist,
const QString& album, int id) {

View File

@ -37,7 +37,7 @@ static const char* kAlbumCoverUrl =
} // namespace
MusicbrainzCoverProvider::MusicbrainzCoverProvider(QObject* parent)
: CoverProvider("MusicBrainz", parent),
: CoverProvider("MusicBrainz", true, parent),
network_(new NetworkAccessManager(this)) {}
bool MusicbrainzCoverProvider::StartSearch(const QString& artist,

View File

@ -250,7 +250,7 @@ void AlbumCoverChoiceController::ShowCover(const Song& song) {
void AlbumCoverChoiceController::SearchCoverAutomatically(const Song& song) {
qint64 id = cover_fetcher_->FetchAlbumCover(song.effective_albumartist(),
song.effective_album());
song.effective_album(), false);
cover_fetching_tasks_[id] = song;
}

View File

@ -430,7 +430,8 @@ void AlbumCoverManager::FetchAlbumCovers() {
if (ItemHasCover(*item)) continue;
quint64 id = cover_fetcher_->FetchAlbumCover(
EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString());
EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString(),
true);
cover_fetching_tasks_[id] = item;
jobs_++;
}
@ -566,7 +567,8 @@ void AlbumCoverManager::ShowCover() {
void AlbumCoverManager::FetchSingleCover() {
for (QListWidgetItem* item : context_menu_items_) {
quint64 id = cover_fetcher_->FetchAlbumCover(
EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString());
EffectiveAlbumArtistName(*item), item->data(Role_AlbumName).toString(),
false);
cover_fetching_tasks_[id] = item;
jobs_++;
}