1
0
mirror of https://github.com/strawberrymusicplayer/strawberry synced 2024-12-26 16:24:41 +01:00

Add deezer cover provider

This commit is contained in:
Jonas Kvinge 2019-04-14 02:54:40 +02:00
parent 0b460017b8
commit 6dcdf5bf92
8 changed files with 355 additions and 4 deletions

View File

@ -19,7 +19,7 @@ Strawberry is a music player and music collection organizer. It is a fork of Cle
* Advanced audio output and device configuration for bit-perfect playback on Linux
* Edit tags on music files
* Fetch tags from MusicBrainz
* Album cover art from Last.fm, Musicbrainz and Discogs
* Album cover art from Last.fm, Musicbrainz, Discogs and Deezer
* Song lyrics from AudD
* Support for multiple backends
* Audio analyzer

2
dist/debian/control vendored
View File

@ -58,7 +58,7 @@ Description: Audio player and music collection organizer
- Advanced audio output and device configuration for bit-perfect playback on Linux
- Edit tags on music files
- Fetch tags from MusicBrainz
- Album cover art from Lastfm, Musicbrainz and Discogs
- Album cover art from Lastfm, Musicbrainz, Discogs and Deezer
- Song lyrics from AudD
- Support for multiple backends
- Audio analyzer

View File

@ -25,7 +25,7 @@ Features:
.br
- Fetch tags from MusicBrainz
.br
- Album cover art from Lastfm, Musicbrainz and Discogs
- Album cover art from Lastfm, Musicbrainz, Discogs and Deezer
.br
- Song lyrics from AudD
.br

View File

@ -96,7 +96,7 @@ Features:
- Advanced audio output and device configuration for bit-perfect playback on Linux
- Edit tags on music files
- Fetch tags from MusicBrainz
- Album cover art from Last.fm, Musicbrainz and Discogs
- Album cover art from Last.fm, Musicbrainz, Discogs and Deezer
- Song lyrics from AudD
- Support for multiple backends
- Audio analyzer

View File

