~mixxxdevelopers/mixxx/engine-control-refactor

« back to all changes in this revision

Viewing changes to mixxx/src/musicbrainz/acoustidclient.cpp

  • Committer: RJ Ryan
  • Date: 2013-06-04 00:41:29 UTC
  • mfrom: (2890.22.101 mixxx)
  • Revision ID: rryan@mixxx.org-20130604004129-8jjxkicsb3givu4a
Merging from lp:mixxx.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *****************************************************************************/
 
9
    
 
10
#include <QCoreApplication>
 
11
#include <QNetworkReply>
 
12
#include <QXmlStreamReader>
 
13
 
 
14
#include "acoustidclient.h"
 
15
#include "gzip.h"
 
16
#include "network.h"
 
17
 
 
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
 
24
 
 
25
AcoustidClient::AcoustidClient(QObject* parent)
 
26
              : QObject(parent),
 
27
                m_network(this),
 
28
                m_timeouts(m_DefaultTimeout, this) {
 
29
}
 
30
 
 
31
void AcoustidClient::setTimeout(int msec) {
 
32
    m_timeouts.setTimeout(msec);
 
33
}
 
34
 
 
35
void AcoustidClient::start(int id, const QString& fingerprint, int duration) {
 
36
    QUrl url;
 
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);
 
42
 
 
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());
 
47
    
 
48
    QNetworkReply* reply = m_network.post(req, gzipCompress(url.encodedQuery()));
 
49
    connect(reply, SIGNAL(finished()), SLOT(requestFinished()));
 
50
    m_requests[reply] = id;
 
51
    
 
52
    m_timeouts.addReply(reply);
 
53
}
 
54
 
 
55
void AcoustidClient::cancel(int id) {
 
56
    QNetworkReply* reply = m_requests.key(id);
 
57
    m_requests.remove(reply);
 
58
    delete reply;
 
59
}
 
60
 
 
61
void AcoustidClient::cancelAll() {
 
62
    qDeleteAll(m_requests.keys());
 
63
    m_requests.clear();
 
64
}
 
65
 
 
66
void AcoustidClient::requestFinished() {
 
67
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 
68
    if (!reply)
 
69
        return;
 
70
    
 
71
    reply->deleteLater();
 
72
    if (!m_requests.contains(reply))
 
73
        return;
 
74
    
 
75
    int id = m_requests.take(reply);
 
76
    
 
77
    if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
 
78
        emit finished(id, QString());
 
79
        return;
 
80
    }
 
81
 
 
82
    QXmlStreamReader reader(reply);
 
83
    QString ID;
 
84
    while (!reader.atEnd()) {
 
85
        if (reader.readNext() == QXmlStreamReader::StartElement 
 
86
            && reader.name()== "results") {
 
87
                ID = parseResult(reader);
 
88
            }
 
89
    }
 
90
 
 
91
    emit finished(id, ID);
 
92
}
 
93
 
 
94
QString AcoustidClient::parseResult(QXmlStreamReader& reader){
 
95
 
 
96
    while (!reader.atEnd()) {
 
97
        QXmlStreamReader::TokenType type = reader.readNext();
 
98
        if (type== QXmlStreamReader::StartElement) {
 
99
            QStringRef name = reader.name();
 
100
            if (name == "id") {
 
101
                return reader.readElementText();
 
102
            }
 
103
        }
 
104
        if (type == QXmlStreamReader::EndElement && reader.name()=="result") {
 
105
            break;
 
106
        }
 
107
    }
 
108
    return QString();
 
109
}