1
/*****************************************************************************
2
* Copyright © 2012 John Maguire <john.maguire@gmail.com> *
3
* David Sansome <me@davidsansome.com> *
4
* This work is free. You can redistribute it and/or modify it under the *
5
* terms of the Do What The Fuck You Want To Public License, Version 2, *
6
* as published by Sam Hocevar. *
7
* See http://www.wtfpl.net/ for more details. *
8
*****************************************************************************/
10
#include <QCoreApplication>
11
#include <QNetworkReply>
12
#include <QXmlStreamReader>
14
#include "acoustidclient.h"
18
// see API-KEY site here http://acoustid.org/application/496
19
// I registered the KEY for version 1.12 -- kain88 (may 2013)
20
const QString CLIENT_APIKEY = "czKxnkyO";
21
const QString CLIENT_NAME = "Mixxx1.12";
22
const QString ACOUSTID_URL = "http://api.acoustid.org/v2/lookup";
23
const int AcoustidClient::m_DefaultTimeout = 5000; // msec
25
AcoustidClient::AcoustidClient(QObject* parent)
28
m_timeouts(m_DefaultTimeout, this) {
31
void AcoustidClient::setTimeout(int msec) {
32
m_timeouts.setTimeout(msec);
35
void AcoustidClient::start(int id, const QString& fingerprint, int duration) {
37
url.addQueryItem("format", "xml");
38
url.addQueryItem("client", CLIENT_APIKEY);
39
url.addQueryItem("duration", QString::number(duration));
40
url.addQueryItem("meta", "recordingids");
41
url.addQueryItem("fingerprint", fingerprint);
43
QNetworkRequest req(QUrl::fromEncoded(ACOUSTID_URL.toAscii()));
44
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
45
req.setRawHeader("Content-Encoding", "gzip");
46
req.setRawHeader("User-Agent", CLIENT_NAME.toAscii());
48
QNetworkReply* reply = m_network.post(req, gzipCompress(url.encodedQuery()));
49
connect(reply, SIGNAL(finished()), SLOT(requestFinished()));
50
m_requests[reply] = id;
52
m_timeouts.addReply(reply);
55
void AcoustidClient::cancel(int id) {
56
QNetworkReply* reply = m_requests.key(id);
57
m_requests.remove(reply);
61
void AcoustidClient::cancelAll() {
62
qDeleteAll(m_requests.keys());
66
void AcoustidClient::requestFinished() {
67
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
72
if (!m_requests.contains(reply))
75
int id = m_requests.take(reply);
77
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
78
emit finished(id, QString());
82
QXmlStreamReader reader(reply);
84
while (!reader.atEnd()) {
85
if (reader.readNext() == QXmlStreamReader::StartElement
86
&& reader.name()== "results") {
87
ID = parseResult(reader);
91
emit finished(id, ID);
94
QString AcoustidClient::parseResult(QXmlStreamReader& reader){
96
while (!reader.atEnd()) {
97
QXmlStreamReader::TokenType type = reader.readNext();
98
if (type== QXmlStreamReader::StartElement) {
99
QStringRef name = reader.name();
101
return reader.readElementText();
104
if (type == QXmlStreamReader::EndElement && reader.name()=="result") {