@ -198,6 +198,7 @@ set(SOURCES
covermanager/lastfmcoverprovider.cpp
covermanager/musicbrainzcoverprovider.cpp
covermanager/discogscoverprovider.cpp
covermanager/deezercoverprovider.cpp
lyrics/lyricsproviders.cpp
lyrics/lyricsprovider.cpp
@ -373,6 +374,7 @@ set(HEADERS
covermanager/lastfmcoverprovider.h
covermanager/musicbrainzcoverprovider.h
covermanager/discogscoverprovider.h
covermanager/deezercoverprovider.h
lyrics/lyricsproviders.h
lyrics/lyricsprovider.h

View File

@ -54,6 +54,7 @@
#include "covermanager/lastfmcoverprovider.h"
#include "covermanager/discogscoverprovider.h"
#include "covermanager/musicbrainzcoverprovider.h"
#include "covermanager/deezercoverprovider.h"
#include "lyrics/lyricsproviders.h"
#include "lyrics/lyricsprovider.h"
@ -105,6 +106,7 @@ class ApplicationImpl {
cover_providers->AddProvider(new LastFmCoverProvider(app));
cover_providers->AddProvider(new DiscogsCoverProvider(app));
cover_providers->AddProvider(new MusicbrainzCoverProvider(app));
cover_providers->AddProvider(new DeezerCoverProvider(app));
return cover_providers;
}),
album_cover_loader_([=]() {

View File

@ -0,0 +1,284 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <algorithm>
#include <functional>
#include <QObject>
#include <QList>
#include <QVariant>
#include <QByteArray>
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonArray>
#include "core/closure.h"
#include "core/network.h"
#include "core/logging.h"
#include "albumcoverfetcher.h"
#include "coverprovider.h"
#include "deezercoverprovider.h"
const char *DeezerCoverProvider::kApiUrl = "https://api.deezer.com";
const int DeezerCoverProvider::kLimit = 10;
DeezerCoverProvider::DeezerCoverProvider(QObject *parent): CoverProvider("Deezer", true, parent), network_(new NetworkAccessManager(this)) {}
bool DeezerCoverProvider::StartSearch(const QString &artist, const QString &album, int id) {
typedef QPair<QString, QString> Param;
typedef QList<Param> Parameters;
typedef QPair<QByteArray, QByteArray> EncodedParam;
typedef QList<EncodedParam> EncodedParamList;
Parameters params = Parameters() << Param("output", "json")
<< Param("q", QString(album + " " + artist))
<< Param("limit", QString::number(kLimit));
QUrlQuery url_query;
for (const Param &param : params) {
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
url_query.addQueryItem(encoded_param.first, encoded_param.second);
}
QUrl url(kApiUrl + QString("/search/album"));
url.setQuery(url_query);
QNetworkRequest req(url);
QNetworkReply *reply = network_->get(req);
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, int)), reply, id);
return true;
}
void DeezerCoverProvider::CancelSearch(int id) {}
QByteArray DeezerCoverProvider::GetReplyData(QNetworkReply *reply) {
QByteArray data;
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
data = reply->readAll();
}
else {
if (reply->error() < 200) {
// This is a network error, there is nothing more to do.
QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
Error(failure_reason);
}
else {
// See if there is Json data containing "error" - then use that instead.
data = reply->readAll();
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
QString failure_reason;
if (error.error == QJsonParseError::NoError && !json_doc.isNull() && !json_doc.isEmpty() && json_doc.isObject()) {
QJsonObject json_obj = json_doc.object();
if (json_obj.contains("error")) {
QJsonValue json_value_error = json_obj["error"];
if (json_value_error.isObject()) {
QJsonObject json_error = json_value_error.toObject();
int code = json_error["code"].toInt();
QString message = json_error["message"].toString();
QString type = json_error["type"].toString();
failure_reason = QString("%1 (%2)").arg(message).arg(code);
}
}
}
if (failure_reason.isEmpty()) failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
Error(failure_reason);
}
return QByteArray();
}
return data;
}
QJsonObject DeezerCoverProvider::ExtractJsonObj(QByteArray &data) {
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
Error("Reply from server missing Json data.", data);
return QJsonObject();
}
if (json_doc.isNull() || json_doc.isEmpty()) {
Error("Received empty Json document.", json_doc);
return QJsonObject();
}
if (!json_doc.isObject()) {
Error("Json document is not an object.", json_doc);
return QJsonObject();
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
Error("Received empty Json object.", json_doc);
return QJsonObject();
}
return json_obj;
}
QJsonValue DeezerCoverProvider::ExtractData(QByteArray &data) {
QJsonObject json_obj = ExtractJsonObj(data);
if (json_obj.isEmpty()) return QJsonObject();
if (json_obj.contains("error")) {
QJsonValue json_value_error = json_obj["error"];
if (!json_value_error.isObject()) {
Error("Error missing object", json_obj);
return QJsonValue();
}
QJsonObject json_error = json_value_error.toObject();
int code = json_error["code"].toInt();
QString message = json_error["message"].toString();
QString type = json_error["type"].toString();
Error(QString("%1 (%2)").arg(message).arg(code));
return QJsonValue();
}
if (!json_obj.contains("data") && !json_obj.contains("DATA")) {
Error("Json reply is missing data.", json_obj);
return QJsonValue();
}
QJsonValue json_data;
if (json_obj.contains("data")) json_data = json_obj["data"];
else json_data = json_obj["DATA"];
return json_data;
}
void DeezerCoverProvider::HandleSearchReply(QNetworkReply *reply, int id) {
reply->deleteLater();
CoverSearchResults results;
QByteArray data = GetReplyData(reply);
if (data.isEmpty()) {
emit SearchFinished(id, results);
return;
}
QJsonValue json_value = ExtractData(data);
if (!json_value.isArray()) {
emit SearchFinished(id, results);
return;
}
QJsonArray json_data = json_value.toArray();
if (json_data.isEmpty()) {
emit SearchFinished(id, results);
return;
}
for (const QJsonValue &value : json_data) {
if (!value.isObject()) {
Error("Invalid Json reply, data is not an object.", value);
continue;
}
QJsonObject json_obj = value.toObject();
if (!json_obj.contains("id") || !json_obj.contains("type")) {
Error("Invalid Json reply, item is missing ID or type.", json_obj);
continue;
}
QString type = json_obj["type"].toString();
if (type != "album") {
Error("Invalid Json reply, incorrect type returned", json_obj);
continue;
}
if (!json_obj.contains("artist")) {
Error("Invalid Json reply, item missing artist.", json_obj);
continue;
}
QJsonValue json_value_artist = json_obj["artist"];
if (!json_value_artist.isObject()) {
Error("Invalid Json reply, item artist is not a object.", json_value_artist);
continue;
}
QJsonObject json_artist = json_value_artist.toObject();
if (!json_artist.contains("name")) {
Error("Invalid Json reply, artist data missing name.", json_artist);
continue;
}
QString artist = json_artist["name"].toString();
if (!json_obj.contains("title")) {
Error("Invalid Json reply, data missing title.", json_obj);
continue;
}
QString album = json_obj["title"].toString();
QString cover;
if (json_obj.contains("cover_xl")) {
cover = json_obj["cover_xl"].toString();
}
else if (json_obj.contains("cover_big")) {
cover = json_obj["cover_big"].toString();
}
else if (json_obj.contains("cover_medium")) {
cover = json_obj["cover_medium"].toString();
}
else if (json_obj.contains("cover_small")) {
cover = json_obj["cover_small"].toString();
}
else {
Error("Invalid Json reply, data missing cover.", json_obj);
continue;
}
QUrl url(cover);
CoverSearchResult cover_result;
cover_result.description = artist + " " + album;
cover_result.image_url = url;
results << cover_result;
}
emit SearchFinished(id, results);
}
void DeezerCoverProvider::Error(QString error, QVariant debug) {
qLog(Error) << "Deezer:" << error;
if (debug.isValid()) qLog(Debug) << debug;
}

View File

@ -0,0 +1,63 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef DEEZERCOVERPROVIDER_H
#define DEEZERCOVERPROVIDER_H
#include "config.h"
#include <stdbool.h>
#include <QObject>
#include <QVariant>
#include <QByteArray>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonArray>
#include "coverprovider.h"
class DeezerCoverProvider : public CoverProvider {
Q_OBJECT
public:
explicit DeezerCoverProvider(QObject *parent = nullptr);
bool StartSearch(const QString &artist, const QString &album, int id);
void CancelSearch(int id);
private slots:
void HandleSearchReply(QNetworkReply *reply, int id);
private:
static const char *kApiUrl;
static const int kLimit;
QByteArray GetReplyData(QNetworkReply *reply);
QJsonObject ExtractJsonObj(QByteArray &data);
QJsonValue ExtractData(QByteArray &data);
void Error(QString error, QVariant debug = QVariant());
QNetworkAccessManager *network_;
};
#endif // DEEZERCOVERPROVIDER_H