2011-03-12 22:19:41 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
2012-01-06 17:51:27 +01:00
|
|
|
#include "acoustidclient.h"
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2012-01-06 17:31:29 +01:00
|
|
|
#include <qjson/parser.h>
|
|
|
|
|
2012-10-12 14:25:04 +02:00
|
|
|
#include "core/closure.h"
|
2012-01-06 17:31:29 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
#include "core/timeconstants.h"
|
|
|
|
|
2012-01-06 17:51:27 +01:00
|
|
|
const char* AcoustidClient::kClientId = "qsZGpeLx";
|
|
|
|
const char* AcoustidClient::kUrl = "http://api.acoustid.org/v2/lookup";
|
2014-02-07 16:34:20 +01:00
|
|
|
const int AcoustidClient::kDefaultTimeout = 5000; // msec
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2012-01-06 17:51:27 +01:00
|
|
|
AcoustidClient::AcoustidClient(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent),
|
|
|
|
network_(new NetworkAccessManager(this)),
|
|
|
|
timeouts_(new NetworkTimeouts(kDefaultTimeout, this)) {}
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AcoustidClient::SetTimeout(int msec) { timeouts_->SetTimeout(msec); }
|
2011-03-13 14:17:35 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AcoustidClient::Start(int id, const QString& fingerprint,
|
|
|
|
int duration_msec) {
|
2011-03-12 22:19:41 +01:00
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
|
|
|
|
QList<Param> parameters;
|
2014-02-07 16:34:20 +01:00
|
|
|
parameters << Param("format", "json") << Param("client", kClientId)
|
2012-01-06 17:31:29 +01:00
|
|
|
<< Param("duration", QString::number(duration_msec / kMsecPerSec))
|
|
|
|
<< Param("meta", "recordingids")
|
|
|
|
<< Param("fingerprint", fingerprint);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
QUrl url(kUrl);
|
|
|
|
url.setQueryItems(parameters);
|
|
|
|
QNetworkRequest req(url);
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(req);
|
2012-10-12 14:25:04 +02:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(RequestFinished(QNetworkReply*, int)), reply, id);
|
|
|
|
requests_[id] = reply;
|
2011-03-13 14:17:35 +01:00
|
|
|
|
|
|
|
timeouts_->AddReply(reply);
|
2011-03-12 22:19:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AcoustidClient::Cancel(int id) { delete requests_.take(id); }
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2012-01-06 17:51:27 +01:00
|
|
|
void AcoustidClient::CancelAll() {
|
2012-10-12 14:25:04 +02:00
|
|
|
qDeleteAll(requests_.values());
|
2011-03-12 22:19:41 +01:00
|
|
|
requests_.clear();
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:25:04 +02:00
|
|
|
void AcoustidClient::RequestFinished(QNetworkReply* reply, int id) {
|
2011-03-12 22:19:41 +01:00
|
|
|
reply->deleteLater();
|
2012-10-12 14:25:04 +02:00
|
|
|
requests_.remove(id);
|
2011-03-12 22:19:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() !=
|
|
|
|
200) {
|
2011-03-12 22:19:41 +01:00
|
|
|
emit Finished(id, QString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-06 17:31:29 +01:00
|
|
|
QJson::Parser parser;
|
|
|
|
bool ok = false;
|
|
|
|
QVariantMap result = parser.parse(reply, &ok).toMap();
|
|
|
|
if (!ok) {
|
|
|
|
emit Finished(id, QString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString status = result["status"].toString();
|
|
|
|
if (status != "ok") {
|
|
|
|
emit Finished(id, QString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QVariantList results = result["results"].toList();
|
2014-02-07 16:34:20 +01:00
|
|
|
foreach(const QVariant & v, results) {
|
2012-01-06 17:31:29 +01:00
|
|
|
QVariantMap r = v.toMap();
|
|
|
|
if (r.contains("recordings")) {
|
|
|
|
QVariantList recordings = r["recordings"].toList();
|
2014-02-07 16:34:20 +01:00
|
|
|
foreach(const QVariant & recording, recordings) {
|
2012-01-06 17:31:29 +01:00
|
|
|
QVariantMap o = recording.toMap();
|
|
|
|
if (o.contains("id")) {
|
|
|
|
emit Finished(id, o["id"].toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-03-12 22:19:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit Finished(id, QString());
|
|
|
|
}
|