2011-07-23 20:34:54 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
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 "amazoncoverprovider.h"
|
2011-12-12 17:36:51 +01:00
|
|
|
#include "core/closure.h"
|
2011-07-23 20:34:54 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
|
2012-12-31 11:45:26 +01:00
|
|
|
// Amazon has a web crawler that looks for access keys in public source code,
|
|
|
|
// so we apply some sophisticated encryption to these keys.
|
|
|
|
const char* AmazonCoverProvider::kAccessKeyB64 = "QUtJQUlRSDI2UlZNNlVaNFdBNlE=";
|
|
|
|
const char* AmazonCoverProvider::kSecretAccessKeyB64 =
|
|
|
|
"ZTQ2UGczM0JRNytDajd4MWR6eFNvODVFd2tpdi9FbGVCcUZjMkVmMQ==";
|
2011-07-23 20:34:54 +02:00
|
|
|
const char* AmazonCoverProvider::kUrl = "http://ecs.amazonaws.com/onca/xml";
|
2012-12-31 11:45:26 +01:00
|
|
|
const char* AmazonCoverProvider::kAssociateTag = "clemmusiplay-20";
|
2011-07-23 20:34:54 +02:00
|
|
|
|
|
|
|
AmazonCoverProvider::AmazonCoverProvider(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: CoverProvider("Amazon", parent),
|
|
|
|
network_(new NetworkAccessManager(this)) {}
|
2011-07-23 20:34:54 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool AmazonCoverProvider::StartSearch(const QString& artist,
|
|
|
|
const QString& album, int id) {
|
2011-07-23 20:34:54 +02:00
|
|
|
typedef QPair<QString, QString> Arg;
|
|
|
|
typedef QList<Arg> ArgList;
|
|
|
|
|
|
|
|
typedef QPair<QByteArray, QByteArray> EncodedArg;
|
|
|
|
typedef QList<EncodedArg> EncodedArgList;
|
|
|
|
|
|
|
|
// Must be sorted by parameter name
|
2014-02-07 16:34:20 +01:00
|
|
|
ArgList args =
|
|
|
|
ArgList() << Arg("AWSAccessKeyId", QByteArray::fromBase64(kAccessKeyB64))
|
|
|
|
<< Arg("AssociateTag", kAssociateTag)
|
|
|
|
<< Arg("Keywords", artist + " " + album)
|
|
|
|
<< Arg("Operation", "ItemSearch")
|
|
|
|
<< Arg("ResponseGroup", "Images") << Arg("SearchIndex", "All")
|
|
|
|
<< Arg("Service", "AWSECommerceService")
|
|
|
|
<< Arg("Timestamp", QDateTime::currentDateTime().toString(
|
|
|
|
"yyyy-MM-ddThh:mm:ss.zzzZ"))
|
|
|
|
<< Arg("Version", "2009-11-01");
|
2011-07-23 20:34:54 +02:00
|
|
|
|
|
|
|
EncodedArgList encoded_args;
|
|
|
|
QStringList query_items;
|
|
|
|
|
|
|
|
// Encode the arguments
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Arg& arg : args) {
|
2011-07-23 20:34:54 +02:00
|
|
|
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first),
|
|
|
|
QUrl::toPercentEncoding(arg.second));
|
|
|
|
encoded_args << encoded_arg;
|
2012-02-19 17:43:23 +01:00
|
|
|
query_items << QString(encoded_arg.first + "=" + encoded_arg.second);
|
2011-07-23 20:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the request
|
|
|
|
QUrl url(kUrl);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const QByteArray data_to_sign =
|
|
|
|
QString("GET\n%1\n%2\n%3")
|
|
|
|
.arg(url.host(), url.path(), query_items.join("&"))
|
|
|
|
.toAscii();
|
2012-12-31 11:45:26 +01:00
|
|
|
const QByteArray signature(Utilities::HmacSha256(
|
2014-02-07 16:34:20 +01:00
|
|
|
QByteArray::fromBase64(kSecretAccessKeyB64), data_to_sign));
|
2011-07-23 20:34:54 +02:00
|
|
|
|
|
|
|
// Add the signature to the request
|
2014-02-07 16:34:20 +01:00
|
|
|
encoded_args << EncodedArg("Signature",
|
|
|
|
QUrl::toPercentEncoding(signature.toBase64()));
|
2011-07-23 20:34:54 +02:00
|
|
|
url.setEncodedQueryItems(encoded_args);
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(QueryFinished(QNetworkReply*, int)), reply, id);
|
2011-07-23 20:34:54 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-12 17:36:51 +01:00
|
|
|
void AmazonCoverProvider::QueryFinished(QNetworkReply* reply, int id) {
|
2011-07-23 20:34:54 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
CoverSearchResults results;
|
|
|
|
|
|
|
|
QXmlStreamReader reader(reply);
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
reader.readNext();
|
|
|
|
if (reader.tokenType() == QXmlStreamReader::StartElement &&
|
|
|
|
reader.name() == "Item") {
|
|
|
|
ReadItem(&reader, &results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit SearchFinished(id, results);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AmazonCoverProvider::ReadItem(QXmlStreamReader* reader,
|
|
|
|
CoverSearchResults* results) {
|
2011-07-23 20:34:54 +02:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
switch (reader->readNext()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
if (reader->name() == "LargeImage") {
|
|
|
|
ReadLargeImage(reader, results);
|
|
|
|
} else {
|
|
|
|
reader->skipCurrentElement();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2011-07-23 20:34:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AmazonCoverProvider::ReadLargeImage(QXmlStreamReader* reader,
|
|
|
|
CoverSearchResults* results) {
|
2011-07-23 20:34:54 +02:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
switch (reader->readNext()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
if (reader->name() == "URL") {
|
|
|
|
CoverSearchResult result;
|
|
|
|
result.image_url = QUrl(reader->readElementText());
|
|
|
|
results->append(result);
|
|
|
|
} else {
|
|
|
|
reader->skipCurrentElement();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2011-07-23 20:34:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|