Add new last.fm album cover provider
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* 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
|
||||
@@ -21,39 +20,70 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QMapIterator>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QStringBuilder>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkReply>
|
||||
#include <QCryptographicHash>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include "core/closure.h"
|
||||
#include "core/network.h"
|
||||
#include "core/logging.h"
|
||||
|
||||
#include "coverprovider.h"
|
||||
#include "albumcoverfetcher.h"
|
||||
#include "lastfmcompat.h"
|
||||
#include "lastfmcoverprovider.h"
|
||||
|
||||
const char *LastFmCoverProvider::kUrl = "https://ws.audioscrobbler.com/2.0/";
|
||||
const char *LastFmCoverProvider::kApiKey = "211990b4c96782c05d1536e7219eb56e";
|
||||
const char *LastFmCoverProvider::kSecret = "80fd738f49596e9709b1bf9319c444a8";
|
||||
|
||||
LastFmCoverProvider::LastFmCoverProvider(QObject *parent) : CoverProvider("last.fm", true, parent), network_(new NetworkAccessManager(this)) {
|
||||
lastfm::ws::ApiKey = kApiKey;
|
||||
lastfm::ws::SharedSecret = kSecret;
|
||||
lastfm::setNetworkAccessManager(network_);
|
||||
}
|
||||
LastFmCoverProvider::LastFmCoverProvider(QObject *parent) : CoverProvider("last.fm", true, parent), network_(new NetworkAccessManager(this)) {}
|
||||
|
||||
bool LastFmCoverProvider::StartSearch(const QString &artist, const QString &album, int id) {
|
||||
|
||||
QMap<QString, QString> params;
|
||||
params["method"] = "album.search";
|
||||
params["album"] = album + " " + artist;
|
||||
typedef QPair<QString, QString> Param;
|
||||
typedef QPair<QByteArray, QByteArray> EncodedParam;
|
||||
typedef QList<Param> ParamList;
|
||||
|
||||
QNetworkReply *reply = lastfm::ws::post(params);
|
||||
ParamList params = ParamList()
|
||||
<< Param("album", QString(album + " " + artist))
|
||||
<< Param("api_key", kApiKey)
|
||||
<< Param("lang", QLocale().name().left(2).toLower())
|
||||
<< Param("method", "album.search");
|
||||
|
||||
QUrlQuery url_query;
|
||||
QString data_to_sign;
|
||||
for (const Param ¶m : params) {
|
||||
EncodedParam encoded_param(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
||||
url_query.addQueryItem(encoded_param.first, encoded_param.second);
|
||||
data_to_sign += param.first + param.second;
|
||||
}
|
||||
data_to_sign += kSecret;
|
||||
|
||||
QByteArray const digest = QCryptographicHash::hash(data_to_sign.toUtf8(), QCryptographicHash::Md5);
|
||||
QString signature = QString::fromLatin1(digest.toHex()).rightJustified(32, '0').toLower();
|
||||
|
||||
url_query.addQueryItem(QUrl::toPercentEncoding("api_sig"), QUrl::toPercentEncoding(signature));
|
||||
url_query.addQueryItem(QUrl::toPercentEncoding("format"), QUrl::toPercentEncoding("json"));
|
||||
|
||||
QUrl url(kUrl);
|
||||
QNetworkRequest req(url);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
QNetworkReply *reply = network_->post(req, url_query.toString(QUrl::FullyEncoded).toUtf8());
|
||||
NewClosure(reply, SIGNAL(finished()), this, SLOT(QueryFinished(QNetworkReply*, int)), reply, id);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void LastFmCoverProvider::QueryFinished(QNetworkReply *reply, int id) {
|
||||
@@ -62,22 +92,207 @@ void LastFmCoverProvider::QueryFinished(QNetworkReply *reply, int id) {
|
||||
|
||||
CoverSearchResults results;
|
||||
|
||||
lastfm::XmlQuery query(lastfm::compat::EmptyXmlQuery());
|
||||
if (lastfm::compat::ParseQuery(reply->readAll(), &query)) {
|
||||
// parse the list of search results
|
||||
QList<lastfm::XmlQuery> elements = query["results"]["albummatches"].children("album");
|
||||
QByteArray data = GetReplyData(reply);
|
||||
if (data.isEmpty()) {
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const lastfm::XmlQuery &element : elements) {
|
||||
CoverSearchResult result;
|
||||
result.description = element["artist"].text() + " - " + element["name"].text();
|
||||
result.image_url = QUrl(element["image size=extralarge"].text());
|
||||
if (result.image_url.isEmpty()) continue;
|
||||
results << result;
|
||||
QJsonValue json_value = ExtractResults(data);
|
||||
if (!json_value.isObject()) {
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_results = json_value.toObject();
|
||||
if (json_results.isEmpty()) {
|
||||
Error("Json object is empty.", json_value);
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!json_results.contains("albummatches")) {
|
||||
Error("Json results is missing albummatches.", json_results);
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonValue json_matches = json_results["albummatches"];
|
||||
if (!json_matches.isObject()) {
|
||||
Error("Json albummatches is not an object.", json_matches);
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject json_obj_matches = json_matches.toObject();
|
||||
if (json_obj_matches.isEmpty()) {
|
||||
Error("Json albummatches object is empty.", json_matches);
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
if (!json_obj_matches.contains("album")) {
|
||||
Error("Json albummatches is missing album.", json_obj_matches);
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonValue json_album = json_obj_matches["album"];
|
||||
if (!json_album.isArray()) {
|
||||
Error("Json album is not an array.", json_album);
|
||||
emit SearchFinished(id, results);
|
||||
return;
|
||||
}
|
||||
QJsonArray json_array = json_album.toArray();
|
||||
|
||||
for (const QJsonValue &value : json_array) {
|
||||
if (!value.isObject()) {
|
||||
Error("Invalid Json reply, album value is not an object.", value);
|
||||
continue;
|
||||
}
|
||||
QJsonObject json_obj = value.toObject();
|
||||
if (!json_obj.contains("artist") || !json_obj.contains("image") || !json_obj.contains("name")) {
|
||||
Error("Invalid Json reply, album is missing artist, image or name.", json_obj);
|
||||
continue;
|
||||
}
|
||||
QString artist = json_obj["artist"].toString();
|
||||
QString name = json_obj["name"].toString();
|
||||
|
||||
QJsonValue json_image = json_obj["image"];
|
||||
if (!json_image.isArray()) {
|
||||
Error("Invalid Json reply, album image is not an array.", json_image);
|
||||
continue;
|
||||
}
|
||||
QJsonArray json_array_image = json_image.toArray();
|
||||
QUrl url;
|
||||
LastFmImageSize size;
|
||||
for (QJsonValue json_value_image : json_array_image) {
|
||||
if (!json_value_image.isObject()) {
|
||||
Error("Invalid Json reply, album image value is not an object.", json_value_image);
|
||||
continue;
|
||||
}
|
||||
QJsonObject json_object_image = json_value_image.toObject();
|
||||
if (!json_object_image.contains("#text") || !json_object_image.contains("size")) {
|
||||
Error("Invalid Json reply, album image value is missing #text or size.", json_object_image);
|
||||
continue;
|
||||
}
|
||||
QString image_url = json_object_image["#text"].toString();
|
||||
LastFmImageSize image_size = ImageSizeFromString(json_object_image["size"].toString().toLower());
|
||||
if (url.isEmpty() || image_size > size) {
|
||||
url.setUrl(image_url);
|
||||
size = image_size;
|
||||
}
|
||||
}
|
||||
|
||||
if (url.isEmpty()) continue;
|
||||
|
||||
CoverSearchResult cover_result;
|
||||
cover_result.description = artist + " " + name;
|
||||
cover_result.image_url = url;
|
||||
results << cover_result;
|
||||
}
|
||||
emit SearchFinished(id, results);
|
||||
|
||||
}
|
||||
|
||||
QByteArray LastFmCoverProvider::GetReplyData(QNetworkReply *reply) {
|
||||
|
||||
QByteArray data;
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
|
||||
data = reply->readAll();
|
||||
}
|
||||
else {
|
||||
// Drop through and emit an empty list of results.
|
||||
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" and "message" - 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") && json_obj.contains("message")) {
|
||||
int error = json_obj["error"].toInt();
|
||||
QString message = json_obj["message"].toString();
|
||||
failure_reason = "Error: " + QString::number(error) + ": " + message;
|
||||
}
|
||||
else {
|
||||
failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
}
|
||||
else {
|
||||
failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
||||
}
|
||||
Error(failure_reason);
|
||||
}
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
emit SearchFinished(id, results);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
QJsonObject LastFmCoverProvider::ExtractJsonObj(const 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 LastFmCoverProvider::ExtractResults(const QByteArray &data) {
|
||||
|
||||
QJsonObject json_obj = ExtractJsonObj(data);
|
||||
if (json_obj.isEmpty()) return QJsonObject();
|
||||
|
||||
if (json_obj.contains("results")) {
|
||||
QJsonValue json_results = json_obj["results"];
|
||||
return json_results;
|
||||
}
|
||||
else if (json_obj.contains("error") && json_obj.contains("message")) {
|
||||
int error = json_obj["error"].toInt();
|
||||
QString message = json_obj["message"].toString();
|
||||
Error(QString("Error: %1: %2").arg(QString::number(error)).arg(message));
|
||||
}
|
||||
else {
|
||||
Error(QString("Json reply is missing results."), json_obj);
|
||||
}
|
||||
return QJsonValue();
|
||||
|
||||
}
|
||||
|
||||
void LastFmCoverProvider::Error(QString error, QVariant debug) {
|
||||
qLog(Error) << "LastFm:" << error;
|
||||
if (debug.isValid()) qLog(Debug) << debug;
|
||||
}
|
||||
|
||||
LastFmCoverProvider::LastFmImageSize LastFmCoverProvider::ImageSizeFromString(const QString size) {
|
||||
if (size == "small") return LastFmImageSize::Small;
|
||||
else if (size == "medium") return LastFmImageSize::Medium;
|
||||
else if (size == "large") return LastFmImageSize::Large;
|
||||
else if (size == "extralarge") return LastFmImageSize::ExtraLarge;
|
||||
else return LastFmImageSize::Unknown;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user